Skip to content

Commit d84f4f9

Browse files
dhowellsJames Morris
authored andcommitted
CRED: Inaugurate COW credentials
Inaugurate copy-on-write credentials management. This uses RCU to manage the credentials pointer in the task_struct with respect to accesses by other tasks. A process may only modify its own credentials, and so does not need locking to access or modify its own credentials. A mutex (cred_replace_mutex) is added to the task_struct to control the effect of PTRACE_ATTACHED on credential calculations, particularly with respect to execve(). With this patch, the contents of an active credentials struct may not be changed directly; rather a new set of credentials must be prepared, modified and committed using something like the following sequence of events: struct cred *new = prepare_creds(); int ret = blah(new); if (ret < 0) { abort_creds(new); return ret; } return commit_creds(new); There are some exceptions to this rule: the keyrings pointed to by the active credentials may be instantiated - keyrings violate the COW rule as managing COW keyrings is tricky, given that it is possible for a task to directly alter the keys in a keyring in use by another task. To help enforce this, various pointers to sets of credentials, such as those in the task_struct, are declared const. The purpose of this is compile-time discouragement of altering credentials through those pointers. Once a set of credentials has been made public through one of these pointers, it may not be modified, except under special circumstances: (1) Its reference count may incremented and decremented. (2) The keyrings to which it points may be modified, but not replaced. The only safe way to modify anything else is to create a replacement and commit using the functions described in Documentation/credentials.txt (which will be added by a later patch). This patch and the preceding patches have been tested with the LTP SELinux testsuite. This patch makes several logical sets of alteration: (1) execve(). This now prepares and commits credentials in various places in the security code rather than altering the current creds directly. (2) Temporary credential overrides. do_coredump() and sys_faccessat() now prepare their own credentials and temporarily override the ones currently on the acting thread, whilst preventing interference from other threads by holding cred_replace_mutex on the thread being dumped. This will be replaced in a future patch by something that hands down the credentials directly to the functions being called, rather than altering the task's objective credentials. (3) LSM interface. A number of functions have been changed, added or removed: (*) security_capset_check(), ->capset_check() (*) security_capset_set(), ->capset_set() Removed in favour of security_capset(). (*) security_capset(), ->capset() New. This is passed a pointer to the new creds, a pointer to the old creds and the proposed capability sets. It should fill in the new creds or return an error. All pointers, barring the pointer to the new creds, are now const. (*) security_bprm_apply_creds(), ->bprm_apply_creds() Changed; now returns a value, which will cause the process to be killed if it's an error. (*) security_task_alloc(), ->task_alloc_security() Removed in favour of security_prepare_creds(). (*) security_cred_free(), ->cred_free() New. Free security data attached to cred->security. (*) security_prepare_creds(), ->cred_prepare() New. Duplicate any security data attached to cred->security. (*) security_commit_creds(), ->cred_commit() New. Apply any security effects for the upcoming installation of new security by commit_creds(). (*) security_task_post_setuid(), ->task_post_setuid() Removed in favour of security_task_fix_setuid(). (*) security_task_fix_setuid(), ->task_fix_setuid() Fix up the proposed new credentials for setuid(). This is used by cap_set_fix_setuid() to implicitly adjust capabilities in line with setuid() changes. Changes are made to the new credentials, rather than the task itself as in security_task_post_setuid(). (*) security_task_reparent_to_init(), ->task_reparent_to_init() Removed. Instead the task being reparented to init is referred directly to init's credentials. NOTE! This results in the loss of some state: SELinux's osid no longer records the sid of the thread that forked it. (*) security_key_alloc(), ->key_alloc() (*) security_key_permission(), ->key_permission() Changed. These now take cred pointers rather than task pointers to refer to the security context. (4) sys_capset(). This has been simplified and uses less locking. The LSM functions it calls have been merged. (5) reparent_to_kthreadd(). This gives the current thread the same credentials as init by simply using commit_thread() to point that way. (6) __sigqueue_alloc() and switch_uid() __sigqueue_alloc() can't stop the target task from changing its creds beneath it, so this function gets a reference to the currently applicable user_struct which it then passes into the sigqueue struct it returns if successful. switch_uid() is now called from commit_creds(), and possibly should be folded into that. commit_creds() should take care of protecting __sigqueue_alloc(). (7) [sg]et[ug]id() and co and [sg]et_current_groups. The set functions now all use prepare_creds(), commit_creds() and abort_creds() to build and check a new set of credentials before applying it. security_task_set[ug]id() is called inside the prepared section. This guarantees that nothing else will affect the creds until we've finished. The calling of set_dumpable() has been moved into commit_creds(). Much of the functionality of set_user() has been moved into commit_creds(). The get functions all simply access the data directly. (8) security_task_prctl() and cap_task_prctl(). security_task_prctl() has been modified to return -ENOSYS if it doesn't want to handle a function, or otherwise return the return value directly rather than through an argument. Additionally, cap_task_prctl() now prepares a new set of credentials, even if it doesn't end up using it. (9) Keyrings. A number of changes have been made to the keyrings code: (a) switch_uid_keyring(), copy_keys(), exit_keys() and suid_keys() have all been dropped and built in to the credentials functions directly. They may want separating out again later. (b) key_alloc() and search_process_keyrings() now take a cred pointer rather than a task pointer to specify the security context. (c) copy_creds() gives a new thread within the same thread group a new thread keyring if its parent had one, otherwise it discards the thread keyring. (d) The authorisation key now points directly to the credentials to extend the search into rather pointing to the task that carries them. (e) Installing thread, process or session keyrings causes a new set of credentials to be created, even though it's not strictly necessary for process or session keyrings (they're shared). (10) Usermode helper. The usermode helper code now carries a cred struct pointer in its subprocess_info struct instead of a new session keyring pointer. This set of credentials is derived from init_cred and installed on the new process after it has been cloned. call_usermodehelper_setup() allocates the new credentials and call_usermodehelper_freeinfo() discards them if they haven't been used. A special cred function (prepare_usermodeinfo_creds()) is provided specifically for call_usermodehelper_setup() to call. call_usermodehelper_setkeys() adjusts the credentials to sport the supplied keyring as the new session keyring. (11) SELinux. SELinux has a number of changes, in addition to those to support the LSM interface changes mentioned above: (a) selinux_setprocattr() no longer does its check for whether the current ptracer can access processes with the new SID inside the lock that covers getting the ptracer's SID. Whilst this lock ensures that the check is done with the ptracer pinned, the result is only valid until the lock is released, so there's no point doing it inside the lock. (12) is_single_threaded(). This function has been extracted from selinux_setprocattr() and put into a file of its own in the lib/ directory as join_session_keyring() now wants to use it too. The code in SELinux just checked to see whether a task shared mm_structs with other tasks (CLONE_VM), but that isn't good enough. We really want to know if they're part of the same thread group (CLONE_THREAD). (13) nfsd. The NFS server daemon now has to use the COW credentials to set the credentials it is going to use. It really needs to pass the credentials down to the functions it calls, but it can't do that until other patches in this series have been applied. Signed-off-by: David Howells <dhowells@redhat.com> Acked-by: James Morris <jmorris@namei.org> Signed-off-by: James Morris <jmorris@namei.org>
1 parent 745ca24 commit d84f4f9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1603
-1239
lines changed

