Skip to content

Commit 078de5f

Browse files
committed
userns: Store uid and gid values in struct cred with kuid_t and kgid_t types
cred.h and a few trivial users of struct cred are changed. The rest of the users of struct cred are left for other patches as there are too many changes to make in one go and leave the change reviewable. If the user namespace is disabled and CONFIG_UIDGID_STRICT_TYPE_CHECKS are disabled the code will contiue to compile and behave correctly. Acked-by: Serge Hallyn <serge.hallyn@canonical.com> Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
1 parent ae2975b commit 078de5f

10 files changed

Lines changed: 59 additions & 62 deletions

File tree

‎arch/x86/mm/fault.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ show_fault_oops(struct pt_regs *regs, unsigned long error_code,
582582
pte_t *pte = lookup_address(address, &level);
583583

584584
if (pte && pte_present(*pte) && !pte_exec(*pte))
585-
printk(nx_warning, current_uid());
585+
printk(nx_warning, from_kuid(&init_user_ns, current_uid()));
586586
}
587587

588588
printk(KERN_ALERT "BUG: unable to handle kernel ");

‎fs/ioprio.c‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,7 @@ SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio)
123123
break;
124124

125125
do_each_thread(g, p) {
126-
const struct cred *tcred = __task_cred(p);
127-
kuid_t tcred_uid = make_kuid(tcred->user_ns, tcred->uid);
128-
if (!uid_eq(tcred_uid, uid))
126+
if (!uid_eq(task_uid(p), uid))
129127
continue;
130128
ret = set_task_ioprio(p, ioprio);
131129
if (ret)
@@ -220,9 +218,7 @@ SYSCALL_DEFINE2(ioprio_get, int, which, int, who)
220218
break;
221219

222220
do_each_thread(g, p) {
223-
const struct cred *tcred = __task_cred(p);
224-
kuid_t tcred_uid = make_kuid(tcred->user_ns, tcred->uid);
225-
if (!uid_eq(tcred_uid, user->uid))
221+
if (!uid_eq(task_uid(p), user->uid))
226222
continue;
227223
tmpio = get_task_ioprio(p);
228224
if (tmpio < 0)

‎include/linux/cred.h‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,14 @@ struct cred {
123123
#define CRED_MAGIC 0x43736564
124124
#define CRED_MAGIC_DEAD 0x44656144
125125
#endif
126-
uid_t uid; /* real UID of the task */
127-
gid_t gid; /* real GID of the task */
128-
uid_t suid; /* saved UID of the task */
129-
gid_t sgid; /* saved GID of the task */
130-
uid_t euid; /* effective UID of the task */
131-
gid_t egid; /* effective GID of the task */
132-
uid_t fsuid; /* UID for VFS ops */
133-
gid_t fsgid; /* GID for VFS ops */
126+
kuid_t uid; /* real UID of the task */
127+
kgid_t gid; /* real GID of the task */
128+
kuid_t suid; /* saved UID of the task */
129+
kgid_t sgid; /* saved GID of the task */
130+
kuid_t euid; /* effective UID of the task */
131+
kgid_t egid; /* effective GID of the task */
132+
kuid_t fsuid; /* UID for VFS ops */
133+
kgid_t fsgid; /* GID for VFS ops */
134134
unsigned securebits; /* SUID-less security management */
135135
kernel_cap_t cap_inheritable; /* caps our children can inherit */
136136
kernel_cap_t cap_permitted; /* caps we're permitted */

‎include/linux/user_namespace.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ static inline void put_user_ns(struct user_namespace *ns)
7070
#endif
7171

7272
static inline uid_t user_ns_map_uid(struct user_namespace *to,
73-
const struct cred *cred, uid_t uid)
73+
const struct cred *cred, kuid_t uid)
7474
{
75-
return from_kuid_munged(to, make_kuid(cred->user_ns, uid));
75+
return from_kuid_munged(to, uid);
7676
}
7777

7878
static inline gid_t user_ns_map_gid(struct user_namespace *to,
79-
const struct cred *cred, gid_t gid)
79+
const struct cred *cred, kgid_t gid)
8080
{
81-
return from_kgid_munged(to, make_kgid(cred->user_ns, gid));
81+
return from_kgid_munged(to, gid);
8282
}
8383

8484
#endif /* _LINUX_USER_H */

‎kernel/cred.c‎

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ struct cred init_cred = {
4949
.subscribers = ATOMIC_INIT(2),
5050
.magic = CRED_MAGIC,
5151
#endif
52+
.uid = GLOBAL_ROOT_UID,
53+
.gid = GLOBAL_ROOT_GID,
54+
.suid = GLOBAL_ROOT_UID,
55+
.sgid = GLOBAL_ROOT_GID,
56+
.euid = GLOBAL_ROOT_UID,
57+
.egid = GLOBAL_ROOT_GID,
58+
.fsuid = GLOBAL_ROOT_UID,
59+
.fsgid = GLOBAL_ROOT_GID,
5260
.securebits = SECUREBITS_DEFAULT,
5361
.cap_inheritable = CAP_EMPTY_SET,
5462
.cap_permitted = CAP_FULL_SET,
@@ -488,10 +496,10 @@ int commit_creds(struct cred *new)
488496
get_cred(new); /* we will require a ref for the subj creds too */
489497

490498
/* dumpability changes */
491-
if (old->euid != new->euid ||
492-
old->egid != new->egid ||
493-
old->fsuid != new->fsuid ||
494-
old->fsgid != new->fsgid ||
499+
if (!uid_eq(old->euid, new->euid) ||
500+
!gid_eq(old->egid, new->egid) ||
501+
!uid_eq(old->fsuid, new->fsuid) ||
502+
!gid_eq(old->fsgid, new->fsgid) ||
495503
!cap_issubset(new->cap_permitted, old->cap_permitted)) {
496504
if (task->mm)
497505
set_dumpable(task->mm, suid_dumpable);
@@ -500,9 +508,9 @@ int commit_creds(struct cred *new)
500508
}
501509

502510
/* alter the thread keyring */
503-
if (new->fsuid != old->fsuid)
511+
if (!uid_eq(new->fsuid, old->fsuid))
504512
key_fsuid_changed(task);
505-
if (new->fsgid != old->fsgid)
513+
if (!gid_eq(new->fsgid, old->fsgid))
506514
key_fsgid_changed(task);
507515

508516
/* do it
@@ -519,16 +527,16 @@ int commit_creds(struct cred *new)
519527
alter_cred_subscribers(old, -2);
520528

521529
/* send notifications */
522-
if (new->uid != old->uid ||
523-
new->euid != old->euid ||
524-
new->suid != old->suid ||
525-
new->fsuid != old->fsuid)
530+
if (!uid_eq(new->uid, old->uid) ||
531+
!uid_eq(new->euid, old->euid) ||
532+
!uid_eq(new->suid, old->suid) ||
533+
!uid_eq(new->fsuid, old->fsuid))
526534
proc_id_connector(task, PROC_EVENT_UID);
527535

528-
if (new->gid != old->gid ||
529-
new->egid != old->egid ||
530-
new->sgid != old->sgid ||
531-
new->fsgid != old->fsgid)
536+
if (!gid_eq(new->gid, old->gid) ||
537+
!gid_eq(new->egid, old->egid) ||
538+
!gid_eq(new->sgid, old->sgid) ||
539+
!gid_eq(new->fsgid, old->fsgid))
532540
proc_id_connector(task, PROC_EVENT_GID);
533541

534542
/* release the old obj and subj refs both */

‎kernel/signal.c‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,8 +1038,10 @@ static inline void userns_fixup_signal_uid(struct siginfo *info, struct task_str
10381038
if (SI_FROMKERNEL(info))
10391039
return;
10401040

1041-
info->si_uid = user_ns_map_uid(task_cred_xxx(t, user_ns),
1042-
current_cred(), info->si_uid);
1041+
rcu_read_lock();
1042+
info->si_uid = from_kuid_munged(task_cred_xxx(t, user_ns),
1043+
make_kuid(current_user_ns(), info->si_uid));
1044+
rcu_read_unlock();
10431045
}
10441046
#else
10451047
static inline void userns_fixup_signal_uid(struct siginfo *info, struct task_struct *t)
@@ -1106,7 +1108,7 @@ static int __send_signal(int sig, struct siginfo *info, struct task_struct *t,
11061108
q->info.si_code = SI_USER;
11071109
q->info.si_pid = task_tgid_nr_ns(current,
11081110
task_active_pid_ns(t));
1109-
q->info.si_uid = current_uid();
1111+
q->info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
11101112
break;
11111113
case (unsigned long) SEND_SIG_PRIV:
11121114
q->info.si_signo = sig;
@@ -1973,7 +1975,7 @@ static void ptrace_do_notify(int signr, int exit_code, int why)
19731975
info.si_signo = signr;
19741976
info.si_code = exit_code;
19751977
info.si_pid = task_pid_vnr(current);
1976-
info.si_uid = current_uid();
1978+
info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
19771979

19781980
/* Let the debugger run. */
19791981
ptrace_stop(exit_code, why, 1, &info);
@@ -2828,7 +2830,7 @@ SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
28282830
info.si_errno = 0;
28292831
info.si_code = SI_USER;
28302832
info.si_pid = task_tgid_vnr(current);
2831-
info.si_uid = current_uid();
2833+
info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
28322834

28332835
return kill_something_info(sig, &info, pid);
28342836
}
@@ -2871,7 +2873,7 @@ static int do_tkill(pid_t tgid, pid_t pid, int sig)
28712873
info.si_errno = 0;
28722874
info.si_code = SI_TKILL;
28732875
info.si_pid = task_tgid_vnr(current);
2874-
info.si_uid = current_uid();
2876+
info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
28752877

28762878
return do_send_specific(tgid, pid, sig, &info);
28772879
}

‎kernel/sys.c‎

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ SYSCALL_DEFINE3(setpriority, int, which, int, who, int, niceval)
175175
const struct cred *cred = current_cred();
176176
int error = -EINVAL;
177177
struct pid *pgrp;
178-
kuid_t cred_uid;
179178
kuid_t uid;
180179

181180
if (which > PRIO_USER || which < PRIO_PROCESS)
@@ -209,22 +208,19 @@ SYSCALL_DEFINE3(setpriority, int, which, int, who, int, niceval)
209208
} while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
210209
break;
211210
case PRIO_USER:
212-
cred_uid = make_kuid(cred->user_ns, cred->uid);
213211
uid = make_kuid(cred->user_ns, who);
214212
user = cred->user;
215213
if (!who)
216-
uid = cred_uid;
217-
else if (!uid_eq(uid, cred_uid) &&
214+
uid = cred->uid;
215+
else if (!uid_eq(uid, cred->uid) &&
218216
!(user = find_user(uid)))
219217
goto out_unlock; /* No processes for this user */
220218

221219
do_each_thread(g, p) {
222-
const struct cred *tcred = __task_cred(p);
223-
kuid_t tcred_uid = make_kuid(tcred->user_ns, tcred->uid);
224-
if (uid_eq(tcred_uid, uid))
220+
if (uid_eq(task_uid(p), uid))
225221
error = set_one_prio(p, niceval, error);
226222
} while_each_thread(g, p);
227-
if (!uid_eq(uid, cred_uid))
223+
if (!uid_eq(uid, cred->uid))
228224
free_uid(user); /* For find_user() */
229225
break;
230226
}
@@ -248,7 +244,6 @@ SYSCALL_DEFINE2(getpriority, int, which, int, who)
248244
const struct cred *cred = current_cred();
249245
long niceval, retval = -ESRCH;
250246
struct pid *pgrp;
251-
kuid_t cred_uid;
252247
kuid_t uid;
253248

254249
if (which > PRIO_USER || which < PRIO_PROCESS)
@@ -280,25 +275,22 @@ SYSCALL_DEFINE2(getpriority, int, which, int, who)
280275
} while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
281276
break;
282277
case PRIO_USER:
283-
cred_uid = make_kuid(cred->user_ns, cred->uid);
284278
uid = make_kuid(cred->user_ns, who);
285279
user = cred->user;
286280
if (!who)
287-
uid = cred_uid;
288-
else if (!uid_eq(uid, cred_uid) &&
281+
uid = cred->uid;
282+
else if (!uid_eq(uid, cred->uid) &&
289283
!(user = find_user(uid)))
290284
goto out_unlock; /* No processes for this user */
291285

292286
do_each_thread(g, p) {
293-
const struct cred *tcred = __task_cred(p);
294-
kuid_t tcred_uid = make_kuid(tcred->user_ns, tcred->uid);
295-
if (uid_eq(tcred_uid, uid)) {
287+
if (uid_eq(task_uid(p), uid)) {
296288
niceval = 20 - task_nice(p);
297289
if (niceval > retval)
298290
retval = niceval;
299291
}
300292
} while_each_thread(g, p);
301-
if (!uid_eq(uid, cred_uid))
293+
if (!uid_eq(uid, cred->uid))
302294
free_uid(user); /* for find_user() */
303295
break;
304296
}
@@ -641,7 +633,7 @@ static int set_user(struct cred *new)
641633
{
642634
struct user_struct *new_user;
643635

644-
new_user = alloc_uid(make_kuid(new->user_ns, new->uid));
636+
new_user = alloc_uid(new->uid);
645637
if (!new_user)
646638
return -EAGAIN;
647639

‎kernel/user_namespace.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ static bool new_idmap_permitted(struct user_namespace *ns, int cap_setid,
3636
int create_user_ns(struct cred *new)
3737
{
3838
struct user_namespace *ns, *parent_ns = new->user_ns;
39-
kuid_t owner = make_kuid(new->user_ns, new->euid);
40-
kgid_t group = make_kgid(new->user_ns, new->egid);
39+
kuid_t owner = new->euid;
40+
kgid_t group = new->egid;
4141

4242
/* The creator needs a mapping in the parent user namespace
4343
* or else we won't be able to reasonably tell userspace who

‎mm/oom_kill.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,8 @@ static void dump_tasks(const struct mem_cgroup *memcg, const nodemask_t *nodemas
410410
}
411411

412412
pr_info("[%5d] %5d %5d %8lu %8lu %3u %3d %5d %s\n",
413-
task->pid, task_uid(task), task->tgid,
414-
task->mm->total_vm, get_mm_rss(task->mm),
413+
task->pid, from_kuid(&init_user_ns, task_uid(task)),
414+
task->tgid, task->mm->total_vm, get_mm_rss(task->mm),
415415
task_cpu(task), task->signal->oom_adj,
416416
task->signal->oom_score_adj, task->comm);
417417
task_unlock(task);

‎security/commoncap.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ int cap_capable(const struct cred *cred, struct user_namespace *targ_ns,
7777
{
7878
for (;;) {
7979
/* The owner of the user namespace has all caps. */
80-
if (targ_ns != &init_user_ns && uid_eq(targ_ns->owner,
81-
make_kuid(cred->user_ns, cred->euid)))
80+
if (targ_ns != &init_user_ns && uid_eq(targ_ns->owner, cred->euid))
8281
return 0;
8382

8483
/* Do we have the necessary capabilities? */

0 commit comments

Comments
 (0)