aboutsummaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorSerge E. Hallyn <serge@hallyn.com>2011-03-23 19:43:17 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2011-03-23 22:47:02 -0400
commit3486740a4f32a6a466f5ac931654d154790ba648 (patch)
treeac5d968a66057fa84933b8f89fd3e916270dffed /include
parent59607db367c57f515183cb203642291bb14d9c40 (diff)
userns: security: make capabilities relative to the user namespace
- Introduce ns_capable to test for a capability in a non-default user namespace. - Teach cap_capable to handle capabilities in a non-default user namespace. The motivation is to get to the unprivileged creation of new namespaces. It looks like this gets us 90% of the way there, with only potential uid confusion issues left. I still need to handle getting all caps after creation but otherwise I think I have a good starter patch that achieves all of your goals. Changelog: 11/05/2010: [serge] add apparmor 12/14/2010: [serge] fix capabilities to created user namespaces Without this, if user serge creates a user_ns, he won't have capabilities to the user_ns he created. THis is because we were first checking whether his effective caps had the caps he needed and returning -EPERM if not, and THEN checking whether he was the creator. Reverse those checks. 12/16/2010: [serge] security_real_capable needs ns argument in !security case 01/11/2011: [serge] add task_ns_capable helper 01/11/2011: [serge] add nsown_capable() helper per Bastian Blank suggestion 02/16/2011: [serge] fix a logic bug: the root user is always creator of init_user_ns, but should not always have capabilities to it! Fix the check in cap_capable(). 02/21/2011: Add the required user_ns parameter to security_capable, fixing a compile failure. 02/23/2011: Convert some macros to functions as per akpm comments. Some couldn't be converted because we can't easily forward-declare them (they are inline if !SECURITY, extern if SECURITY). Add a current_user_ns function so we can use it in capability.h without #including cred.h. Move all forward declarations together to the top of the #ifdef __KERNEL__ section, and use kernel-doc format. 02/23/2011: Per dhowells, clean up comment in cap_capable(). 02/23/2011: Per akpm, remove unreachable 'return -EPERM' in cap_capable. (Original written and signed off by Eric; latest, modified version acked by him) [akpm@linux-foundation.org: fix build] [akpm@linux-foundation.org: export current_user_ns() for ecryptfs] [serge.hallyn@canonical.com: remove unneeded extra argument in selinux's task_has_capability] Signed-off-by: Eric W. Biederman <ebiederm@xmission.com> Signed-off-by: Serge E. Hallyn <serge.hallyn@canonical.com> Acked-by: "Eric W. Biederman" <ebiederm@xmission.com> Acked-by: Daniel Lezcano <daniel.lezcano@free.fr> Acked-by: David Howells <dhowells@redhat.com> Cc: James Morris <jmorris@namei.org> Signed-off-by: Serge E. Hallyn <serge.hallyn@canonical.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'include')
-rw-r--r--include/linux/capability.h36
-rw-r--r--include/linux/cred.h4
-rw-r--r--include/linux/security.h28
3 files changed, 48 insertions, 20 deletions
diff --git a/include/linux/capability.h b/include/linux/capability.h
index fb16a3699b99..7c9c82903012 100644
--- a/include/linux/capability.h
+++ b/include/linux/capability.h
@@ -368,6 +368,17 @@ struct cpu_vfs_cap_data {
368 368
369#ifdef __KERNEL__ 369#ifdef __KERNEL__
370 370
371struct dentry;
372struct user_namespace;
373
374extern struct user_namespace init_user_ns;
375
376struct user_namespace *current_user_ns(void);
377
378extern const kernel_cap_t __cap_empty_set;
379extern const kernel_cap_t __cap_full_set;
380extern const kernel_cap_t __cap_init_eff_set;
381
371/* 382/*
372 * Internal kernel functions only 383 * Internal kernel functions only
373 */ 384 */
@@ -530,10 +541,6 @@ static inline kernel_cap_t cap_raise_nfsd_set(const kernel_cap_t a,
530 cap_intersect(permitted, __cap_nfsd_set)); 541 cap_intersect(permitted, __cap_nfsd_set));
531} 542}
532 543
533extern const kernel_cap_t __cap_empty_set;
534extern const kernel_cap_t __cap_full_set;
535extern const kernel_cap_t __cap_init_eff_set;
536
537/** 544/**
538 * has_capability - Determine if a task has a superior capability available 545 * has_capability - Determine if a task has a superior capability available
539 * @t: The task in question 546 * @t: The task in question
@@ -544,7 +551,7 @@ extern const kernel_cap_t __cap_init_eff_set;
544 * 551 *
545 * Note that this does not set PF_SUPERPRIV on the task. 552 * Note that this does not set PF_SUPERPRIV on the task.
546 */ 553 */
547#define has_capability(t, cap) (security_real_capable((t), (cap)) == 0) 554#define has_capability(t, cap) (security_real_capable((t), &init_user_ns, (cap)) == 0)
548 555
549/** 556/**
550 * has_capability_noaudit - Determine if a task has a superior capability available (unaudited) 557 * has_capability_noaudit - Determine if a task has a superior capability available (unaudited)
@@ -558,12 +565,25 @@ extern const kernel_cap_t __cap_init_eff_set;
558 * Note that this does not set PF_SUPERPRIV on the task. 565 * Note that this does not set PF_SUPERPRIV on the task.
559 */ 566 */
560#define has_capability_noaudit(t, cap) \ 567#define has_capability_noaudit(t, cap) \
561 (security_real_capable_noaudit((t), (cap)) == 0) 568 (security_real_capable_noaudit((t), &init_user_ns, (cap)) == 0)
562 569
563extern int capable(int cap); 570extern bool capable(int cap);
571extern bool ns_capable(struct user_namespace *ns, int cap);
572extern bool task_ns_capable(struct task_struct *t, int cap);
573
574/**
575 * nsown_capable - Check superior capability to one's own user_ns
576 * @cap: The capability in question
577 *
578 * Return true if the current task has the given superior capability
579 * targeted at its own user namespace.
580 */
581static inline bool nsown_capable(int cap)
582{
583 return ns_capable(current_user_ns(), cap);
584}
564 585
565/* audit system wants to get cap info from files as well */ 586/* audit system wants to get cap info from files as well */
566struct dentry;
567extern int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps); 587extern int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps);
568 588
569#endif /* __KERNEL__ */ 589#endif /* __KERNEL__ */
diff --git a/include/linux/cred.h b/include/linux/cred.h
index 4aaeab376446..9aeeb0ba2003 100644
--- a/include/linux/cred.h
+++ b/include/linux/cred.h
@@ -354,9 +354,11 @@ static inline void put_cred(const struct cred *_cred)
354#define current_fsgid() (current_cred_xxx(fsgid)) 354#define current_fsgid() (current_cred_xxx(fsgid))
355#define current_cap() (current_cred_xxx(cap_effective)) 355#define current_cap() (current_cred_xxx(cap_effective))
356#define current_user() (current_cred_xxx(user)) 356#define current_user() (current_cred_xxx(user))
357#define current_user_ns() (current_cred_xxx(user)->user_ns) 357#define _current_user_ns() (current_cred_xxx(user)->user_ns)
358#define current_security() (current_cred_xxx(security)) 358#define current_security() (current_cred_xxx(security))
359 359
360extern struct user_namespace *current_user_ns(void);
361
360#define current_uid_gid(_uid, _gid) \ 362#define current_uid_gid(_uid, _gid) \
361do { \ 363do { \
362 const struct cred *__cred; \ 364 const struct cred *__cred; \
diff --git a/include/linux/security.h b/include/linux/security.h
index 56cac520d014..ca02f1716736 100644
--- a/include/linux/security.h
+++ b/include/linux/security.h
@@ -47,13 +47,14 @@
47 47
48struct ctl_table; 48struct ctl_table;
49struct audit_krule; 49struct audit_krule;
50struct user_namespace;
50 51
51/* 52/*
52 * These functions are in security/capability.c and are used 53 * These functions are in security/capability.c and are used
53 * as the default capabilities functions 54 * as the default capabilities functions
54 */ 55 */
55extern int cap_capable(struct task_struct *tsk, const struct cred *cred, 56extern int cap_capable(struct task_struct *tsk, const struct cred *cred,
56 int cap, int audit); 57 struct user_namespace *ns, int cap, int audit);
57extern int cap_settime(const struct timespec *ts, const struct timezone *tz); 58extern int cap_settime(const struct timespec *ts, const struct timezone *tz);
58extern int cap_ptrace_access_check(struct task_struct *child, unsigned int mode); 59extern int cap_ptrace_access_check(struct task_struct *child, unsigned int mode);
59extern int cap_ptrace_traceme(struct task_struct *parent); 60extern int cap_ptrace_traceme(struct task_struct *parent);
@@ -1262,6 +1263,7 @@ static inline void security_free_mnt_opts(struct security_mnt_opts *opts)
1262 * credentials. 1263 * credentials.
1263 * @tsk contains the task_struct for the process. 1264 * @tsk contains the task_struct for the process.
1264 * @cred contains the credentials to use. 1265 * @cred contains the credentials to use.
1266 * @ns contains the user namespace we want the capability in
1265 * @cap contains the capability <include/linux/capability.h>. 1267 * @cap contains the capability <include/linux/capability.h>.
1266 * @audit: Whether to write an audit message or not 1268 * @audit: Whether to write an audit message or not
1267 * Return 0 if the capability is granted for @tsk. 1269 * Return 0 if the capability is granted for @tsk.
@@ -1384,7 +1386,7 @@ struct security_operations {
1384 const kernel_cap_t *inheritable, 1386 const kernel_cap_t *inheritable,
1385 const kernel_cap_t *permitted); 1387 const kernel_cap_t *permitted);
1386 int (*capable) (struct task_struct *tsk, const struct cred *cred, 1388 int (*capable) (struct task_struct *tsk, const struct cred *cred,
1387 int cap, int audit); 1389 struct user_namespace *ns, int cap, int audit);
1388 int (*quotactl) (int cmds, int type, int id, struct super_block *sb); 1390 int (*quotactl) (int cmds, int type, int id, struct super_block *sb);
1389 int (*quota_on) (struct dentry *dentry); 1391 int (*quota_on) (struct dentry *dentry);
1390 int (*syslog) (int type); 1392 int (*syslog) (int type);
@@ -1665,9 +1667,12 @@ int security_capset(struct cred *new, const struct cred *old,
1665 const kernel_cap_t *effective, 1667 const kernel_cap_t *effective,
1666 const kernel_cap_t *inheritable, 1668 const kernel_cap_t *inheritable,
1667 const kernel_cap_t *permitted); 1669 const kernel_cap_t *permitted);
1668int security_capable(const struct cred *cred, int cap); 1670int security_capable(struct user_namespace *ns, const struct cred *cred,
1669int security_real_capable(struct task_struct *tsk, int cap); 1671 int cap);
1670int security_real_capable_noaudit(struct task_struct *tsk, int cap); 1672int security_real_capable(struct task_struct *tsk, struct user_namespace *ns,
1673 int cap);
1674int security_real_capable_noaudit(struct task_struct *tsk,
1675 struct user_namespace *ns, int cap);
1671int security_quotactl(int cmds, int type, int id, struct super_block *sb); 1676int security_quotactl(int cmds, int type, int id, struct super_block *sb);
1672int security_quota_on(struct dentry *dentry); 1677int security_quota_on(struct dentry *dentry);
1673int security_syslog(int type); 1678int security_syslog(int type);
@@ -1860,28 +1865,29 @@ static inline int security_capset(struct cred *new,
1860 return cap_capset(new, old, effective, inheritable, permitted); 1865 return cap_capset(new, old, effective, inheritable, permitted);
1861} 1866}
1862 1867
1863static inline int security_capable(const struct cred *cred, int cap) 1868static inline int security_capable(struct user_namespace *ns,
1869 const struct cred *cred, int cap)
1864{ 1870{
1865 return cap_capable(current, cred, cap, SECURITY_CAP_AUDIT); 1871 return cap_capable(current, cred, ns, cap, SECURITY_CAP_AUDIT);
1866} 1872}
1867 1873
1868static inline int security_real_capable(struct task_struct *tsk, int cap) 1874static inline int security_real_capable(struct task_struct *tsk, struct user_namespace *ns, int cap)
1869{ 1875{
1870 int ret; 1876 int ret;
1871 1877
1872 rcu_read_lock(); 1878 rcu_read_lock();
1873 ret = cap_capable(tsk, __task_cred(tsk), cap, SECURITY_CAP_AUDIT); 1879 ret = cap_capable(tsk, __task_cred(tsk), ns, cap, SECURITY_CAP_AUDIT);
1874 rcu_read_unlock(); 1880 rcu_read_unlock();
1875 return ret; 1881 return ret;
1876} 1882}
1877 1883
1878static inline 1884static inline
1879int security_real_capable_noaudit(struct task_struct *tsk, int cap) 1885int security_real_capable_noaudit(struct task_struct *tsk, struct user_namespace *ns, int cap)
1880{ 1886{
1881 int ret; 1887 int ret;
1882 1888
1883 rcu_read_lock(); 1889 rcu_read_lock();
1884 ret = cap_capable(tsk, __task_cred(tsk), cap, 1890 ret = cap_capable(tsk, __task_cred(tsk), ns, cap,
1885 SECURITY_CAP_NOAUDIT); 1891 SECURITY_CAP_NOAUDIT);
1886 rcu_read_unlock(); 1892 rcu_read_unlock();
1887 return ret; 1893 return ret;