fs/exec.c

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,13 +1007,12 @@ int flush_old_exec(struct linux_binprm * bprm)
10071007
*/
10081008
current->mm->task_size = TASK_SIZE;
10091009

1010-
if (bprm->e_uid != current_euid() || bprm->e_gid != current_egid()) {
1011-
suid_keys(current);
1010+
if (bprm->e_uid != current_euid() ||
1011+
bprm->e_gid != current_egid()) {
10121012
set_dumpable(current->mm, suid_dumpable);
10131013
current->pdeath_signal = 0;
10141014
} else if (file_permission(bprm->file, MAY_READ) ||
10151015
(bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)) {
1016-
suid_keys(current);
10171016
set_dumpable(current->mm, suid_dumpable);
10181017
}
10191018

@@ -1096,10 +1095,8 @@ void compute_creds(struct linux_binprm *bprm)
10961095
{
10971096
int unsafe;
10981097

1099-
if (bprm->e_uid != current_uid()) {
1100-
suid_keys(current);
1098+
if (bprm->e_uid != current_uid())
11011099
current->pdeath_signal = 0;
1102-
}
11031100
exec_keys(current);
11041101

11051102
task_lock(current);
@@ -1709,8 +1706,9 @@ int do_coredump(long signr, int exit_code, struct pt_regs * regs)
17091706
struct linux_binfmt * binfmt;
17101707
struct inode * inode;
17111708
struct file * file;
1709+
const struct cred *old_cred;
1710+
struct cred *cred;
17121711
int retval = 0;
1713-
int fsuid = current_fsuid();
17141712
int flag = 0;
17151713
int ispipe = 0;
17161714
unsigned long core_limit = current->signal->rlim[RLIMIT_CORE].rlim_cur;
@@ -1723,12 +1721,20 @@ int do_coredump(long signr, int exit_code, struct pt_regs * regs)
17231721
binfmt = current->binfmt;
17241722
if (!binfmt || !binfmt->core_dump)
17251723
goto fail;
1724+
1725+
cred = prepare_creds();
1726+
if (!cred) {
1727+
retval = -ENOMEM;
1728+
goto fail;
1729+
}
1730+
17261731
down_write(&mm->mmap_sem);
17271732
/*
17281733
* If another thread got here first, or we are not dumpable, bail out.
17291734
*/
17301735
if (mm->core_state || !get_dumpable(mm)) {
17311736
up_write(&mm->mmap_sem);
1737+
put_cred(cred);
17321738
goto fail;
17331739
}
17341740

@@ -1739,12 +1745,16 @@ int do_coredump(long signr, int exit_code, struct pt_regs * regs)
17391745
*/
17401746
if (get_dumpable(mm) == 2) { /* Setuid core dump mode */
17411747
flag = O_EXCL; /* Stop rewrite attacks */
1742-
current->cred->fsuid = 0; /* Dump root private */
1748+
cred->fsuid = 0; /* Dump root private */
17431749
}
17441750

17451751
retval = coredump_wait(exit_code, &core_state);
1746-
if (retval < 0)
1752+
if (retval < 0) {
1753+
put_cred(cred);
17471754
goto fail;
1755+
}
1756+
1757+
old_cred = override_creds(cred);
17481758

17491759
/*
17501760
* Clear any false indication of pending signals that might
@@ -1835,7 +1845,8 @@ int do_coredump(long signr, int exit_code, struct pt_regs * regs)
18351845
if (helper_argv)
18361846
argv_free(helper_argv);
18371847

1838-
current->cred->fsuid = fsuid;
1848+
revert_creds(old_cred);
1849+
put_cred(cred);
18391850
coredump_finish(mm);
18401851
fail:
18411852
return retval;

fs/nfsd/auth.c

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -27,55 +27,67 @@ int nfsexp_flags(struct svc_rqst *rqstp, struct svc_export *exp)
2727

2828
int nfsd_setuser(struct svc_rqst *rqstp, struct svc_export *exp)
2929
{
30-
struct cred *act_as = current->cred ;
31-
struct svc_cred cred = rqstp->rq_cred;
30+
struct group_info *rqgi;
31+
struct group_info *gi;
32+
struct cred *new;
3233
int i;
3334
int flags = nfsexp_flags(rqstp, exp);
3435
int ret;
3536

37+
new = prepare_creds();
38+
if (!new)
39+
return -ENOMEM;
40+
41+
new->fsuid = rqstp->rq_cred.cr_uid;
42+
new->fsgid = rqstp->rq_cred.cr_gid;
43+
44+
rqgi = rqstp->rq_cred.cr_group_info;
45+
3646
if (flags & NFSEXP_ALLSQUASH) {
37-
cred.cr_uid = exp->ex_anon_uid;
38-
cred.cr_gid = exp->ex_anon_gid;
39-
cred.cr_group_info = groups_alloc(0);
47+
new->fsuid = exp->ex_anon_uid;
48+
new->fsgid = exp->ex_anon_gid;
49+
gi = groups_alloc(0);
4050
} else if (flags & NFSEXP_ROOTSQUASH) {
41-
struct group_info *gi;
42-
if (!cred.cr_uid)
43-
cred.cr_uid = exp->ex_anon_uid;
44-
if (!cred.cr_gid)
45-
cred.cr_gid = exp->ex_anon_gid;
46-
gi = groups_alloc(cred.cr_group_info->ngroups);
47-
if (gi)
48-
for (i = 0; i < cred.cr_group_info->ngroups; i++) {
49-
if (!GROUP_AT(cred.cr_group_info, i))
50-
GROUP_AT(gi, i) = exp->ex_anon_gid;
51-
else
52-
GROUP_AT(gi, i) = GROUP_AT(cred.cr_group_info, i);
53-
}
54-
cred.cr_group_info = gi;
55-
} else
56-
get_group_info(cred.cr_group_info);
57-
58-
if (cred.cr_uid != (uid_t) -1)
59-
act_as->fsuid = cred.cr_uid;
60-
else
61-
act_as->fsuid = exp->ex_anon_uid;
62-
if (cred.cr_gid != (gid_t) -1)
63-
act_as->fsgid = cred.cr_gid;
64-
else
65-
act_as->fsgid = exp->ex_anon_gid;
51+
if (!new->fsuid)
52+
new->fsuid = exp->ex_anon_uid;
53+
if (!new->fsgid)
54+
new->fsgid = exp->ex_anon_gid;
6655

67-
if (!cred.cr_group_info)
68-
return -ENOMEM;
69-
ret = set_groups(act_as, cred.cr_group_info);
70-
put_group_info(cred.cr_group_info);
71-
if ((cred.cr_uid)) {
72-
act_as->cap_effective =
73-
cap_drop_nfsd_set(act_as->cap_effective);
56+
gi = groups_alloc(rqgi->ngroups);
57+
if (!gi)
58+
goto oom;
59+
60+
for (i = 0; i < rqgi->ngroups; i++) {
61+
if (!GROUP_AT(rqgi, i))
62+
GROUP_AT(gi, i) = exp->ex_anon_gid;
63+
else
64+
GROUP_AT(gi, i) = GROUP_AT(rqgi, i);
65+
}
7466
} else {
75-
act_as->cap_effective =
76-
cap_raise_nfsd_set(act_as->cap_effective,
77-
act_as->cap_permitted);
67+
gi = get_group_info(rqgi);
7868
}
69+
70+
if (new->fsuid == (uid_t) -1)
71+
new->fsuid = exp->ex_anon_uid;
72+
if (new->fsgid == (gid_t) -1)
73+
new->fsgid = exp->ex_anon_gid;
74+
75+
ret = set_groups(new, gi);
76+
put_group_info(gi);
77+
if (!ret)
78+
goto error;
79+
80+
if (new->uid)
81+
new->cap_effective = cap_drop_nfsd_set(new->cap_effective);
82+
else
83+
new->cap_effective = cap_raise_nfsd_set(new->cap_effective,
84+
new->cap_permitted);
85+
return commit_creds(new);
86+
87+
oom:
88+
ret = -ENOMEM;
89+
error:
90+
abort_creds(new);
7991
return ret;
8092
}
8193

fs/nfsd/nfs4recover.c

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,26 @@
5454
static struct path rec_dir;
5555
static int rec_dir_init = 0;
5656

57-
static void
58-
nfs4_save_user(uid_t *saveuid, gid_t *savegid)
57+
static int
58+
nfs4_save_creds(const struct cred **original_creds)
5959
{
60-
*saveuid = current->cred->fsuid;
61-
*savegid = current->cred->fsgid;
62-
current->cred->fsuid = 0;
63-
current->cred->fsgid = 0;
60+
struct cred *new;
61+
62+
new = prepare_creds();
63+
if (!new)
64+
return -ENOMEM;
65+
66+
new->fsuid = 0;
67+
new->fsgid = 0;
68+
*original_creds = override_creds(new);
69+
put_cred(new);
70+
return 0;
6471
}
6572

6673
static void
67-
nfs4_reset_user(uid_t saveuid, gid_t savegid)
74+
nfs4_reset_creds(const struct cred *original)
6875
{
69-
current->cred->fsuid = saveuid;
70-
current->cred->fsgid = savegid;
76+
revert_creds(original);
7177
}
7278

7379
static void
@@ -129,18 +135,19 @@ nfsd4_sync_rec_dir(void)
129135
int
130136
nfsd4_create_clid_dir(struct nfs4_client *clp)
131137
{
138+
const struct cred *original_cred;
132139
char *dname = clp->cl_recdir;
133140
struct dentry *dentry;
134-
uid_t uid;
135-
gid_t gid;
136141
int status;
137142

138143
dprintk("NFSD: nfsd4_create_clid_dir for \"%s\"\n", dname);
139144

140145
if (!rec_dir_init || clp->cl_firststate)
141146
return 0;
142147

143-
nfs4_save_user(&uid, &gid);
148+
status = nfs4_save_creds(&original_cred);
149+
if (status < 0)
150+
return status;
144151

145152
/* lock the parent */
146153
mutex_lock(&rec_dir.dentry->d_inode->i_mutex);
@@ -168,7 +175,7 @@ nfsd4_create_clid_dir(struct nfs4_client *clp)
168175
clp->cl_firststate = 1;
169176
nfsd4_sync_rec_dir();
170177
}
171-
nfs4_reset_user(uid, gid);
178+
nfs4_reset_creds(original_cred);
172179
dprintk("NFSD: nfsd4_create_clid_dir returns %d\n", status);
173180
return status;
174181
}
@@ -211,20 +218,21 @@ nfsd4_build_dentrylist(void *arg, const char *name, int namlen,
211218
static int
212219
nfsd4_list_rec_dir(struct dentry *dir, recdir_func *f)
213220
{
221+
const struct cred *original_cred;
214222
struct file *filp;
215223
struct dentry_list_arg dla = {
216224
.parent = dir,
217225
};
218226
struct list_head *dentries = &dla.dentries;
219227
struct dentry_list *child;
220-
uid_t uid;
221-
gid_t gid;
222228
int status;
223229

224230
if (!rec_dir_init)
225231
return 0;
226232

227-
nfs4_save_user(&uid, &gid);
233+
status = nfs4_save_creds(&original_cred);
234+
if (status < 0)
235+
return status;
228236

229237
filp = dentry_open(dget(dir), mntget(rec_dir.mnt), O_RDONLY,
230238
current_cred());
@@ -250,7 +258,7 @@ nfsd4_list_rec_dir(struct dentry *dir, recdir_func *f)
250258
dput(child->dentry);
251259
kfree(child);
252260
}
253-
nfs4_reset_user(uid, gid);
261+
nfs4_reset_creds(original_cred);
254262
return status;
255263
}
256264

