Skip to content

Commit f1c84da

Browse files
committed
capabilities: remove task_ns_* functions
task_ in the front of a function, in the security subsystem anyway, means to me at least, that we are operating with that task as the subject of the security decision. In this case what it means is that we are using current as the subject but we use the task to get the right namespace. Who in the world would ever realize that's what task_ns_capability means just by the name? This patch eliminates the task_ns functions entirely and uses the has_ns_capability function instead. This means we explicitly open code the ns in question in the caller. I think it makes the caller a LOT more clear what is going on. Signed-off-by: Eric Paris <eparis@redhat.com> Acked-by: Serge E. Hallyn <serge.hallyn@canonical.com>
1 parent d2a7009 commit f1c84da

5 files changed

Lines changed: 7 additions & 20 deletions

File tree

‎include/linux/capability.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,6 @@ extern bool has_ns_capability_noaudit(struct task_struct *t,
547547
struct user_namespace *ns, int cap);
548548
extern bool capable(int cap);
549549
extern bool ns_capable(struct user_namespace *ns, int cap);
550-
extern bool task_ns_capable(struct task_struct *t, int cap);
551550
extern bool nsown_capable(int cap);
552551

553552
/* audit system wants to get cap info from files as well */

‎include/linux/cred.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,10 +358,12 @@ static inline void put_cred(const struct cred *_cred)
358358
#define current_security() (current_cred_xxx(security))
359359

360360
#ifdef CONFIG_USER_NS
361-
#define current_user_ns() (current_cred_xxx(user_ns))
361+
#define current_user_ns() (current_cred_xxx(user_ns))
362+
#define task_user_ns(task) (task_cred_xxx((task), user_ns))
362363
#else
363364
extern struct user_namespace init_user_ns;
364-
#define current_user_ns() (&init_user_ns)
365+
#define current_user_ns() (&init_user_ns)
366+
#define task_user_ns(task) (&init_user_ns)
365367
#endif
366368

367369

‎kernel/capability.c‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -408,20 +408,6 @@ bool capable(int cap)
408408
}
409409
EXPORT_SYMBOL(capable);
410410

411-
/**
412-
* task_ns_capable - Determine whether current task has a superior
413-
* capability targeted at a specific task's user namespace.
414-
* @t: The task whose user namespace is targeted.
415-
* @cap: The capability in question.
416-
*
417-
* Return true if it does, false otherwise.
418-
*/
419-
bool task_ns_capable(struct task_struct *t, int cap)
420-
{
421-
return ns_capable(task_cred_xxx(t, user)->user_ns, cap);
422-
}
423-
EXPORT_SYMBOL(task_ns_capable);
424-
425411
/**
426412
* nsown_capable - Check superior capability to one's own user_ns
427413
* @cap: The capability in question

‎kernel/ptrace.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ int __ptrace_may_access(struct task_struct *task, unsigned int mode)
196196
smp_rmb();
197197
if (task->mm)
198198
dumpable = get_dumpable(task->mm);
199-
if (!dumpable && !task_ns_capable(task, CAP_SYS_PTRACE))
199+
if (!dumpable && !ns_capable(task_user_ns(task), CAP_SYS_PTRACE))
200200
return -EPERM;
201201

202202
return security_ptrace_access_check(task, mode);
@@ -266,7 +266,7 @@ static int ptrace_attach(struct task_struct *task, long request,
266266
task->ptrace = PT_PTRACED;
267267
if (seize)
268268
task->ptrace |= PT_SEIZED;
269-
if (task_ns_capable(task, CAP_SYS_PTRACE))
269+
if (ns_capable(task_user_ns(task), CAP_SYS_PTRACE))
270270
task->ptrace |= PT_PTRACE_CAP;
271271

272272
__ptrace_link(task, current);

‎kernel/sched.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5409,7 +5409,7 @@ long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
54095409
goto out_free_cpus_allowed;
54105410
}
54115411
retval = -EPERM;
5412-
if (!check_same_owner(p) && !task_ns_capable(p, CAP_SYS_NICE))
5412+
if (!check_same_owner(p) && !ns_capable(task_user_ns(p), CAP_SYS_NICE))
54135413
goto out_unlock;
54145414

54155415
retval = security_task_setscheduler(p);

0 commit comments

Comments
 (0)