Skip to content

Commit ae5906c

Browse files
committed
Merge branch 'next-general' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux-security
Pull security subsystem updates from James Morris: - Extend LSM stacking to allow sharing of cred, file, ipc, inode, and task blobs. This paves the way for more full-featured LSMs to be merged, and is specifically aimed at LandLock and SARA LSMs. This work is from Casey and Kees. - There's a new LSM from Micah Morton: "SafeSetID gates the setid family of syscalls to restrict UID/GID transitions from a given UID/GID to only those approved by a system-wide whitelist." This feature is currently shipping in ChromeOS. * 'next-general' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux-security: (62 commits) keys: fix missing __user in KEYCTL_PKEY_QUERY LSM: Update list of SECURITYFS users in Kconfig LSM: Ignore "security=" when "lsm=" is specified LSM: Update function documentation for cap_capable security: mark expected switch fall-throughs and add a missing break tomoyo: Bump version. LSM: fix return value check in safesetid_init_securityfs() LSM: SafeSetID: add selftest LSM: SafeSetID: remove unused include LSM: SafeSetID: 'depend' on CONFIG_SECURITY LSM: Add 'name' field for SafeSetID in DEFINE_LSM LSM: add SafeSetID module that gates setid calls LSM: add SafeSetID module that gates setid calls tomoyo: Allow multiple use_group lines. tomoyo: Coding style fix. tomoyo: Swicth from cred->security to task_struct->security. security: keys: annotate implicit fall throughs security: keys: annotate implicit fall throughs security: keys: annotate implicit fall through capabilities:: annotate implicit fall through ...
2 parents 1fc1cd8 + 468e91c commit ae5906c

78 files changed

