PageRenderTime 31ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/security/selinux/hooks.c

https://github.com/mstsirkin/kvm
C | 2016 lines | 1520 code | 274 blank | 222 comment | 230 complexity | 44e054126ed876f220f2dbef27daf498 MD5 | raw file
  1. /*
  2. * NSA Security-Enhanced Linux (SELinux) security module
  3. *
  4. * This file contains the SELinux hook function implementations.
  5. *
  6. * Authors: Stephen Smalley, <sds@epoch.ncsc.mil>
  7. * Chris Vance, <cvance@nai.com>
  8. * Wayne Salamon, <wsalamon@nai.com>
  9. * James Morris <jmorris@redhat.com>
  10. *
  11. * Copyright (C) 2001,2002 Networks Associates Technology, Inc.
  12. * Copyright (C) 2003-2008 Red Hat, Inc., James Morris <jmorris@redhat.com>
  13. * Eric Paris <eparis@redhat.com>
  14. * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
  15. * <dgoeddel@trustedcs.com>
  16. * Copyright (C) 2006, 2007, 2009 Hewlett-Packard Development Company, L.P.
  17. * Paul Moore <paul@paul-moore.com>
  18. * Copyright (C) 2007 Hitachi Software Engineering Co., Ltd.
  19. * Yuichi Nakamura <ynakam@hitachisoft.jp>
  20. *
  21. * This program is free software; you can redistribute it and/or modify
  22. * it under the terms of the GNU General Public License version 2,
  23. * as published by the Free Software Foundation.
  24. */
  25. #include <linux/init.h>
  26. #include <linux/kd.h>
  27. #include <linux/kernel.h>
  28. #include <linux/tracehook.h>
  29. #include <linux/errno.h>
  30. #include <linux/ext2_fs.h>
  31. #include <linux/sched.h>
  32. #include <linux/security.h>
  33. #include <linux/xattr.h>
  34. #include <linux/capability.h>
  35. #include <linux/unistd.h>
  36. #include <linux/mm.h>
  37. #include <linux/mman.h>
  38. #include <linux/slab.h>
  39. #include <linux/pagemap.h>
  40. #include <linux/proc_fs.h>
  41. #include <linux/swap.h>
  42. #include <linux/spinlock.h>
  43. #include <linux/syscalls.h>
  44. #include <linux/dcache.h>
  45. #include <linux/file.h>
  46. #include <linux/fdtable.h>
  47. #include <linux/namei.h>
  48. #include <linux/mount.h>
  49. #include <linux/netfilter_ipv4.h>
  50. #include <linux/netfilter_ipv6.h>
  51. #include <linux/tty.h>
  52. #include <net/icmp.h>
  53. #include <net/ip.h> /* for local_port_range[] */
  54. #include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */
  55. #include <net/net_namespace.h>
  56. #include <net/netlabel.h>
  57. #include <linux/uaccess.h>
  58. #include <asm/ioctls.h>
  59. #include <linux/atomic.h>
  60. #include <linux/bitops.h>
  61. #include <linux/interrupt.h>
  62. #include <linux/netdevice.h> /* for network interface checks */
  63. #include <linux/netlink.h>
  64. #include <linux/tcp.h>
  65. #include <linux/udp.h>
  66. #include <linux/dccp.h>
  67. #include <linux/quota.h>
  68. #include <linux/un.h> /* for Unix socket types */
  69. #include <net/af_unix.h> /* for Unix socket types */
  70. #include <linux/parser.h>
  71. #include <linux/nfs_mount.h>
  72. #include <net/ipv6.h>
  73. #include <linux/hugetlb.h>
  74. #include <linux/personality.h>
  75. #include <linux/audit.h>
  76. #include <linux/string.h>
  77. #include <linux/selinux.h>
  78. #include <linux/mutex.h>
  79. #include <linux/posix-timers.h>
  80. #include <linux/syslog.h>
  81. #include <linux/user_namespace.h>
  82. #include "avc.h"
  83. #include "objsec.h"
  84. #include "netif.h"
  85. #include "netnode.h"
  86. #include "netport.h"
  87. #include "xfrm.h"
  88. #include "netlabel.h"
  89. #include "audit.h"
  90. #define NUM_SEL_MNT_OPTS 5
  91. extern int selinux_nlmsg_lookup(u16 sclass, u16 nlmsg_type, u32 *perm);
  92. extern struct security_operations *security_ops;
  93. /* SECMARK reference count */
  94. atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
  95. #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
  96. int selinux_enforcing;
  97. static int __init enforcing_setup(char *str)
  98. {
  99. unsigned long enforcing;
  100. if (!strict_strtoul(str, 0, &enforcing))
  101. selinux_enforcing = enforcing ? 1 : 0;
  102. return 1;
  103. }
  104. __setup("enforcing=", enforcing_setup);
  105. #endif
  106. #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
  107. int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE;
  108. static int __init selinux_enabled_setup(char *str)
  109. {
  110. unsigned long enabled;
  111. if (!strict_strtoul(str, 0, &enabled))
  112. selinux_enabled = enabled ? 1 : 0;
  113. return 1;
  114. }
  115. __setup("selinux=", selinux_enabled_setup);
  116. #else
  117. int selinux_enabled = 1;
  118. #endif
  119. static struct kmem_cache *sel_inode_cache;
  120. /**
  121. * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
  122. *
  123. * Description:
  124. * This function checks the SECMARK reference counter to see if any SECMARK
  125. * targets are currently configured, if the reference counter is greater than
  126. * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is
  127. * enabled, false (0) if SECMARK is disabled.
  128. *
  129. */
  130. static int selinux_secmark_enabled(void)
  131. {
  132. return (atomic_read(&selinux_secmark_refcount) > 0);
  133. }
  134. /*
  135. * initialise the security for the init task
  136. */
  137. static void cred_init_security(void)
  138. {
  139. struct cred *cred = (struct cred *) current->real_cred;
  140. struct task_security_struct *tsec;
  141. tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL);
  142. if (!tsec)
  143. panic("SELinux: Failed to initialize initial task.\n");
  144. tsec->osid = tsec->sid = SECINITSID_KERNEL;
  145. cred->security = tsec;
  146. }
  147. /*
  148. * get the security ID of a set of credentials
  149. */
  150. static inline u32 cred_sid(const struct cred *cred)
  151. {
  152. const struct task_security_struct *tsec;
  153. tsec = cred->security;
  154. return tsec->sid;
  155. }
  156. /*
  157. * get the objective security ID of a task
  158. */
  159. static inline u32 task_sid(const struct task_struct *task)
  160. {
  161. u32 sid;
  162. rcu_read_lock();
  163. sid = cred_sid(__task_cred(task));
  164. rcu_read_unlock();
  165. return sid;
  166. }
  167. /*
  168. * get the subjective security ID of the current task
  169. */
  170. static inline u32 current_sid(void)
  171. {
  172. const struct task_security_struct *tsec = current_security();
  173. return tsec->sid;
  174. }
  175. /* Allocate and free functions for each kind of security blob. */
  176. static int inode_alloc_security(struct inode *inode)
  177. {
  178. struct inode_security_struct *isec;
  179. u32 sid = current_sid();
  180. isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS);
  181. if (!isec)
  182. return -ENOMEM;
  183. mutex_init(&isec->lock);
  184. INIT_LIST_HEAD(&isec->list);
  185. isec->inode = inode;
  186. isec->sid = SECINITSID_UNLABELED;
  187. isec->sclass = SECCLASS_FILE;
  188. isec->task_sid = sid;
  189. inode->i_security = isec;
  190. return 0;
  191. }
  192. static void inode_free_security(struct inode *inode)
  193. {
  194. struct inode_security_struct *isec = inode->i_security;
  195. struct superblock_security_struct *sbsec = inode->i_sb->s_security;
  196. spin_lock(&sbsec->isec_lock);
  197. if (!list_empty(&isec->list))
  198. list_del_init(&isec->list);
  199. spin_unlock(&sbsec->isec_lock);
  200. inode->i_security = NULL;
  201. kmem_cache_free(sel_inode_cache, isec);
  202. }
  203. static int file_alloc_security(struct file *file)
  204. {
  205. struct file_security_struct *fsec;
  206. u32 sid = current_sid();
  207. fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL);
  208. if (!fsec)
  209. return -ENOMEM;
  210. fsec->sid = sid;
  211. fsec->fown_sid = sid;
  212. file->f_security = fsec;
  213. return 0;
  214. }
  215. static void file_free_security(struct file *file)
  216. {
  217. struct file_security_struct *fsec = file->f_security;
  218. file->f_security = NULL;
  219. kfree(fsec);
  220. }
  221. static int superblock_alloc_security(struct super_block *sb)
  222. {
  223. struct superblock_security_struct *sbsec;
  224. sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
  225. if (!sbsec)
  226. return -ENOMEM;
  227. mutex_init(&sbsec->lock);
  228. INIT_LIST_HEAD(&sbsec->isec_head);
  229. spin_lock_init(&sbsec->isec_lock);
  230. sbsec->sb = sb;
  231. sbsec->sid = SECINITSID_UNLABELED;
  232. sbsec->def_sid = SECINITSID_FILE;
  233. sbsec->mntpoint_sid = SECINITSID_UNLABELED;
  234. sb->s_security = sbsec;
  235. return 0;
  236. }
  237. static void superblock_free_security(struct super_block *sb)
  238. {
  239. struct superblock_security_struct *sbsec = sb->s_security;
  240. sb->s_security = NULL;
  241. kfree(sbsec);
  242. }
  243. /* The security server must be initialized before
  244. any labeling or access decisions can be provided. */
  245. extern int ss_initialized;
  246. /* The file system's label must be initialized prior to use. */
  247. static const char *labeling_behaviors[6] = {
  248. "uses xattr",
  249. "uses transition SIDs",
  250. "uses task SIDs",
  251. "uses genfs_contexts",
  252. "not configured for labeling",
  253. "uses mountpoint labeling",
  254. };
  255. static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
  256. static inline int inode_doinit(struct inode *inode)
  257. {
  258. return inode_doinit_with_dentry(inode, NULL);
  259. }
  260. enum {
  261. Opt_error = -1,
  262. Opt_context = 1,
  263. Opt_fscontext = 2,
  264. Opt_defcontext = 3,
  265. Opt_rootcontext = 4,
  266. Opt_labelsupport = 5,
  267. };
  268. static const match_table_t tokens = {
  269. {Opt_context, CONTEXT_STR "%s"},
  270. {Opt_fscontext, FSCONTEXT_STR "%s"},
  271. {Opt_defcontext, DEFCONTEXT_STR "%s"},
  272. {Opt_rootcontext, ROOTCONTEXT_STR "%s"},
  273. {Opt_labelsupport, LABELSUPP_STR},
  274. {Opt_error, NULL},
  275. };
  276. #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
  277. static int may_context_mount_sb_relabel(u32 sid,
  278. struct superblock_security_struct *sbsec,
  279. const struct cred *cred)
  280. {
  281. const struct task_security_struct *tsec = cred->security;
  282. int rc;
  283. rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
  284. FILESYSTEM__RELABELFROM, NULL);
  285. if (rc)
  286. return rc;
  287. rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM,
  288. FILESYSTEM__RELABELTO, NULL);
  289. return rc;
  290. }
  291. static int may_context_mount_inode_relabel(u32 sid,
  292. struct superblock_security_struct *sbsec,
  293. const struct cred *cred)
  294. {
  295. const struct task_security_struct *tsec = cred->security;
  296. int rc;
  297. rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
  298. FILESYSTEM__RELABELFROM, NULL);
  299. if (rc)
  300. return rc;
  301. rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM,
  302. FILESYSTEM__ASSOCIATE, NULL);
  303. return rc;
  304. }
  305. static int sb_finish_set_opts(struct super_block *sb)
  306. {
  307. struct superblock_security_struct *sbsec = sb->s_security;
  308. struct dentry *root = sb->s_root;
  309. struct inode *root_inode = root->d_inode;
  310. int rc = 0;
  311. if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
  312. /* Make sure that the xattr handler exists and that no
  313. error other than -ENODATA is returned by getxattr on
  314. the root directory. -ENODATA is ok, as this may be
  315. the first boot of the SELinux kernel before we have
  316. assigned xattr values to the filesystem. */
  317. if (!root_inode->i_op->getxattr) {
  318. printk(KERN_WARNING "SELinux: (dev %s, type %s) has no "
  319. "xattr support\n", sb->s_id, sb->s_type->name);
  320. rc = -EOPNOTSUPP;
  321. goto out;
  322. }
  323. rc = root_inode->i_op->getxattr(root, XATTR_NAME_SELINUX, NULL, 0);
  324. if (rc < 0 && rc != -ENODATA) {
  325. if (rc == -EOPNOTSUPP)
  326. printk(KERN_WARNING "SELinux: (dev %s, type "
  327. "%s) has no security xattr handler\n",
  328. sb->s_id, sb->s_type->name);
  329. else
  330. printk(KERN_WARNING "SELinux: (dev %s, type "
  331. "%s) getxattr errno %d\n", sb->s_id,
  332. sb->s_type->name, -rc);
  333. goto out;
  334. }
  335. }
  336. sbsec->flags |= (SE_SBINITIALIZED | SE_SBLABELSUPP);
  337. if (sbsec->behavior > ARRAY_SIZE(labeling_behaviors))
  338. printk(KERN_ERR "SELinux: initialized (dev %s, type %s), unknown behavior\n",
  339. sb->s_id, sb->s_type->name);
  340. else
  341. printk(KERN_DEBUG "SELinux: initialized (dev %s, type %s), %s\n",
  342. sb->s_id, sb->s_type->name,
  343. labeling_behaviors[sbsec->behavior-1]);
  344. if (sbsec->behavior == SECURITY_FS_USE_GENFS ||
  345. sbsec->behavior == SECURITY_FS_USE_MNTPOINT ||
  346. sbsec->behavior == SECURITY_FS_USE_NONE ||
  347. sbsec->behavior > ARRAY_SIZE(labeling_behaviors))
  348. sbsec->flags &= ~SE_SBLABELSUPP;
  349. /* Special handling for sysfs. Is genfs but also has setxattr handler*/
  350. if (strncmp(sb->s_type->name, "sysfs", sizeof("sysfs")) == 0)
  351. sbsec->flags |= SE_SBLABELSUPP;
  352. /* Initialize the root inode. */
  353. rc = inode_doinit_with_dentry(root_inode, root);
  354. /* Initialize any other inodes associated with the superblock, e.g.
  355. inodes created prior to initial policy load or inodes created
  356. during get_sb by a pseudo filesystem that directly
  357. populates itself. */
  358. spin_lock(&sbsec->isec_lock);
  359. next_inode:
  360. if (!list_empty(&sbsec->isec_head)) {
  361. struct inode_security_struct *isec =
  362. list_entry(sbsec->isec_head.next,
  363. struct inode_security_struct, list);
  364. struct inode *inode = isec->inode;
  365. spin_unlock(&sbsec->isec_lock);
  366. inode = igrab(inode);
  367. if (inode) {
  368. if (!IS_PRIVATE(inode))
  369. inode_doinit(inode);
  370. iput(inode);
  371. }
  372. spin_lock(&sbsec->isec_lock);
  373. list_del_init(&isec->list);
  374. goto next_inode;
  375. }
  376. spin_unlock(&sbsec->isec_lock);
  377. out:
  378. return rc;
  379. }
  380. /*
  381. * This function should allow an FS to ask what it's mount security
  382. * options were so it can use those later for submounts, displaying
  383. * mount options, or whatever.
  384. */
  385. static int selinux_get_mnt_opts(const struct super_block *sb,
  386. struct security_mnt_opts *opts)
  387. {
  388. int rc = 0, i;
  389. struct superblock_security_struct *sbsec = sb->s_security;
  390. char *context = NULL;
  391. u32 len;
  392. char tmp;
  393. security_init_mnt_opts(opts);
  394. if (!(sbsec->flags & SE_SBINITIALIZED))
  395. return -EINVAL;
  396. if (!ss_initialized)
  397. return -EINVAL;
  398. tmp = sbsec->flags & SE_MNTMASK;
  399. /* count the number of mount options for this sb */
  400. for (i = 0; i < 8; i++) {
  401. if (tmp & 0x01)
  402. opts->num_mnt_opts++;
  403. tmp >>= 1;
  404. }
  405. /* Check if the Label support flag is set */
  406. if (sbsec->flags & SE_SBLABELSUPP)
  407. opts->num_mnt_opts++;
  408. opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC);
  409. if (!opts->mnt_opts) {
  410. rc = -ENOMEM;
  411. goto out_free;
  412. }
  413. opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC);
  414. if (!opts->mnt_opts_flags) {
  415. rc = -ENOMEM;
  416. goto out_free;
  417. }
  418. i = 0;
  419. if (sbsec->flags & FSCONTEXT_MNT) {
  420. rc = security_sid_to_context(sbsec->sid, &context, &len);
  421. if (rc)
  422. goto out_free;
  423. opts->mnt_opts[i] = context;
  424. opts->mnt_opts_flags[i++] = FSCONTEXT_MNT;
  425. }
  426. if (sbsec->flags & CONTEXT_MNT) {
  427. rc = security_sid_to_context(sbsec->mntpoint_sid, &context, &len);
  428. if (rc)
  429. goto out_free;
  430. opts->mnt_opts[i] = context;
  431. opts->mnt_opts_flags[i++] = CONTEXT_MNT;
  432. }
  433. if (sbsec->flags & DEFCONTEXT_MNT) {
  434. rc = security_sid_to_context(sbsec->def_sid, &context, &len);
  435. if (rc)
  436. goto out_free;
  437. opts->mnt_opts[i] = context;
  438. opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT;
  439. }
  440. if (sbsec->flags & ROOTCONTEXT_MNT) {
  441. struct inode *root = sbsec->sb->s_root->d_inode;
  442. struct inode_security_struct *isec = root->i_security;
  443. rc = security_sid_to_context(isec->sid, &context, &len);
  444. if (rc)
  445. goto out_free;
  446. opts->mnt_opts[i] = context;
  447. opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT;
  448. }
  449. if (sbsec->flags & SE_SBLABELSUPP) {
  450. opts->mnt_opts[i] = NULL;
  451. opts->mnt_opts_flags[i++] = SE_SBLABELSUPP;
  452. }
  453. BUG_ON(i != opts->num_mnt_opts);
  454. return 0;
  455. out_free:
  456. security_free_mnt_opts(opts);
  457. return rc;
  458. }
  459. static int bad_option(struct superblock_security_struct *sbsec, char flag,
  460. u32 old_sid, u32 new_sid)
  461. {
  462. char mnt_flags = sbsec->flags & SE_MNTMASK;
  463. /* check if the old mount command had the same options */
  464. if (sbsec->flags & SE_SBINITIALIZED)
  465. if (!(sbsec->flags & flag) ||
  466. (old_sid != new_sid))
  467. return 1;
  468. /* check if we were passed the same options twice,
  469. * aka someone passed context=a,context=b
  470. */
  471. if (!(sbsec->flags & SE_SBINITIALIZED))
  472. if (mnt_flags & flag)
  473. return 1;
  474. return 0;
  475. }
  476. /*
  477. * Allow filesystems with binary mount data to explicitly set mount point
  478. * labeling information.
  479. */
  480. static int selinux_set_mnt_opts(struct super_block *sb,
  481. struct security_mnt_opts *opts)
  482. {
  483. const struct cred *cred = current_cred();
  484. int rc = 0, i;
  485. struct superblock_security_struct *sbsec = sb->s_security;
  486. const char *name = sb->s_type->name;
  487. struct inode *inode = sbsec->sb->s_root->d_inode;
  488. struct inode_security_struct *root_isec = inode->i_security;
  489. u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
  490. u32 defcontext_sid = 0;
  491. char **mount_options = opts->mnt_opts;
  492. int *flags = opts->mnt_opts_flags;
  493. int num_opts = opts->num_mnt_opts;
  494. mutex_lock(&sbsec->lock);
  495. if (!ss_initialized) {
  496. if (!num_opts) {
  497. /* Defer initialization until selinux_complete_init,
  498. after the initial policy is loaded and the security
  499. server is ready to handle calls. */
  500. goto out;
  501. }
  502. rc = -EINVAL;
  503. printk(KERN_WARNING "SELinux: Unable to set superblock options "
  504. "before the security server is initialized\n");
  505. goto out;
  506. }
  507. /*
  508. * Binary mount data FS will come through this function twice. Once
  509. * from an explicit call and once from the generic calls from the vfs.
  510. * Since the generic VFS calls will not contain any security mount data
  511. * we need to skip the double mount verification.
  512. *
  513. * This does open a hole in which we will not notice if the first
  514. * mount using this sb set explict options and a second mount using
  515. * this sb does not set any security options. (The first options
  516. * will be used for both mounts)
  517. */
  518. if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
  519. && (num_opts == 0))
  520. goto out;
  521. /*
  522. * parse the mount options, check if they are valid sids.
  523. * also check if someone is trying to mount the same sb more
  524. * than once with different security options.
  525. */
  526. for (i = 0; i < num_opts; i++) {
  527. u32 sid;
  528. if (flags[i] == SE_SBLABELSUPP)
  529. continue;
  530. rc = security_context_to_sid(mount_options[i],
  531. strlen(mount_options[i]), &sid);
  532. if (rc) {
  533. printk(KERN_WARNING "SELinux: security_context_to_sid"
  534. "(%s) failed for (dev %s, type %s) errno=%d\n",
  535. mount_options[i], sb->s_id, name, rc);
  536. goto out;
  537. }
  538. switch (flags[i]) {
  539. case FSCONTEXT_MNT:
  540. fscontext_sid = sid;
  541. if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
  542. fscontext_sid))
  543. goto out_double_mount;
  544. sbsec->flags |= FSCONTEXT_MNT;
  545. break;
  546. case CONTEXT_MNT:
  547. context_sid = sid;
  548. if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
  549. context_sid))
  550. goto out_double_mount;
  551. sbsec->flags |= CONTEXT_MNT;
  552. break;
  553. case ROOTCONTEXT_MNT:
  554. rootcontext_sid = sid;
  555. if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
  556. rootcontext_sid))
  557. goto out_double_mount;
  558. sbsec->flags |= ROOTCONTEXT_MNT;
  559. break;
  560. case DEFCONTEXT_MNT:
  561. defcontext_sid = sid;
  562. if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
  563. defcontext_sid))
  564. goto out_double_mount;
  565. sbsec->flags |= DEFCONTEXT_MNT;
  566. break;
  567. default:
  568. rc = -EINVAL;
  569. goto out;
  570. }
  571. }
  572. if (sbsec->flags & SE_SBINITIALIZED) {
  573. /* previously mounted with options, but not on this attempt? */
  574. if ((sbsec->flags & SE_MNTMASK) && !num_opts)
  575. goto out_double_mount;
  576. rc = 0;
  577. goto out;
  578. }
  579. if (strcmp(sb->s_type->name, "proc") == 0)
  580. sbsec->flags |= SE_SBPROC;
  581. /* Determine the labeling behavior to use for this filesystem type. */
  582. rc = security_fs_use((sbsec->flags & SE_SBPROC) ? "proc" : sb->s_type->name, &sbsec->behavior, &sbsec->sid);
  583. if (rc) {
  584. printk(KERN_WARNING "%s: security_fs_use(%s) returned %d\n",
  585. __func__, sb->s_type->name, rc);
  586. goto out;
  587. }
  588. /* sets the context of the superblock for the fs being mounted. */
  589. if (fscontext_sid) {
  590. rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
  591. if (rc)
  592. goto out;
  593. sbsec->sid = fscontext_sid;
  594. }
  595. /*
  596. * Switch to using mount point labeling behavior.
  597. * sets the label used on all file below the mountpoint, and will set
  598. * the superblock context if not already set.
  599. */
  600. if (context_sid) {
  601. if (!fscontext_sid) {
  602. rc = may_context_mount_sb_relabel(context_sid, sbsec,
  603. cred);
  604. if (rc)
  605. goto out;
  606. sbsec->sid = context_sid;
  607. } else {
  608. rc = may_context_mount_inode_relabel(context_sid, sbsec,
  609. cred);
  610. if (rc)
  611. goto out;
  612. }
  613. if (!rootcontext_sid)
  614. rootcontext_sid = context_sid;
  615. sbsec->mntpoint_sid = context_sid;
  616. sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
  617. }
  618. if (rootcontext_sid) {
  619. rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
  620. cred);
  621. if (rc)
  622. goto out;
  623. root_isec->sid = rootcontext_sid;
  624. root_isec->initialized = 1;
  625. }
  626. if (defcontext_sid) {
  627. if (sbsec->behavior != SECURITY_FS_USE_XATTR) {
  628. rc = -EINVAL;
  629. printk(KERN_WARNING "SELinux: defcontext option is "
  630. "invalid for this filesystem type\n");
  631. goto out;
  632. }
  633. if (defcontext_sid != sbsec->def_sid) {
  634. rc = may_context_mount_inode_relabel(defcontext_sid,
  635. sbsec, cred);
  636. if (rc)
  637. goto out;
  638. }
  639. sbsec->def_sid = defcontext_sid;
  640. }
  641. rc = sb_finish_set_opts(sb);
  642. out:
  643. mutex_unlock(&sbsec->lock);
  644. return rc;
  645. out_double_mount:
  646. rc = -EINVAL;
  647. printk(KERN_WARNING "SELinux: mount invalid. Same superblock, different "
  648. "security settings for (dev %s, type %s)\n", sb->s_id, name);
  649. goto out;
  650. }
  651. static void selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
  652. struct super_block *newsb)
  653. {
  654. const struct superblock_security_struct *oldsbsec = oldsb->s_security;
  655. struct superblock_security_struct *newsbsec = newsb->s_security;
  656. int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT);
  657. int set_context = (oldsbsec->flags & CONTEXT_MNT);
  658. int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT);
  659. /*
  660. * if the parent was able to be mounted it clearly had no special lsm
  661. * mount options. thus we can safely deal with this superblock later
  662. */
  663. if (!ss_initialized)
  664. return;
  665. /* how can we clone if the old one wasn't set up?? */
  666. BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
  667. /* if fs is reusing a sb, just let its options stand... */
  668. if (newsbsec->flags & SE_SBINITIALIZED)
  669. return;
  670. mutex_lock(&newsbsec->lock);
  671. newsbsec->flags = oldsbsec->flags;
  672. newsbsec->sid = oldsbsec->sid;
  673. newsbsec->def_sid = oldsbsec->def_sid;
  674. newsbsec->behavior = oldsbsec->behavior;
  675. if (set_context) {
  676. u32 sid = oldsbsec->mntpoint_sid;
  677. if (!set_fscontext)
  678. newsbsec->sid = sid;
  679. if (!set_rootcontext) {
  680. struct inode *newinode = newsb->s_root->d_inode;
  681. struct inode_security_struct *newisec = newinode->i_security;
  682. newisec->sid = sid;
  683. }
  684. newsbsec->mntpoint_sid = sid;
  685. }
  686. if (set_rootcontext) {
  687. const struct inode *oldinode = oldsb->s_root->d_inode;
  688. const struct inode_security_struct *oldisec = oldinode->i_security;
  689. struct inode *newinode = newsb->s_root->d_inode;
  690. struct inode_security_struct *newisec = newinode->i_security;
  691. newisec->sid = oldisec->sid;
  692. }
  693. sb_finish_set_opts(newsb);
  694. mutex_unlock(&newsbsec->lock);
  695. }
  696. static int selinux_parse_opts_str(char *options,
  697. struct security_mnt_opts *opts)
  698. {
  699. char *p;
  700. char *context = NULL, *defcontext = NULL;
  701. char *fscontext = NULL, *rootcontext = NULL;
  702. int rc, num_mnt_opts = 0;
  703. opts->num_mnt_opts = 0;
  704. /* Standard string-based options. */
  705. while ((p = strsep(&options, "|")) != NULL) {
  706. int token;
  707. substring_t args[MAX_OPT_ARGS];
  708. if (!*p)
  709. continue;
  710. token = match_token(p, tokens, args);
  711. switch (token) {
  712. case Opt_context:
  713. if (context || defcontext) {
  714. rc = -EINVAL;
  715. printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
  716. goto out_err;
  717. }
  718. context = match_strdup(&args[0]);
  719. if (!context) {
  720. rc = -ENOMEM;
  721. goto out_err;
  722. }
  723. break;
  724. case Opt_fscontext:
  725. if (fscontext) {
  726. rc = -EINVAL;
  727. printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
  728. goto out_err;
  729. }
  730. fscontext = match_strdup(&args[0]);
  731. if (!fscontext) {
  732. rc = -ENOMEM;
  733. goto out_err;
  734. }
  735. break;
  736. case Opt_rootcontext:
  737. if (rootcontext) {
  738. rc = -EINVAL;
  739. printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
  740. goto out_err;
  741. }
  742. rootcontext = match_strdup(&args[0]);
  743. if (!rootcontext) {
  744. rc = -ENOMEM;
  745. goto out_err;
  746. }
  747. break;
  748. case Opt_defcontext:
  749. if (context || defcontext) {
  750. rc = -EINVAL;
  751. printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
  752. goto out_err;
  753. }
  754. defcontext = match_strdup(&args[0]);
  755. if (!defcontext) {
  756. rc = -ENOMEM;
  757. goto out_err;
  758. }
  759. break;
  760. case Opt_labelsupport:
  761. break;
  762. default:
  763. rc = -EINVAL;
  764. printk(KERN_WARNING "SELinux: unknown mount option\n");
  765. goto out_err;
  766. }
  767. }
  768. rc = -ENOMEM;
  769. opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_ATOMIC);
  770. if (!opts->mnt_opts)
  771. goto out_err;
  772. opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int), GFP_ATOMIC);
  773. if (!opts->mnt_opts_flags) {
  774. kfree(opts->mnt_opts);
  775. goto out_err;
  776. }
  777. if (fscontext) {
  778. opts->mnt_opts[num_mnt_opts] = fscontext;
  779. opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT;
  780. }
  781. if (context) {
  782. opts->mnt_opts[num_mnt_opts] = context;
  783. opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT;
  784. }
  785. if (rootcontext) {
  786. opts->mnt_opts[num_mnt_opts] = rootcontext;
  787. opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT;
  788. }
  789. if (defcontext) {
  790. opts->mnt_opts[num_mnt_opts] = defcontext;
  791. opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT;
  792. }
  793. opts->num_mnt_opts = num_mnt_opts;
  794. return 0;
  795. out_err:
  796. kfree(context);
  797. kfree(defcontext);
  798. kfree(fscontext);
  799. kfree(rootcontext);
  800. return rc;
  801. }
  802. /*
  803. * string mount options parsing and call set the sbsec
  804. */
  805. static int superblock_doinit(struct super_block *sb, void *data)
  806. {
  807. int rc = 0;
  808. char *options = data;
  809. struct security_mnt_opts opts;
  810. security_init_mnt_opts(&opts);
  811. if (!data)
  812. goto out;
  813. BUG_ON(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA);
  814. rc = selinux_parse_opts_str(options, &opts);
  815. if (rc)
  816. goto out_err;
  817. out:
  818. rc = selinux_set_mnt_opts(sb, &opts);
  819. out_err:
  820. security_free_mnt_opts(&opts);
  821. return rc;
  822. }
  823. static void selinux_write_opts(struct seq_file *m,
  824. struct security_mnt_opts *opts)
  825. {
  826. int i;
  827. char *prefix;
  828. for (i = 0; i < opts->num_mnt_opts; i++) {
  829. char *has_comma;
  830. if (opts->mnt_opts[i])
  831. has_comma = strchr(opts->mnt_opts[i], ',');
  832. else
  833. has_comma = NULL;
  834. switch (opts->mnt_opts_flags[i]) {
  835. case CONTEXT_MNT:
  836. prefix = CONTEXT_STR;
  837. break;
  838. case FSCONTEXT_MNT:
  839. prefix = FSCONTEXT_STR;
  840. break;
  841. case ROOTCONTEXT_MNT:
  842. prefix = ROOTCONTEXT_STR;
  843. break;
  844. case DEFCONTEXT_MNT:
  845. prefix = DEFCONTEXT_STR;
  846. break;
  847. case SE_SBLABELSUPP:
  848. seq_putc(m, ',');
  849. seq_puts(m, LABELSUPP_STR);
  850. continue;
  851. default:
  852. BUG();
  853. return;
  854. };
  855. /* we need a comma before each option */
  856. seq_putc(m, ',');
  857. seq_puts(m, prefix);
  858. if (has_comma)
  859. seq_putc(m, '\"');
  860. seq_puts(m, opts->mnt_opts[i]);
  861. if (has_comma)
  862. seq_putc(m, '\"');
  863. }
  864. }
  865. static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
  866. {
  867. struct security_mnt_opts opts;
  868. int rc;
  869. rc = selinux_get_mnt_opts(sb, &opts);
  870. if (rc) {
  871. /* before policy load we may get EINVAL, don't show anything */
  872. if (rc == -EINVAL)
  873. rc = 0;
  874. return rc;
  875. }
  876. selinux_write_opts(m, &opts);
  877. security_free_mnt_opts(&opts);
  878. return rc;
  879. }
  880. static inline u16 inode_mode_to_security_class(umode_t mode)
  881. {
  882. switch (mode & S_IFMT) {
  883. case S_IFSOCK:
  884. return SECCLASS_SOCK_FILE;
  885. case S_IFLNK:
  886. return SECCLASS_LNK_FILE;
  887. case S_IFREG:
  888. return SECCLASS_FILE;
  889. case S_IFBLK:
  890. return SECCLASS_BLK_FILE;
  891. case S_IFDIR:
  892. return SECCLASS_DIR;
  893. case S_IFCHR:
  894. return SECCLASS_CHR_FILE;
  895. case S_IFIFO:
  896. return SECCLASS_FIFO_FILE;
  897. }
  898. return SECCLASS_FILE;
  899. }
  900. static inline int default_protocol_stream(int protocol)
  901. {
  902. return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
  903. }
  904. static inline int default_protocol_dgram(int protocol)
  905. {
  906. return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
  907. }
  908. static inline u16 socket_type_to_security_class(int family, int type, int protocol)
  909. {
  910. switch (family) {
  911. case PF_UNIX:
  912. switch (type) {
  913. case SOCK_STREAM:
  914. case SOCK_SEQPACKET:
  915. return SECCLASS_UNIX_STREAM_SOCKET;
  916. case SOCK_DGRAM:
  917. return SECCLASS_UNIX_DGRAM_SOCKET;
  918. }
  919. break;
  920. case PF_INET:
  921. case PF_INET6:
  922. switch (type) {
  923. case SOCK_STREAM:
  924. if (default_protocol_stream(protocol))
  925. return SECCLASS_TCP_SOCKET;
  926. else
  927. return SECCLASS_RAWIP_SOCKET;
  928. case SOCK_DGRAM:
  929. if (default_protocol_dgram(protocol))
  930. return SECCLASS_UDP_SOCKET;
  931. else
  932. return SECCLASS_RAWIP_SOCKET;
  933. case SOCK_DCCP:
  934. return SECCLASS_DCCP_SOCKET;
  935. default:
  936. return SECCLASS_RAWIP_SOCKET;
  937. }
  938. break;
  939. case PF_NETLINK:
  940. switch (protocol) {
  941. case NETLINK_ROUTE:
  942. return SECCLASS_NETLINK_ROUTE_SOCKET;
  943. case NETLINK_FIREWALL:
  944. return SECCLASS_NETLINK_FIREWALL_SOCKET;
  945. case NETLINK_INET_DIAG:
  946. return SECCLASS_NETLINK_TCPDIAG_SOCKET;
  947. case NETLINK_NFLOG:
  948. return SECCLASS_NETLINK_NFLOG_SOCKET;
  949. case NETLINK_XFRM:
  950. return SECCLASS_NETLINK_XFRM_SOCKET;
  951. case NETLINK_SELINUX:
  952. return SECCLASS_NETLINK_SELINUX_SOCKET;
  953. case NETLINK_AUDIT:
  954. return SECCLASS_NETLINK_AUDIT_SOCKET;
  955. case NETLINK_IP6_FW:
  956. return SECCLASS_NETLINK_IP6FW_SOCKET;
  957. case NETLINK_DNRTMSG:
  958. return SECCLASS_NETLINK_DNRT_SOCKET;
  959. case NETLINK_KOBJECT_UEVENT:
  960. return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
  961. default:
  962. return SECCLASS_NETLINK_SOCKET;
  963. }
  964. case PF_PACKET:
  965. return SECCLASS_PACKET_SOCKET;
  966. case PF_KEY:
  967. return SECCLASS_KEY_SOCKET;
  968. case PF_APPLETALK:
  969. return SECCLASS_APPLETALK_SOCKET;
  970. }
  971. return SECCLASS_SOCKET;
  972. }
  973. #ifdef CONFIG_PROC_FS
  974. static int selinux_proc_get_sid(struct dentry *dentry,
  975. u16 tclass,
  976. u32 *sid)
  977. {
  978. int rc;
  979. char *buffer, *path;
  980. buffer = (char *)__get_free_page(GFP_KERNEL);
  981. if (!buffer)
  982. return -ENOMEM;
  983. path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
  984. if (IS_ERR(path))
  985. rc = PTR_ERR(path);
  986. else {
  987. /* each process gets a /proc/PID/ entry. Strip off the
  988. * PID part to get a valid selinux labeling.
  989. * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
  990. while (path[1] >= '0' && path[1] <= '9') {
  991. path[1] = '/';
  992. path++;
  993. }
  994. rc = security_genfs_sid("proc", path, tclass, sid);
  995. }
  996. free_page((unsigned long)buffer);
  997. return rc;
  998. }
  999. #else
  1000. static int selinux_proc_get_sid(struct dentry *dentry,
  1001. u16 tclass,
  1002. u32 *sid)
  1003. {
  1004. return -EINVAL;
  1005. }
  1006. #endif
  1007. /* The inode's security attributes must be initialized before first use. */
  1008. static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
  1009. {
  1010. struct superblock_security_struct *sbsec = NULL;
  1011. struct inode_security_struct *isec = inode->i_security;
  1012. u32 sid;
  1013. struct dentry *dentry;
  1014. #define INITCONTEXTLEN 255
  1015. char *context = NULL;
  1016. unsigned len = 0;
  1017. int rc = 0;
  1018. if (isec->initialized)
  1019. goto out;
  1020. mutex_lock(&isec->lock);
  1021. if (isec->initialized)
  1022. goto out_unlock;
  1023. sbsec = inode->i_sb->s_security;
  1024. if (!(sbsec->flags & SE_SBINITIALIZED)) {
  1025. /* Defer initialization until selinux_complete_init,
  1026. after the initial policy is loaded and the security
  1027. server is ready to handle calls. */
  1028. spin_lock(&sbsec->isec_lock);
  1029. if (list_empty(&isec->list))
  1030. list_add(&isec->list, &sbsec->isec_head);
  1031. spin_unlock(&sbsec->isec_lock);
  1032. goto out_unlock;
  1033. }
  1034. switch (sbsec->behavior) {
  1035. case SECURITY_FS_USE_XATTR:
  1036. if (!inode->i_op->getxattr) {
  1037. isec->sid = sbsec->def_sid;
  1038. break;
  1039. }
  1040. /* Need a dentry, since the xattr API requires one.
  1041. Life would be simpler if we could just pass the inode. */
  1042. if (opt_dentry) {
  1043. /* Called from d_instantiate or d_splice_alias. */
  1044. dentry = dget(opt_dentry);
  1045. } else {
  1046. /* Called from selinux_complete_init, try to find a dentry. */
  1047. dentry = d_find_alias(inode);
  1048. }
  1049. if (!dentry) {
  1050. /*
  1051. * this is can be hit on boot when a file is accessed
  1052. * before the policy is loaded. When we load policy we
  1053. * may find inodes that have no dentry on the
  1054. * sbsec->isec_head list. No reason to complain as these
  1055. * will get fixed up the next time we go through
  1056. * inode_doinit with a dentry, before these inodes could
  1057. * be used again by userspace.
  1058. */
  1059. goto out_unlock;
  1060. }
  1061. len = INITCONTEXTLEN;
  1062. context = kmalloc(len+1, GFP_NOFS);
  1063. if (!context) {
  1064. rc = -ENOMEM;
  1065. dput(dentry);
  1066. goto out_unlock;
  1067. }
  1068. context[len] = '\0';
  1069. rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
  1070. context, len);
  1071. if (rc == -ERANGE) {
  1072. kfree(context);
  1073. /* Need a larger buffer. Query for the right size. */
  1074. rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
  1075. NULL, 0);
  1076. if (rc < 0) {
  1077. dput(dentry);
  1078. goto out_unlock;
  1079. }
  1080. len = rc;
  1081. context = kmalloc(len+1, GFP_NOFS);
  1082. if (!context) {
  1083. rc = -ENOMEM;
  1084. dput(dentry);
  1085. goto out_unlock;
  1086. }
  1087. context[len] = '\0';
  1088. rc = inode->i_op->getxattr(dentry,
  1089. XATTR_NAME_SELINUX,
  1090. context, len);
  1091. }
  1092. dput(dentry);
  1093. if (rc < 0) {
  1094. if (rc != -ENODATA) {
  1095. printk(KERN_WARNING "SELinux: %s: getxattr returned "
  1096. "%d for dev=%s ino=%ld\n", __func__,
  1097. -rc, inode->i_sb->s_id, inode->i_ino);
  1098. kfree(context);
  1099. goto out_unlock;
  1100. }
  1101. /* Map ENODATA to the default file SID */
  1102. sid = sbsec->def_sid;
  1103. rc = 0;
  1104. } else {
  1105. rc = security_context_to_sid_default(context, rc, &sid,
  1106. sbsec->def_sid,
  1107. GFP_NOFS);
  1108. if (rc) {
  1109. char *dev = inode->i_sb->s_id;
  1110. unsigned long ino = inode->i_ino;
  1111. if (rc == -EINVAL) {
  1112. if (printk_ratelimit())
  1113. printk(KERN_NOTICE "SELinux: inode=%lu on dev=%s was found to have an invalid "
  1114. "context=%s. This indicates you may need to relabel the inode or the "
  1115. "filesystem in question.\n", ino, dev, context);
  1116. } else {
  1117. printk(KERN_WARNING "SELinux: %s: context_to_sid(%s) "
  1118. "returned %d for dev=%s ino=%ld\n",
  1119. __func__, context, -rc, dev, ino);
  1120. }
  1121. kfree(context);
  1122. /* Leave with the unlabeled SID */
  1123. rc = 0;
  1124. break;
  1125. }
  1126. }
  1127. kfree(context);
  1128. isec->sid = sid;
  1129. break;
  1130. case SECURITY_FS_USE_TASK:
  1131. isec->sid = isec->task_sid;
  1132. break;
  1133. case SECURITY_FS_USE_TRANS:
  1134. /* Default to the fs SID. */
  1135. isec->sid = sbsec->sid;
  1136. /* Try to obtain a transition SID. */
  1137. isec->sclass = inode_mode_to_security_class(inode->i_mode);
  1138. rc = security_transition_sid(isec->task_sid, sbsec->sid,
  1139. isec->sclass, NULL, &sid);
  1140. if (rc)
  1141. goto out_unlock;
  1142. isec->sid = sid;
  1143. break;
  1144. case SECURITY_FS_USE_MNTPOINT:
  1145. isec->sid = sbsec->mntpoint_sid;
  1146. break;
  1147. default:
  1148. /* Default to the fs superblock SID. */
  1149. isec->sid = sbsec->sid;
  1150. if ((sbsec->flags & SE_SBPROC) && !S_ISLNK(inode->i_mode)) {
  1151. if (opt_dentry) {
  1152. isec->sclass = inode_mode_to_security_class(inode->i_mode);
  1153. rc = selinux_proc_get_sid(opt_dentry,
  1154. isec->sclass,
  1155. &sid);
  1156. if (rc)
  1157. goto out_unlock;
  1158. isec->sid = sid;
  1159. }
  1160. }
  1161. break;
  1162. }
  1163. isec->initialized = 1;
  1164. out_unlock:
  1165. mutex_unlock(&isec->lock);
  1166. out:
  1167. if (isec->sclass == SECCLASS_FILE)
  1168. isec->sclass = inode_mode_to_security_class(inode->i_mode);
  1169. return rc;
  1170. }
  1171. /* Convert a Linux signal to an access vector. */
  1172. static inline u32 signal_to_av(int sig)
  1173. {
  1174. u32 perm = 0;
  1175. switch (sig) {
  1176. case SIGCHLD:
  1177. /* Commonly granted from child to parent. */
  1178. perm = PROCESS__SIGCHLD;
  1179. break;
  1180. case SIGKILL:
  1181. /* Cannot be caught or ignored */
  1182. perm = PROCESS__SIGKILL;
  1183. break;
  1184. case SIGSTOP:
  1185. /* Cannot be caught or ignored */
  1186. perm = PROCESS__SIGSTOP;
  1187. break;
  1188. default:
  1189. /* All other signals. */
  1190. perm = PROCESS__SIGNAL;
  1191. break;
  1192. }
  1193. return perm;
  1194. }
  1195. /*
  1196. * Check permission between a pair of credentials
  1197. * fork check, ptrace check, etc.
  1198. */
  1199. static int cred_has_perm(const struct cred *actor,
  1200. const struct cred *target,
  1201. u32 perms)
  1202. {
  1203. u32 asid = cred_sid(actor), tsid = cred_sid(target);
  1204. return avc_has_perm(asid, tsid, SECCLASS_PROCESS, perms, NULL);
  1205. }
  1206. /*
  1207. * Check permission between a pair of tasks, e.g. signal checks,
  1208. * fork check, ptrace check, etc.
  1209. * tsk1 is the actor and tsk2 is the target
  1210. * - this uses the default subjective creds of tsk1
  1211. */
  1212. static int task_has_perm(const struct task_struct *tsk1,
  1213. const struct task_struct *tsk2,
  1214. u32 perms)
  1215. {
  1216. const struct task_security_struct *__tsec1, *__tsec2;
  1217. u32 sid1, sid2;
  1218. rcu_read_lock();
  1219. __tsec1 = __task_cred(tsk1)->security; sid1 = __tsec1->sid;
  1220. __tsec2 = __task_cred(tsk2)->security; sid2 = __tsec2->sid;
  1221. rcu_read_unlock();
  1222. return avc_has_perm(sid1, sid2, SECCLASS_PROCESS, perms, NULL);
  1223. }
  1224. /*
  1225. * Check permission between current and another task, e.g. signal checks,
  1226. * fork check, ptrace check, etc.
  1227. * current is the actor and tsk2 is the target
  1228. * - this uses current's subjective creds
  1229. */
  1230. static int current_has_perm(const struct task_struct *tsk,
  1231. u32 perms)
  1232. {
  1233. u32 sid, tsid;
  1234. sid = current_sid();
  1235. tsid = task_sid(tsk);
  1236. return avc_has_perm(sid, tsid, SECCLASS_PROCESS, perms, NULL);
  1237. }
  1238. #if CAP_LAST_CAP > 63
  1239. #error Fix SELinux to handle capabilities > 63.
  1240. #endif
  1241. /* Check whether a task is allowed to use a capability. */
  1242. static int task_has_capability(struct task_struct *tsk,
  1243. const struct cred *cred,
  1244. int cap, int audit)
  1245. {
  1246. struct common_audit_data ad;
  1247. struct av_decision avd;
  1248. u16 sclass;
  1249. u32 sid = cred_sid(cred);
  1250. u32 av = CAP_TO_MASK(cap);
  1251. int rc;
  1252. COMMON_AUDIT_DATA_INIT(&ad, CAP);
  1253. ad.tsk = tsk;
  1254. ad.u.cap = cap;
  1255. switch (CAP_TO_INDEX(cap)) {
  1256. case 0:
  1257. sclass = SECCLASS_CAPABILITY;
  1258. break;
  1259. case 1:
  1260. sclass = SECCLASS_CAPABILITY2;
  1261. break;
  1262. default:
  1263. printk(KERN_ERR
  1264. "SELinux: out of range capability %d\n", cap);
  1265. BUG();
  1266. return -EINVAL;
  1267. }
  1268. rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd);
  1269. if (audit == SECURITY_CAP_AUDIT) {
  1270. int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad, 0);
  1271. if (rc2)
  1272. return rc2;
  1273. }
  1274. return rc;
  1275. }
  1276. /* Check whether a task is allowed to use a system operation. */
  1277. static int task_has_system(struct task_struct *tsk,
  1278. u32 perms)
  1279. {
  1280. u32 sid = task_sid(tsk);
  1281. return avc_has_perm(sid, SECINITSID_KERNEL,
  1282. SECCLASS_SYSTEM, perms, NULL);
  1283. }
  1284. /* Check whether a task has a particular permission to an inode.
  1285. The 'adp' parameter is optional and allows other audit
  1286. data to be passed (e.g. the dentry). */
  1287. static int inode_has_perm(const struct cred *cred,
  1288. struct inode *inode,
  1289. u32 perms,
  1290. struct common_audit_data *adp,
  1291. unsigned flags)
  1292. {
  1293. struct inode_security_struct *isec;
  1294. u32 sid;
  1295. validate_creds(cred);
  1296. if (unlikely(IS_PRIVATE(inode)))
  1297. return 0;
  1298. sid = cred_sid(cred);
  1299. isec = inode->i_security;
  1300. return avc_has_perm_flags(sid, isec->sid, isec->sclass, perms, adp, flags);
  1301. }
  1302. static int inode_has_perm_noadp(const struct cred *cred,
  1303. struct inode *inode,
  1304. u32 perms,
  1305. unsigned flags)
  1306. {
  1307. struct common_audit_data ad;
  1308. COMMON_AUDIT_DATA_INIT(&ad, INODE);
  1309. ad.u.inode = inode;
  1310. return inode_has_perm(cred, inode, perms, &ad, flags);
  1311. }
  1312. /* Same as inode_has_perm, but pass explicit audit data containing
  1313. the dentry to help the auditing code to more easily generate the
  1314. pathname if needed. */
  1315. static inline int dentry_has_perm(const struct cred *cred,
  1316. struct dentry *dentry,
  1317. u32 av)
  1318. {
  1319. struct inode *inode = dentry->d_inode;
  1320. struct common_audit_data ad;
  1321. COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
  1322. ad.u.dentry = dentry;
  1323. return inode_has_perm(cred, inode, av, &ad, 0);
  1324. }
  1325. /* Same as inode_has_perm, but pass explicit audit data containing
  1326. the path to help the auditing code to more easily generate the
  1327. pathname if needed. */
  1328. static inline int path_has_perm(const struct cred *cred,
  1329. struct path *path,
  1330. u32 av)
  1331. {
  1332. struct inode *inode = path->dentry->d_inode;
  1333. struct common_audit_data ad;
  1334. COMMON_AUDIT_DATA_INIT(&ad, PATH);
  1335. ad.u.path = *path;
  1336. return inode_has_perm(cred, inode, av, &ad, 0);
  1337. }
  1338. /* Check whether a task can use an open file descriptor to
  1339. access an inode in a given way. Check access to the
  1340. descriptor itself, and then use dentry_has_perm to
  1341. check a particular permission to the file.
  1342. Access to the descriptor is implicitly granted if it
  1343. has the same SID as the process. If av is zero, then
  1344. access to the file is not checked, e.g. for cases
  1345. where only the descriptor is affected like seek. */
  1346. static int file_has_perm(const struct cred *cred,
  1347. struct file *file,
  1348. u32 av)
  1349. {
  1350. struct file_security_struct *fsec = file->f_security;
  1351. struct inode *inode = file->f_path.dentry->d_inode;
  1352. struct common_audit_data ad;
  1353. u32 sid = cred_sid(cred);
  1354. int rc;
  1355. COMMON_AUDIT_DATA_INIT(&ad, PATH);
  1356. ad.u.path = file->f_path;
  1357. if (sid != fsec->sid) {
  1358. rc = avc_has_perm(sid, fsec->sid,
  1359. SECCLASS_FD,
  1360. FD__USE,
  1361. &ad);
  1362. if (rc)
  1363. goto out;
  1364. }
  1365. /* av is zero if only checking access to the descriptor. */
  1366. rc = 0;
  1367. if (av)
  1368. rc = inode_has_perm(cred, inode, av, &ad, 0);
  1369. out:
  1370. return rc;
  1371. }
  1372. /* Check whether a task can create a file. */
  1373. static int may_create(struct inode *dir,
  1374. struct dentry *dentry,
  1375. u16 tclass)
  1376. {
  1377. const struct task_security_struct *tsec = current_security();
  1378. struct inode_security_struct *dsec;
  1379. struct superblock_security_struct *sbsec;
  1380. u32 sid, newsid;
  1381. struct common_audit_data ad;
  1382. int rc;
  1383. dsec = dir->i_security;
  1384. sbsec = dir->i_sb->s_security;
  1385. sid = tsec->sid;
  1386. newsid = tsec->create_sid;
  1387. COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
  1388. ad.u.dentry = dentry;
  1389. rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR,
  1390. DIR__ADD_NAME | DIR__SEARCH,
  1391. &ad);
  1392. if (rc)
  1393. return rc;
  1394. if (!newsid || !(sbsec->flags & SE_SBLABELSUPP)) {
  1395. rc = security_transition_sid(sid, dsec->sid, tclass,
  1396. &dentry->d_name, &newsid);
  1397. if (rc)
  1398. return rc;
  1399. }
  1400. rc = avc_has_perm(sid, newsid, tclass, FILE__CREATE, &ad);
  1401. if (rc)
  1402. return rc;
  1403. return avc_has_perm(newsid, sbsec->sid,
  1404. SECCLASS_FILESYSTEM,
  1405. FILESYSTEM__ASSOCIATE, &ad);
  1406. }
  1407. /* Check whether a task can create a key. */
  1408. static int may_create_key(u32 ksid,
  1409. struct task_struct *ctx)
  1410. {
  1411. u32 sid = task_sid(ctx);
  1412. return avc_has_perm(sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL);
  1413. }
  1414. #define MAY_LINK 0
  1415. #define MAY_UNLINK 1
  1416. #define MAY_RMDIR 2
  1417. /* Check whether a task can link, unlink, or rmdir a file/directory. */
  1418. static int may_link(struct inode *dir,
  1419. struct dentry *dentry,
  1420. int kind)
  1421. {
  1422. struct inode_security_struct *dsec, *isec;
  1423. struct common_audit_data ad;
  1424. u32 sid = current_sid();
  1425. u32 av;
  1426. int rc;
  1427. dsec = dir->i_security;
  1428. isec = dentry->d_inode->i_security;
  1429. COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
  1430. ad.u.dentry = dentry;
  1431. av = DIR__SEARCH;
  1432. av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
  1433. rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, av, &ad);
  1434. if (rc)
  1435. return rc;
  1436. switch (kind) {
  1437. case MAY_LINK:
  1438. av = FILE__LINK;
  1439. break;
  1440. case MAY_UNLINK:
  1441. av = FILE__UNLINK;
  1442. break;
  1443. case MAY_RMDIR:
  1444. av = DIR__RMDIR;
  1445. break;
  1446. default:
  1447. printk(KERN_WARNING "SELinux: %s: unrecognized kind %d\n",
  1448. __func__, kind);
  1449. return 0;
  1450. }
  1451. rc = avc_has_perm(sid, isec->sid, isec->sclass, av, &ad);
  1452. return rc;
  1453. }
  1454. static inline int may_rename(struct inode *old_dir,
  1455. struct dentry *old_dentry,
  1456. struct inode *new_dir,
  1457. struct dentry *new_dentry)
  1458. {
  1459. struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
  1460. struct common_audit_data ad;
  1461. u32 sid = current_sid();
  1462. u32 av;
  1463. int old_is_dir, new_is_dir;
  1464. int rc;
  1465. old_dsec = old_dir->i_security;
  1466. old_isec = old_dentry->d_inode->i_security;
  1467. old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
  1468. new_dsec = new_dir->i_security;
  1469. COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
  1470. ad.u.dentry = old_dentry;
  1471. rc = avc_has_perm(sid, old_dsec->sid, SECCLASS_DIR,
  1472. DIR__REMOVE_NAME | DIR__SEARCH, &ad);
  1473. if (rc)
  1474. return rc;
  1475. rc = avc_has_perm(sid, old_isec->sid,
  1476. old_isec->sclass, FILE__RENAME, &ad);
  1477. if (rc)
  1478. return rc;
  1479. if (old_is_dir && new_dir != old_dir) {
  1480. rc = avc_has_perm(sid, old_isec->sid,
  1481. old_isec->sclass, DIR__REPARENT, &ad);
  1482. if (rc)
  1483. return rc;
  1484. }
  1485. ad.u.dentry = new_dentry;
  1486. av = DIR__ADD_NAME | DIR__SEARCH;
  1487. if (new_dentry->d_inode)
  1488. av |= DIR__REMOVE_NAME;
  1489. rc = avc_has_perm(sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
  1490. if (rc)
  1491. return rc;
  1492. if (new_dentry->d_inode) {
  1493. new_isec = new_dentry->d_inode->i_security;
  1494. new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode);
  1495. rc = avc_has_perm(sid, new_isec->sid,
  1496. new_isec->sclass,
  1497. (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
  1498. if (rc)
  1499. return rc;
  1500. }
  1501. return 0;
  1502. }
  1503. /* Check whether a task can perform a filesystem operation. */
  1504. static int superblock_has_perm(const struct cred *cred,
  1505. struct super_block *sb,
  1506. u32 perms,
  1507. struct common_audit_data *ad)
  1508. {
  1509. struct superblock_security_struct *sbsec;
  1510. u32 sid = cred_sid(cred);
  1511. sbsec = sb->s_security;
  1512. return avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
  1513. }
  1514. /* Convert a Linux mode and permission mask to an access vector. */
  1515. static inline u32 file_mask_to_av(int mode, int mask)
  1516. {
  1517. u32 av = 0;
  1518. if ((mode & S_IFMT) != S_IFDIR) {
  1519. if (mask & MAY_EXEC)
  1520. av |= FILE__EXECUTE;
  1521. if (mask & MAY_READ)
  1522. av |= FILE__READ;
  1523. if (mask & MAY_APPEND)
  1524. av |= FILE__APPEND;
  1525. else if (mask & MAY_WRITE)
  1526. av |= FILE__WRITE;
  1527. } else {
  1528. if (mask & MAY_EXEC)
  1529. av |= DIR__SEARCH;
  1530. if (mask & MAY_WRITE)
  1531. av |= DIR__WRITE;
  1532. if (mask & MAY_READ)
  1533. av |= DIR__READ;
  1534. }
  1535. return av;
  1536. }
  1537. /* Convert a Linux file to an access vector. */
  1538. static inline u32 file_to_av(struct file *file)
  1539. {
  1540. u32 av = 0;
  1541. if (file->f_mode & FMODE_READ)
  1542. av |= FILE__READ;
  1543. if (file->f_mode & FMODE_WRITE) {
  1544. if (file->f_flags & O_APPEND)
  1545. av |= FILE__APPEND;
  1546. else
  1547. av |= FILE__WRITE;
  1548. }
  1549. if (!av) {
  1550. /*
  1551. * Special file opened with flags 3 for ioctl-only use.
  1552. */
  1553. av = FILE__IOCTL;
  1554. }
  1555. return av;
  1556. }
  1557. /*
  1558. * Convert a file to an access vector and include the correct open
  1559. * open permission.
  1560. */
  1561. static inline u32 open_file_to_av(struct file *file)
  1562. {
  1563. u32 av = file_to_av(file);
  1564. if (selinux_policycap_openperm)
  1565. av |= FILE__OPEN;
  1566. return av;
  1567. }
  1568. /* Hook functions begin here. */
  1569. static int selinux_ptrace_access_check(struct task_struct *child,
  1570. unsigned int mode)
  1571. {
  1572. int rc;
  1573. rc = cap_ptrace_access_check(child, mode);
  1574. if (rc)
  1575. return rc;
  1576. if (mode == PTRACE_MODE_READ) {
  1577. u32 sid = current_sid();
  1578. u32 csid = task_sid(child);
  1579. return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ, NULL);
  1580. }
  1581. return current_has_perm(child, PROCESS__PTRACE);
  1582. }
  1583. static int selinux_ptrace_traceme(struct task_struct *parent)
  1584. {
  1585. int rc;
  1586. rc = cap_ptrace_traceme(parent);
  1587. if (rc)
  1588. return rc;
  1589. return task_has_perm(parent, current, PROCESS__PTRACE);
  1590. }
  1591. static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
  1592. kernel_cap_t *inheritable, kernel_cap_t *permitted)
  1593. {
  1594. int error;
  1595. error = current_has_perm(target, PROCESS__GETCAP);
  1596. if (error)
  1597. return error;
  1598. return cap_capget(target, effective, inheritable, permitted);
  1599. }
  1600. static int selinux_capset(struct cred *new, const struct cred *old,
  1601. const kernel_cap_t *effective,
  1602. const kernel_cap_t *inheritable,
  1603. const kernel_cap_t *permitted)
  1604. {
  1605. int error;
  1606. error = cap_capset(new, old,
  1607. effective, inheritable, permitted);
  1608. if (error)
  1609. return error;
  1610. return cred_has_perm(old, new, PROCESS__SETCAP);
  1611. }
  1612. /*
  1613. * (This comment used to live with the selinux_task_setuid hook,
  1614. * which was removed).
  1615. *
  1616. * Since setuid only affects the current process, and since the SELinux
  1617. * controls are not based on the Linux identity attributes, SELinux does not
  1618. * need to control this operation. However, SELinux does control the use of
  1619. * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
  1620. */
  1621. static int selinux_capable(struct task_struct *tsk, const struct cred *cred,
  1622. struct user_namespace *ns, int cap, int audit)
  1623. {
  1624. int rc;
  1625. rc = cap_capable(tsk, cred, ns, cap, audit);
  1626. if (rc)
  1627. return rc;
  1628. return task_has_capability(tsk, cred, cap, audit);
  1629. }
  1630. static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
  1631. {
  1632. const struct cred *cred = current_cred();
  1633. int rc = 0;
  1634. if (!sb)
  1635. return 0;
  1636. switch (cmds) {
  1637. case Q_SYNC:
  1638. case Q_QUOTAON:
  1639. case Q_QUOTAOFF:
  1640. case Q_SETINFO:
  1641. case Q_SETQUOTA:
  1642. rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
  1643. break;
  1644. case Q_GETFMT:
  1645. case Q_GETINFO:
  1646. case Q_GETQUOTA:
  1647. rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
  1648. break;
  1649. default:
  1650. rc = 0; /* let the kernel handle invalid cmds */
  1651. break;
  1652. }
  1653. return rc;
  1654. }
  1655. static int selinux_quota_on(struct dentry *dentry)
  1656. {
  1657. const struct cred *cred = current_cred();
  1658. return dentry_has_perm(cred, dentry, FILE__QUOTAON);
  1659. }
  1660. static int selinux_syslog(int type)
  1661. {
  1662. int rc;
  1663. switch (type) {
  1664. case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
  1665. case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
  1666. rc = task_has_system(current, SYSTEM__SYSLOG_READ);
  1667. break;
  1668. case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
  1669. case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
  1670. /* Set level of messages printed to console */
  1671. case SYSLOG_ACTION_CONSOLE_LEVEL:
  1672. rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE);
  1673. break;
  1674. case SYSLOG_ACTION_CLOSE: /* Close log */
  1675. case SYSLOG_ACTION_OPEN: /* Open log */
  1676. case SYSLOG_ACTION_READ: /* Read from log */
  1677. case SYSLOG_ACTION_READ_CLEAR: /* Read/clear last kernel messages */
  1678. case SYSLOG_ACTION_CLEAR: /* Clear ring buffer */
  1679. default:
  1680. rc = task_has_system(current, SYSTEM__SYSLOG_MOD);
  1681. break;
  1682. }
  1683. return rc;
  1684. }
  1685. /*
  1686. * Check that a process has enough memory to allocate a new virtual
  1687. * mapping. 0 means there is enough memory for the allocation to
  1688. * succeed and -ENOMEM implies there is not.
  1689. *
  1690. * Do not audit the selinux permission check, as this is applied to all
  1691. * processes that allocate mappings.
  1692. */
  1693. static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
  1694. {
  1695. int rc, cap_sys_admin = 0;
  1696. rc = selinux_capable(current, current_cred(),
  1697. &init_user_ns, CAP_SYS_ADMIN,
  1698. SECURITY_CAP_NOAUDIT);
  1699. if (rc == 0)
  1700. cap_sys_admin = 1;
  1701. return __vm_enough_memory(mm, pages, cap_sys_admin);
  1702. }
  1703. /* binprm security operations */
  1704. static int selinux_bprm_set_creds(struct linux_binprm *bprm)
  1705. {
  1706. const struct task_security_struct *old_tsec;
  1707. struct task_security_struct *new_tsec;
  1708. struct inode_security_struct *isec;
  1709. struct common_audit_data ad;
  1710. struct inode *inode = bprm->file->f_path.dentry->d_inode;
  1711. int rc;
  1712. rc = cap_bprm_set_creds(bprm);
  1713. if (rc)
  1714. return rc;
  1715. /* SELinux context only depends on initial program or script and not
  1716. * the script interpreter */
  1717. if (bprm->cred_prepared)
  1718. return 0;
  1719. old_tsec = current_security();
  1720. new_tsec = bprm->cred->security;
  1721. isec = inode->i_security;
  1722. /* Default to the current task SID. */
  1723. new_tsec->sid = old_tsec->sid;
  1724. new_tsec->osid = old_tsec->sid;
  1725. /* Reset fs, key, and sock SIDs on execve. */
  1726. new_tsec->create_sid = 0;
  1727. new_tsec->keycreate_sid = 0;
  1728. new_tsec->sockcreate_sid = 0;
  1729. if (old_tsec->exec_sid) {
  1730. new_tsec->sid = old_tsec->exec_sid;
  1731. /* Reset exec SID on execve. */
  1732. new_tsec->exec_sid = 0;
  1733. } else {
  1734. /* Check for a default transition on this program. */
  1735. rc = security_transition_sid(old_tsec->sid, isec->sid,
  1736. SECCLASS_PROCESS, NULL,
  1737. &new_tsec->sid);
  1738. if (rc)
  1739. return rc;
  1740. }
  1741. COMMON_AUDIT_DATA_INIT(&ad, PATH);
  1742. ad.u.path = bprm->fi