@@ -312,8 +320,7 @@ nfsd4_unlink_clid_dir(char *name, int namlen)
312320
void
313321
nfsd4_remove_clid_dir(struct nfs4_client *clp)
314322
{
315-
uid_t uid;
316-
gid_t gid;
323+
const struct cred *original_cred;
317324
int status;
318325

319326
if (!rec_dir_init || !clp->cl_firststate)
@@ -323,9 +330,13 @@ nfsd4_remove_clid_dir(struct nfs4_client *clp)
323330
if (status)
324331
goto out;
325332
clp->cl_firststate = 0;
326-
nfs4_save_user(&uid, &gid);
333+
334+
status = nfs4_save_creds(&original_cred);
335+
if (status < 0)
336+
goto out;
337+
327338
status = nfsd4_unlink_clid_dir(clp->cl_recdir, HEXDIR_LEN-1);
328-
nfs4_reset_user(uid, gid);
339+
nfs4_reset_creds(original_cred);
329340
if (status == 0)
330341
nfsd4_sync_rec_dir();
331342
mnt_drop_write(rec_dir.mnt);
@@ -402,16 +413,21 @@ nfsd4_recdir_load(void) {
402413
void
403414
nfsd4_init_recdir(char *rec_dirname)
404415
{
405-
uid_t uid = 0;
406-
gid_t gid = 0;
407-
int status;
416+
const struct cred *original_cred;
417+
int status;
408418

409419
printk("NFSD: Using %s as the NFSv4 state recovery directory\n",
410420
rec_dirname);
411421

412422
BUG_ON(rec_dir_init);
413423

414-
nfs4_save_user(&uid, &gid);
424+
status = nfs4_save_creds(&original_cred);
425+
if (status < 0) {
426+
printk("NFSD: Unable to change credentials to find recovery"
427+
" directory: error %d\n",
428+
status);
429+
return;
430+
}
415431

416432
status = kern_path(rec_dirname, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
417433
&rec_dir);
@@ -421,7 +437,7 @@ nfsd4_init_recdir(char *rec_dirname)
421437

422438
if (!status)
423439
rec_dir_init = 1;
424-
nfs4_reset_user(uid, gid);
440+
nfs4_reset_creds(original_cred);
425441
}
426442

427443
void

fs/nfsd/nfsfh.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,14 @@ static __be32 nfsd_set_fh_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp)
186186
* access control settings being in effect, we cannot
187187
* fix that case easily.
188188
*/
189-
current->cred->cap_effective =
190-
cap_raise_nfsd_set(current->cred->cap_effective,
191-
current->cred->cap_permitted);
189+
struct cred *new = prepare_creds();
190+
if (!new)
191+
return nfserrno(-ENOMEM);
192+
new->cap_effective =
193+
cap_raise_nfsd_set(new->cap_effective,
194+
new->cap_permitted);
195+
put_cred(override_creds(new));
196+
put_cred(new);
192197
} else {
193198
error = nfsd_setuser_and_check_port(rqstp, exp);
194199
if (error)

0 commit comments

Comments
 (0)