Lines changed: 2674 additions & 1090 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
=========
2+
SafeSetID
3+
=========
4+
SafeSetID is an LSM module that gates the setid family of syscalls to restrict
5+
UID/GID transitions from a given UID/GID to only those approved by a
6+
system-wide whitelist. These restrictions also prohibit the given UIDs/GIDs
7+
from obtaining auxiliary privileges associated with CAP_SET{U/G}ID, such as
8+
allowing a user to set up user namespace UID mappings.
9+
10+
11+
Background
12+
==========
13+
In absence of file capabilities, processes spawned on a Linux system that need
14+
to switch to a different user must be spawned with CAP_SETUID privileges.
15+
CAP_SETUID is granted to programs running as root or those running as a non-root
16+
user that have been explicitly given the CAP_SETUID runtime capability. It is
17+
often preferable to use Linux runtime capabilities rather than file
18+
capabilities, since using file capabilities to run a program with elevated
19+
privileges opens up possible security holes since any user with access to the
20+
file can exec() that program to gain the elevated privileges.
21+
22+
While it is possible to implement a tree of processes by giving full
23+
CAP_SET{U/G}ID capabilities, this is often at odds with the goals of running a
24+
tree of processes under non-root user(s) in the first place. Specifically,
25+
since CAP_SETUID allows changing to any user on the system, including the root
26+
user, it is an overpowered capability for what is needed in this scenario,
27+
especially since programs often only call setuid() to drop privileges to a
28+
lesser-privileged user -- not elevate privileges. Unfortunately, there is no
29+
generally feasible way in Linux to restrict the potential UIDs that a user can
30+
switch to through setuid() beyond allowing a switch to any user on the system.
31+
This SafeSetID LSM seeks to provide a solution for restricting setid
32+
capabilities in such a way.
33+
34+
The main use case for this LSM is to allow a non-root program to transition to
35+
other untrusted uids without full blown CAP_SETUID capabilities. The non-root
36+
program would still need CAP_SETUID to do any kind of transition, but the
37+
additional restrictions imposed by this LSM would mean it is a "safer" version
38+
of CAP_SETUID since the non-root program cannot take advantage of CAP_SETUID to
39+
do any unapproved actions (e.g. setuid to uid 0 or create/enter new user
40+
namespace). The higher level goal is to allow for uid-based sandboxing of system
41+
services without having to give out CAP_SETUID all over the place just so that
42+
non-root programs can drop to even-lesser-privileged uids. This is especially
43+
relevant when one non-root daemon on the system should be allowed to spawn other
44+
processes as different uids, but its undesirable to give the daemon a
45+
basically-root-equivalent CAP_SETUID.
46+
47+
48+
Other Approaches Considered
49+
===========================
50+
51+
Solve this problem in userspace
52+
-------------------------------
53+
For candidate applications that would like to have restricted setid capabilities
54+
as implemented in this LSM, an alternative option would be to simply take away
55+
setid capabilities from the application completely and refactor the process
56+
spawning semantics in the application (e.g. by using a privileged helper program
57+
to do process spawning and UID/GID transitions). Unfortunately, there are a
58+
number of semantics around process spawning that would be affected by this, such
59+
as fork() calls where the program doesn???t immediately call exec() after the
60+
fork(), parent processes specifying custom environment variables or command line
61+
args for spawned child processes, or inheritance of file handles across a
62+
fork()/exec(). Because of this, as solution that uses a privileged helper in
63+
userspace would likely be less appealing to incorporate into existing projects
64+
that rely on certain process-spawning semantics in Linux.
65+
66+
Use user namespaces
67+
-------------------
68+
Another possible approach would be to run a given process tree in its own user
69+
namespace and give programs in the tree setid capabilities. In this way,
70+
programs in the tree could change to any desired UID/GID in the context of their
71+
own user namespace, and only approved UIDs/GIDs could be mapped back to the
72+
initial system user namespace, affectively preventing privilege escalation.
73+
Unfortunately, it is not generally feasible to use user namespaces in isolation,
74+
without pairing them with other namespace types, which is not always an option.
75+
Linux checks for capabilities based off of the user namespace that ???owns??? some
76+
entity. For example, Linux has the notion that network namespaces are owned by
77+
the user namespace in which they were created. A consequence of this is that
78+
capability checks for access to a given network namespace are done by checking
79+
whether a task has the given capability in the context of the user namespace
80+
that owns the network namespace -- not necessarily the user namespace under
81+
which the given task runs. Therefore spawning a process in a new user namespace
82+
effectively prevents it from accessing the network namespace owned by the
83+
initial namespace. This is a deal-breaker for any application that expects to
84+
retain the CAP_NET_ADMIN capability for the purpose of adjusting network
85+
configurations. Using user namespaces in isolation causes problems regarding
86+
other system interactions, including use of pid namespaces and device creation.
87+
88+
Use an existing LSM
89+
-------------------
90+
None of the other in-tree LSMs have the capability to gate setid transitions, or
91+
even employ the security_task_fix_setuid hook at all. SELinux says of that hook:
92+
"Since setuid only affects the current process, and since the SELinux controls
93+
are not based on the Linux identity attributes, SELinux does not need to control
94+
this operation."
95+
96+
97+
Directions for use
98+
==================
99+
This LSM hooks the setid syscalls to make sure transitions are allowed if an
100+
applicable restriction policy is in place. Policies are configured through
101+
securityfs by writing to the safesetid/add_whitelist_policy and
102+
safesetid/flush_whitelist_policies files at the location where securityfs is
103+
mounted. The format for adding a policy is '<UID>:<UID>', using literal
104+
numbers, such as '123:456'. To flush the policies, any write to the file is
105+
sufficient. Again, configuring a policy for a UID will prevent that UID from
106+
obtaining auxiliary setid privileges, such as allowing a user to set up user
107+
namespace UID mappings.

‎Documentation/admin-guide/LSM/index.rst‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ MAC extensions, other extensions can be built using the LSM to provide
1717
specific changes to system operation when these tweaks are not available
1818
in the core functionality of Linux itself.
1919

20-
Without a specific LSM built into the kernel, the default LSM will be the
21-
Linux capabilities system. Most LSMs choose to extend the capabilities
22-
system, building their checks on top of the defined capability hooks.
20+
The Linux capabilities modules will always be included. This may be
21+
followed by any number of "minor" modules and at most one "major" module.
2322
For more details on capabilities, see ``capabilities(7)`` in the Linux
2423
man-pages project.
2524

@@ -30,6 +29,14 @@ order in which checks are made. The capability module will always
3029
be first, followed by any "minor" modules (e.g. Yama) and then
3130
the one "major" module (e.g. SELinux) if there is one configured.
3231

32+
Process attributes associated with "major" security modules should
33+
be accessed and maintained using the special files in ``/proc/.../attr``.
34+
A security module may maintain a module specific subdirectory there,
35+
named after the module. ``/proc/.../attr/smack`` is provided by the Smack
36+
security module and contains all its special files. The files directly
37+
in ``/proc/.../attr`` remain as legacy interfaces for modules that provide
38+
subdirectories.
39+
3340
.. toctree::
3441
:maxdepth: 1
3542

@@ -39,3 +46,4 @@ the one "major" module (e.g. SELinux) if there is one configured.
3946
Smack
4047
tomoyo
4148
Yama
49+
SafeSetID

‎Documentation/admin-guide/kernel-parameters.txt‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2333,6 +2333,10 @@
23332333

23342334
lsm.debug [SECURITY] Enable LSM initialization debugging output.
23352335

2336+
lsm=lsm1,...,lsmN
2337+
[SECURITY] Choose order of LSM initialization. This
2338+
overrides CONFIG_LSM, and the "security=" parameter.
2339+
23362340
machvec= [IA-64] Force the use of a particular machine-vector
23372341
(machvec) in a generic kernel.
23382342
Example: machvec=hpzx1_swiotlb
@@ -4110,11 +4114,9 @@
41104114
Note: increases power consumption, thus should only be
41114115
enabled if running jitter sensitive (HPC/RT) workloads.
41124116

4113-
security= [SECURITY] Choose a security module to enable at boot.
4114-
If this boot parameter is not specified, only the first
4115-
security module asking for security registration will be
4116-
loaded. An invalid security module name will be treated
4117-
as if no module has been chosen.
4117+
security= [SECURITY] Choose a legacy "major" security module to
4118+
enable at boot. This has been deprecated by the
4119+
"lsm=" parameter.
41184120

41194121
selinux= [SELINUX] Disable or enable SELinux at boot time.
41204122
Format: { "0" | "1" }

‎MAINTAINERS‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15557,12 +15557,11 @@ F: mm/shmem.c
1555715557
TOMOYO SECURITY MODULE
1555815558
M: Kentaro Takeda <takedakn@nttdata.co.jp>
1555915559
M: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
15560-
L: tomoyo-dev-en@lists.sourceforge.jp (subscribers-only, for developers in English)
15561-
L: tomoyo-users-en@lists.sourceforge.jp (subscribers-only, for users in English)
15562-
L: tomoyo-dev@lists.sourceforge.jp (subscribers-only, for developers in Japanese)
15563-
L: tomoyo-users@lists.sourceforge.jp (subscribers-only, for users in Japanese)
15564-
W: http://tomoyo.sourceforge.jp/
15565-
T: quilt http://svn.sourceforge.jp/svnroot/tomoyo/trunk/2.5.x/tomoyo-lsm/patches/
15560+
L: tomoyo-dev-en@lists.osdn.me (subscribers-only, for developers in English)
15561+
L: tomoyo-users-en@lists.osdn.me (subscribers-only, for users in English)
15562+
L: tomoyo-dev@lists.osdn.me (subscribers-only, for developers in Japanese)
15563+
L: tomoyo-users@lists.osdn.me (subscribers-only, for users in Japanese)
15564+
W: https://tomoyo.osdn.jp/
1556615565
S: Maintained
1556715566
F: security/tomoyo/
1556815567

‎fs/proc/base.c‎

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,13 @@ struct pid_entry {
140140
#define REG(NAME, MODE, fops) \
141141
NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
142142
#define ONE(NAME, MODE, show) \
143-
NOD(NAME, (S_IFREG|(MODE)), \
143+
NOD(NAME, (S_IFREG|(MODE)), \
144144
NULL, &proc_single_file_operations, \
145145
{ .proc_show = show } )
146+
#define ATTR(LSM, NAME, MODE) \
147+
NOD(NAME, (S_IFREG|(MODE)), \
148+
NULL, &proc_pid_attr_operations, \
149+
{ .lsm = LSM })
146150

147151
/*
148152
* Count the number of hardlinks for the pid_entry table, excluding the .
@@ -2521,7 +2525,7 @@ static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
25212525
if (!task)
25222526
return -ESRCH;
25232527

2524-
length = security_getprocattr(task,
2528+
length = security_getprocattr(task, PROC_I(inode)->op.lsm,
25252529
(char*)file->f_path.dentry->d_name.name,
25262530
&p);
25272531
put_task_struct(task);
@@ -2570,7 +2574,9 @@ static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
25702574
if (rv < 0)
25712575
goto out_free;
25722576

2573-
rv = security_setprocattr(file->f_path.dentry->d_name.name, page, count);
2577+
rv = security_setprocattr(PROC_I(inode)->op.lsm,
2578+
file->f_path.dentry->d_name.name, page,
2579+
count);
25742580
mutex_unlock(&current->signal->cred_guard_mutex);
25752581
out_free:
25762582
kfree(page);
@@ -2584,13 +2590,53 @@ static const struct file_operations proc_pid_attr_operations = {
25842590
.llseek = generic_file_llseek,
25852591
};
25862592

2593+
#define LSM_DIR_OPS(LSM) \
2594+
static int proc_##LSM##_attr_dir_iterate(struct file *filp, \
2595+
struct dir_context *ctx) \
2596+
{ \
2597+
return proc_pident_readdir(filp, ctx, \
2598+
LSM##_attr_dir_stuff, \
2599+
ARRAY_SIZE(LSM##_attr_dir_stuff)); \
2600+
} \
2601+
\
2602+
static const struct file_operations proc_##LSM##_attr_dir_ops = { \
2603+
.read = generic_read_dir, \
2604+
.iterate = proc_##LSM##_attr_dir_iterate, \
2605+
.llseek = default_llseek, \
2606+
}; \
2607+
\
2608+
static struct dentry *proc_##LSM##_attr_dir_lookup(struct inode *dir, \
2609+
struct dentry *dentry, unsigned int flags) \
2610+
{ \
2611+
return proc_pident_lookup(dir, dentry, \
2612+
LSM##_attr_dir_stuff, \
2613+
ARRAY_SIZE(LSM##_attr_dir_stuff)); \
2614+
} \
2615+
\
2616+
static const struct inode_operations proc_##LSM##_attr_dir_inode_ops = { \
2617+
.lookup = proc_##LSM##_attr_dir_lookup, \
2618+
.getattr = pid_getattr, \
2619+
.setattr = proc_setattr, \
2620+
}
2621+
2622+
#ifdef CONFIG_SECURITY_SMACK
2623+
static const struct pid_entry smack_attr_dir_stuff[] = {
2624+
ATTR("smack", "current", 0666),
2625+
};
2626+
LSM_DIR_OPS(smack);
2627+
#endif
2628+
25872629
static const struct pid_entry attr_dir_stuff[] = {
2588-
REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2589-
REG("prev", S_IRUGO, proc_pid_attr_operations),
2590-
REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2591-
REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2592-
REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2593-
REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2630+
ATTR(NULL, "current", 0666),
2631+
ATTR(NULL, "prev", 0444),
2632+
ATTR(NULL, "exec", 0666),
2633+
ATTR(NULL, "fscreate", 0666),
2634+
ATTR(NULL, "keycreate", 0666),
2635+
ATTR(NULL, "sockcreate", 0666),
2636+
#ifdef CONFIG_SECURITY_SMACK
2637+
DIR("smack", 0555,
2638+
proc_smack_attr_dir_inode_ops, proc_smack_attr_dir_ops),
2639+
#endif
25942640
};
25952641

25962642
static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)

‎fs/proc/internal.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ union proc_op {
8282
int (*proc_show)(struct seq_file *m,
8383
struct pid_namespace *ns, struct pid *pid,
8484
struct task_struct *task);
85+
const char *lsm;
8586
};
8687

8788
struct proc_inode {

‎include/linux/capability.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ extern bool has_ns_capability_noaudit(struct task_struct *t,
209209
extern bool capable(int cap);
210210
extern bool ns_capable(struct user_namespace *ns, int cap);
211211
extern bool ns_capable_noaudit(struct user_namespace *ns, int cap);
212+
extern bool ns_capable_setid(struct user_namespace *ns, int cap);
212213
#else
213214
static inline bool has_capability(struct task_struct *t, int cap)
214215
{
@@ -240,6 +241,10 @@ static inline bool ns_capable_noaudit(struct user_namespace *ns, int cap)
240241
{
241242
return true;
242243
}
244+
static inline bool ns_capable_setid(struct user_namespace *ns, int cap)
245+
{
246+
return true;
247+
}
243248
#endif /* CONFIG_MULTIUSER */
244249
extern bool privileged_wrt_inode_uidgid(struct user_namespace *ns, const struct inode *inode);
245250
extern bool capable_wrt_inode_uidgid(const struct inode *inode, int cap);

‎include/linux/cred.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include <linux/capability.h>
1616
#include <linux/init.h>
1717
#include <linux/key.h>
18-
#include <linux/selinux.h>
1918
#include <linux/atomic.h>
2019
#include <linux/uidgid.h>
2120
#include <linux/sched.h>

‎include/linux/lsm_hooks.h‎

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,7 +1270,7 @@
12701270
* @cred contains the credentials to use.
12711271
* @ns contains the user namespace we want the capability in
12721272
* @cap contains the capability <include/linux/capability.h>.
1273-
* @audit contains whether to write an audit message or not
1273+
* @opts contains options for the capable check <include/linux/security.h>
12741274
* Return 0 if the capability is granted for @tsk.
12751275
* @syslog:
12761276
* Check permission before accessing the kernel message ring or changing
@@ -1446,8 +1446,10 @@ union security_list_options {
14461446
const kernel_cap_t *effective,
14471447
const kernel_cap_t *inheritable,
14481448
const kernel_cap_t *permitted);
1449-
int (*capable)(const struct cred *cred, struct user_namespace *ns,
1450-
int cap, int audit);
1449+
int (*capable)(const struct cred *cred,
1450+
struct user_namespace *ns,
1451+
int cap,
1452+
unsigned int opts);
14511453
int (*quotactl)(int cmds, int type, int id, struct super_block *sb);
14521454
int (*quota_on)(struct dentry *dentry);
14531455
int (*syslog)(int type);
@@ -2027,6 +2029,18 @@ struct security_hook_list {
20272029
char *lsm;
20282030
} __randomize_layout;
20292031

2032+
/*
2033+
* Security blob size or offset data.
2034+
*/
2035+
struct lsm_blob_sizes {
2036+
int lbs_cred;
2037+
int lbs_file;
2038+
int lbs_inode;
2039+
int lbs_ipc;
2040+
int lbs_msg_msg;
2041+
int lbs_task;
2042+
};
2043+
20302044
/*
20312045
* Initializing a security_hook_list structure takes
20322046
* up a lot of space in a source file. This macro takes
@@ -2042,9 +2056,21 @@ extern char *lsm_names;
20422056
extern void security_add_hooks(struct security_hook_list *hooks, int count,
20432057
char *lsm);
20442058

2059+
#define LSM_FLAG_LEGACY_MAJOR BIT(0)
2060+
#define LSM_FLAG_EXCLUSIVE BIT(1)
2061+
2062+
enum lsm_order {
2063+
LSM_ORDER_FIRST = -1, /* This is only for capabilities. */
2064+
LSM_ORDER_MUTABLE = 0,
2065+
};
2066+
20452067
struct lsm_info {
20462068
const char *name; /* Required. */
2069+
enum lsm_order order; /* Optional: default is LSM_ORDER_MUTABLE */
2070+
unsigned long flags; /* Optional: flags describing LSM */
2071+
int *enabled; /* Optional: controlled by CONFIG_LSM */
20472072
int (*init)(void); /* Required. */
2073+
struct lsm_blob_sizes *blobs; /* Optional: for blob sharing. */
20482074
};
20492075

20502076
extern struct lsm_info __start_lsm_info[], __end_lsm_info[];
@@ -2084,17 +2110,6 @@ static inline void security_delete_hooks(struct security_hook_list *hooks,
20842110
#define __lsm_ro_after_init __ro_after_init
20852111
#endif /* CONFIG_SECURITY_WRITABLE_HOOKS */
20862112

2087-
extern int __init security_module_enable(const char *module);
2088-
extern void __init capability_add_hooks(void);
2089-
#ifdef CONFIG_SECURITY_YAMA
2090-
extern void __init yama_add_hooks(void);
2091-
#else
2092-
static inline void __init yama_add_hooks(void) { }
2093-
#endif
2094-
#ifdef CONFIG_SECURITY_LOADPIN
2095-
void __init loadpin_add_hooks(void);
2096-
#else
2097-
static inline void loadpin_add_hooks(void) { };
2098-
#endif
2113+
extern int lsm_inode_alloc(struct inode *inode);
20992114

21002115
#endif /* ! __LINUX_LSM_HOOKS_H */

0 commit comments

Comments
 (0)