PageRenderTime 63ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/security/selinux/hooks.c

https://gitlab.com/LiquidSmooth-Devices/android_kernel_htc_msm8974
C | 5579 lines | 4543 code | 1005 blank | 31 comment | 726 complexity | df3e1a3692524b5a2b543eb0165add05 MD5 | raw file
Possible License(s): GPL-2.0

Large files files are truncated, but you can click here to view the full 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/sched.h>
  31. #include <linux/security.h>
  32. #include <linux/xattr.h>
  33. #include <linux/capability.h>
  34. #include <linux/unistd.h>
  35. #include <linux/mm.h>
  36. #include <linux/mman.h>
  37. #include <linux/slab.h>
  38. #include <linux/pagemap.h>
  39. #include <linux/proc_fs.h>
  40. #include <linux/swap.h>
  41. #include <linux/spinlock.h>
  42. #include <linux/syscalls.h>
  43. #include <linux/dcache.h>
  44. #include <linux/file.h>
  45. #include <linux/fdtable.h>
  46. #include <linux/namei.h>
  47. #include <linux/mount.h>
  48. #include <linux/netfilter_ipv4.h>
  49. #include <linux/netfilter_ipv6.h>
  50. #include <linux/tty.h>
  51. #include <net/icmp.h>
  52. #include <net/ip.h>
  53. #include <net/tcp.h>
  54. #include <net/net_namespace.h>
  55. #include <net/netlabel.h>
  56. #include <linux/uaccess.h>
  57. #include <asm/ioctls.h>
  58. #include <linux/atomic.h>
  59. #include <linux/bitops.h>
  60. #include <linux/interrupt.h>
  61. #include <linux/netdevice.h>
  62. #include <linux/netlink.h>
  63. #include <linux/tcp.h>
  64. #include <linux/udp.h>
  65. #include <linux/dccp.h>
  66. #include <linux/quota.h>
  67. #include <linux/un.h>
  68. #include <net/af_unix.h>
  69. #include <linux/parser.h>
  70. #include <linux/nfs_mount.h>
  71. #include <net/ipv6.h>
  72. #include <linux/hugetlb.h>
  73. #include <linux/personality.h>
  74. #include <linux/audit.h>
  75. #include <linux/string.h>
  76. #include <linux/selinux.h>
  77. #include <linux/mutex.h>
  78. #include <linux/posix-timers.h>
  79. #include <linux/syslog.h>
  80. #include <linux/user_namespace.h>
  81. #include <linux/export.h>
  82. #include <linux/msg.h>
  83. #include <linux/shm.h>
  84. #include "avc.h"
  85. #include "objsec.h"
  86. #include "netif.h"
  87. #include "netnode.h"
  88. #include "netport.h"
  89. #include "xfrm.h"
  90. #include "netlabel.h"
  91. #include "audit.h"
  92. #include "avc_ss.h"
  93. #define NUM_SEL_MNT_OPTS 5
  94. extern struct security_operations *security_ops;
  95. static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
  96. #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
  97. int selinux_enforcing;
  98. static int __init enforcing_setup(char *str)
  99. {
  100. unsigned long enforcing;
  101. if (!strict_strtoul(str, 0, &enforcing))
  102. selinux_enforcing = enforcing ? 1 : 0;
  103. return 1;
  104. }
  105. __setup("enforcing=", enforcing_setup);
  106. #endif
  107. #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
  108. int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE;
  109. static int __init selinux_enabled_setup(char *str)
  110. {
  111. unsigned long enabled;
  112. if (!strict_strtoul(str, 0, &enabled))
  113. selinux_enabled = enabled ? 1 : 0;
  114. return 1;
  115. }
  116. __setup("selinux=", selinux_enabled_setup);
  117. #else
  118. int selinux_enabled = 1;
  119. #endif
  120. static struct kmem_cache *sel_inode_cache;
  121. static int selinux_secmark_enabled(void)
  122. {
  123. return (atomic_read(&selinux_secmark_refcount) > 0);
  124. }
  125. static void cred_init_security(void)
  126. {
  127. struct cred *cred = (struct cred *) current->real_cred;
  128. struct task_security_struct *tsec;
  129. tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL);
  130. if (!tsec)
  131. panic("SELinux: Failed to initialize initial task.\n");
  132. tsec->osid = tsec->sid = SECINITSID_KERNEL;
  133. cred->security = tsec;
  134. }
  135. static inline u32 cred_sid(const struct cred *cred)
  136. {
  137. const struct task_security_struct *tsec;
  138. tsec = cred->security;
  139. return tsec->sid;
  140. }
  141. static inline u32 task_sid(const struct task_struct *task)
  142. {
  143. u32 sid;
  144. rcu_read_lock();
  145. sid = cred_sid(__task_cred(task));
  146. rcu_read_unlock();
  147. return sid;
  148. }
  149. static inline u32 current_sid(void)
  150. {
  151. const struct task_security_struct *tsec = current_security();
  152. return tsec->sid;
  153. }
  154. static int inode_alloc_security(struct inode *inode)
  155. {
  156. struct inode_security_struct *isec;
  157. u32 sid = current_sid();
  158. isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS);
  159. if (!isec)
  160. return -ENOMEM;
  161. mutex_init(&isec->lock);
  162. INIT_LIST_HEAD(&isec->list);
  163. isec->inode = inode;
  164. isec->sid = SECINITSID_UNLABELED;
  165. isec->sclass = SECCLASS_FILE;
  166. isec->task_sid = sid;
  167. inode->i_security = isec;
  168. return 0;
  169. }
  170. static void inode_free_security(struct inode *inode)
  171. {
  172. struct inode_security_struct *isec = inode->i_security;
  173. struct superblock_security_struct *sbsec = inode->i_sb->s_security;
  174. spin_lock(&sbsec->isec_lock);
  175. if (!list_empty(&isec->list))
  176. list_del_init(&isec->list);
  177. spin_unlock(&sbsec->isec_lock);
  178. inode->i_security = NULL;
  179. kmem_cache_free(sel_inode_cache, isec);
  180. }
  181. static int file_alloc_security(struct file *file)
  182. {
  183. struct file_security_struct *fsec;
  184. u32 sid = current_sid();
  185. fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL);
  186. if (!fsec)
  187. return -ENOMEM;
  188. fsec->sid = sid;
  189. fsec->fown_sid = sid;
  190. file->f_security = fsec;
  191. return 0;
  192. }
  193. static void file_free_security(struct file *file)
  194. {
  195. struct file_security_struct *fsec = file->f_security;
  196. file->f_security = NULL;
  197. kfree(fsec);
  198. }
  199. static int superblock_alloc_security(struct super_block *sb)
  200. {
  201. struct superblock_security_struct *sbsec;
  202. sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
  203. if (!sbsec)
  204. return -ENOMEM;
  205. mutex_init(&sbsec->lock);
  206. INIT_LIST_HEAD(&sbsec->isec_head);
  207. spin_lock_init(&sbsec->isec_lock);
  208. sbsec->sb = sb;
  209. sbsec->sid = SECINITSID_UNLABELED;
  210. sbsec->def_sid = SECINITSID_FILE;
  211. sbsec->mntpoint_sid = SECINITSID_UNLABELED;
  212. sb->s_security = sbsec;
  213. return 0;
  214. }
  215. static void superblock_free_security(struct super_block *sb)
  216. {
  217. struct superblock_security_struct *sbsec = sb->s_security;
  218. sb->s_security = NULL;
  219. kfree(sbsec);
  220. }
  221. static const char *labeling_behaviors[6] = {
  222. "uses xattr",
  223. "uses transition SIDs",
  224. "uses task SIDs",
  225. "uses genfs_contexts",
  226. "not configured for labeling",
  227. "uses mountpoint labeling",
  228. };
  229. static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
  230. static inline int inode_doinit(struct inode *inode)
  231. {
  232. return inode_doinit_with_dentry(inode, NULL);
  233. }
  234. enum {
  235. Opt_error = -1,
  236. Opt_context = 1,
  237. Opt_fscontext = 2,
  238. Opt_defcontext = 3,
  239. Opt_rootcontext = 4,
  240. Opt_labelsupport = 5,
  241. };
  242. static const match_table_t tokens = {
  243. {Opt_context, CONTEXT_STR "%s"},
  244. {Opt_fscontext, FSCONTEXT_STR "%s"},
  245. {Opt_defcontext, DEFCONTEXT_STR "%s"},
  246. {Opt_rootcontext, ROOTCONTEXT_STR "%s"},
  247. {Opt_labelsupport, LABELSUPP_STR},
  248. {Opt_error, NULL},
  249. };
  250. #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
  251. static int may_context_mount_sb_relabel(u32 sid,
  252. struct superblock_security_struct *sbsec,
  253. const struct cred *cred)
  254. {
  255. const struct task_security_struct *tsec = cred->security;
  256. int rc;
  257. rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
  258. FILESYSTEM__RELABELFROM, NULL);
  259. if (rc)
  260. return rc;
  261. rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM,
  262. FILESYSTEM__RELABELTO, NULL);
  263. return rc;
  264. }
  265. static int may_context_mount_inode_relabel(u32 sid,
  266. struct superblock_security_struct *sbsec,
  267. const struct cred *cred)
  268. {
  269. const struct task_security_struct *tsec = cred->security;
  270. int rc;
  271. rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
  272. FILESYSTEM__RELABELFROM, NULL);
  273. if (rc)
  274. return rc;
  275. rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM,
  276. FILESYSTEM__ASSOCIATE, NULL);
  277. return rc;
  278. }
  279. static int sb_finish_set_opts(struct super_block *sb)
  280. {
  281. struct superblock_security_struct *sbsec = sb->s_security;
  282. struct dentry *root = sb->s_root;
  283. struct inode *root_inode = root->d_inode;
  284. int rc = 0;
  285. if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
  286. if (!root_inode->i_op->getxattr) {
  287. printk(KERN_WARNING "SELinux: (dev %s, type %s) has no "
  288. "xattr support\n", sb->s_id, sb->s_type->name);
  289. rc = -EOPNOTSUPP;
  290. goto out;
  291. }
  292. rc = root_inode->i_op->getxattr(root, XATTR_NAME_SELINUX, NULL, 0);
  293. if (rc < 0 && rc != -ENODATA) {
  294. if (rc == -EOPNOTSUPP)
  295. printk(KERN_WARNING "SELinux: (dev %s, type "
  296. "%s) has no security xattr handler\n",
  297. sb->s_id, sb->s_type->name);
  298. else
  299. printk(KERN_WARNING "SELinux: (dev %s, type "
  300. "%s) getxattr errno %d\n", sb->s_id,
  301. sb->s_type->name, -rc);
  302. goto out;
  303. }
  304. }
  305. sbsec->flags |= (SE_SBINITIALIZED | SE_SBLABELSUPP);
  306. if (sbsec->behavior > ARRAY_SIZE(labeling_behaviors))
  307. printk(KERN_ERR "SELinux: initialized (dev %s, type %s), unknown behavior\n",
  308. sb->s_id, sb->s_type->name);
  309. else
  310. printk(KERN_DEBUG "SELinux: initialized (dev %s, type %s), %s\n",
  311. sb->s_id, sb->s_type->name,
  312. labeling_behaviors[sbsec->behavior-1]);
  313. if (sbsec->behavior == SECURITY_FS_USE_GENFS ||
  314. sbsec->behavior == SECURITY_FS_USE_MNTPOINT ||
  315. sbsec->behavior == SECURITY_FS_USE_NONE ||
  316. sbsec->behavior > ARRAY_SIZE(labeling_behaviors))
  317. sbsec->flags &= ~SE_SBLABELSUPP;
  318. if (strncmp(sb->s_type->name, "sysfs", sizeof("sysfs")) == 0)
  319. sbsec->flags |= SE_SBLABELSUPP;
  320. rc = inode_doinit_with_dentry(root_inode, root);
  321. spin_lock(&sbsec->isec_lock);
  322. next_inode:
  323. if (!list_empty(&sbsec->isec_head)) {
  324. struct inode_security_struct *isec =
  325. list_entry(sbsec->isec_head.next,
  326. struct inode_security_struct, list);
  327. struct inode *inode = isec->inode;
  328. spin_unlock(&sbsec->isec_lock);
  329. inode = igrab(inode);
  330. if (inode) {
  331. if (!IS_PRIVATE(inode))
  332. inode_doinit(inode);
  333. iput(inode);
  334. }
  335. spin_lock(&sbsec->isec_lock);
  336. list_del_init(&isec->list);
  337. goto next_inode;
  338. }
  339. spin_unlock(&sbsec->isec_lock);
  340. out:
  341. return rc;
  342. }
  343. static int selinux_get_mnt_opts(const struct super_block *sb,
  344. struct security_mnt_opts *opts)
  345. {
  346. int rc = 0, i;
  347. struct superblock_security_struct *sbsec = sb->s_security;
  348. char *context = NULL;
  349. u32 len;
  350. char tmp;
  351. security_init_mnt_opts(opts);
  352. if (!(sbsec->flags & SE_SBINITIALIZED))
  353. return -EINVAL;
  354. if (!ss_initialized)
  355. return -EINVAL;
  356. tmp = sbsec->flags & SE_MNTMASK;
  357. for (i = 0; i < 8; i++) {
  358. if (tmp & 0x01)
  359. opts->num_mnt_opts++;
  360. tmp >>= 1;
  361. }
  362. if (sbsec->flags & SE_SBLABELSUPP)
  363. opts->num_mnt_opts++;
  364. opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC);
  365. if (!opts->mnt_opts) {
  366. rc = -ENOMEM;
  367. goto out_free;
  368. }
  369. opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC);
  370. if (!opts->mnt_opts_flags) {
  371. rc = -ENOMEM;
  372. goto out_free;
  373. }
  374. i = 0;
  375. if (sbsec->flags & FSCONTEXT_MNT) {
  376. rc = security_sid_to_context(sbsec->sid, &context, &len);
  377. if (rc)
  378. goto out_free;
  379. opts->mnt_opts[i] = context;
  380. opts->mnt_opts_flags[i++] = FSCONTEXT_MNT;
  381. }
  382. if (sbsec->flags & CONTEXT_MNT) {
  383. rc = security_sid_to_context(sbsec->mntpoint_sid, &context, &len);
  384. if (rc)
  385. goto out_free;
  386. opts->mnt_opts[i] = context;
  387. opts->mnt_opts_flags[i++] = CONTEXT_MNT;
  388. }
  389. if (sbsec->flags & DEFCONTEXT_MNT) {
  390. rc = security_sid_to_context(sbsec->def_sid, &context, &len);
  391. if (rc)
  392. goto out_free;
  393. opts->mnt_opts[i] = context;
  394. opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT;
  395. }
  396. if (sbsec->flags & ROOTCONTEXT_MNT) {
  397. struct inode *root = sbsec->sb->s_root->d_inode;
  398. struct inode_security_struct *isec = root->i_security;
  399. rc = security_sid_to_context(isec->sid, &context, &len);
  400. if (rc)
  401. goto out_free;
  402. opts->mnt_opts[i] = context;
  403. opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT;
  404. }
  405. if (sbsec->flags & SE_SBLABELSUPP) {
  406. opts->mnt_opts[i] = NULL;
  407. opts->mnt_opts_flags[i++] = SE_SBLABELSUPP;
  408. }
  409. BUG_ON(i != opts->num_mnt_opts);
  410. return 0;
  411. out_free:
  412. security_free_mnt_opts(opts);
  413. return rc;
  414. }
  415. static int bad_option(struct superblock_security_struct *sbsec, char flag,
  416. u32 old_sid, u32 new_sid)
  417. {
  418. char mnt_flags = sbsec->flags & SE_MNTMASK;
  419. if (sbsec->flags & SE_SBINITIALIZED)
  420. if (!(sbsec->flags & flag) ||
  421. (old_sid != new_sid))
  422. return 1;
  423. if (!(sbsec->flags & SE_SBINITIALIZED))
  424. if (mnt_flags & flag)
  425. return 1;
  426. return 0;
  427. }
  428. static int selinux_set_mnt_opts(struct super_block *sb,
  429. struct security_mnt_opts *opts)
  430. {
  431. const struct cred *cred = current_cred();
  432. int rc = 0, i;
  433. struct superblock_security_struct *sbsec = sb->s_security;
  434. const char *name = sb->s_type->name;
  435. struct inode *inode = sbsec->sb->s_root->d_inode;
  436. struct inode_security_struct *root_isec = inode->i_security;
  437. u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
  438. u32 defcontext_sid = 0;
  439. char **mount_options = opts->mnt_opts;
  440. int *flags = opts->mnt_opts_flags;
  441. int num_opts = opts->num_mnt_opts;
  442. mutex_lock(&sbsec->lock);
  443. if (!ss_initialized) {
  444. if (!num_opts) {
  445. goto out;
  446. }
  447. rc = -EINVAL;
  448. printk(KERN_WARNING "SELinux: Unable to set superblock options "
  449. "before the security server is initialized\n");
  450. goto out;
  451. }
  452. if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
  453. && (num_opts == 0))
  454. goto out;
  455. for (i = 0; i < num_opts; i++) {
  456. u32 sid;
  457. if (flags[i] == SE_SBLABELSUPP)
  458. continue;
  459. rc = security_context_to_sid(mount_options[i],
  460. strlen(mount_options[i]), &sid);
  461. if (rc) {
  462. printk(KERN_WARNING "SELinux: security_context_to_sid"
  463. "(%s) failed for (dev %s, type %s) errno=%d\n",
  464. mount_options[i], sb->s_id, name, rc);
  465. goto out;
  466. }
  467. switch (flags[i]) {
  468. case FSCONTEXT_MNT:
  469. fscontext_sid = sid;
  470. if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
  471. fscontext_sid))
  472. goto out_double_mount;
  473. sbsec->flags |= FSCONTEXT_MNT;
  474. break;
  475. case CONTEXT_MNT:
  476. context_sid = sid;
  477. if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
  478. context_sid))
  479. goto out_double_mount;
  480. sbsec->flags |= CONTEXT_MNT;
  481. break;
  482. case ROOTCONTEXT_MNT:
  483. rootcontext_sid = sid;
  484. if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
  485. rootcontext_sid))
  486. goto out_double_mount;
  487. sbsec->flags |= ROOTCONTEXT_MNT;
  488. break;
  489. case DEFCONTEXT_MNT:
  490. defcontext_sid = sid;
  491. if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
  492. defcontext_sid))
  493. goto out_double_mount;
  494. sbsec->flags |= DEFCONTEXT_MNT;
  495. break;
  496. default:
  497. rc = -EINVAL;
  498. goto out;
  499. }
  500. }
  501. if (sbsec->flags & SE_SBINITIALIZED) {
  502. if ((sbsec->flags & SE_MNTMASK) && !num_opts)
  503. goto out_double_mount;
  504. rc = 0;
  505. goto out;
  506. }
  507. if (strcmp(sb->s_type->name, "proc") == 0)
  508. sbsec->flags |= SE_SBPROC;
  509. rc = security_fs_use((sbsec->flags & SE_SBPROC) ? "proc" : sb->s_type->name, &sbsec->behavior, &sbsec->sid);
  510. if (rc) {
  511. printk(KERN_WARNING "%s: security_fs_use(%s) returned %d\n",
  512. __func__, sb->s_type->name, rc);
  513. goto out;
  514. }
  515. if (fscontext_sid) {
  516. rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
  517. if (rc)
  518. goto out;
  519. sbsec->sid = fscontext_sid;
  520. }
  521. if (context_sid) {
  522. if (!fscontext_sid) {
  523. rc = may_context_mount_sb_relabel(context_sid, sbsec,
  524. cred);
  525. if (rc)
  526. goto out;
  527. sbsec->sid = context_sid;
  528. } else {
  529. rc = may_context_mount_inode_relabel(context_sid, sbsec,
  530. cred);
  531. if (rc)
  532. goto out;
  533. }
  534. if (!rootcontext_sid)
  535. rootcontext_sid = context_sid;
  536. sbsec->mntpoint_sid = context_sid;
  537. sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
  538. }
  539. if (rootcontext_sid) {
  540. rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
  541. cred);
  542. if (rc)
  543. goto out;
  544. root_isec->sid = rootcontext_sid;
  545. root_isec->initialized = 1;
  546. }
  547. if (defcontext_sid) {
  548. if (sbsec->behavior != SECURITY_FS_USE_XATTR) {
  549. rc = -EINVAL;
  550. printk(KERN_WARNING "SELinux: defcontext option is "
  551. "invalid for this filesystem type\n");
  552. goto out;
  553. }
  554. if (defcontext_sid != sbsec->def_sid) {
  555. rc = may_context_mount_inode_relabel(defcontext_sid,
  556. sbsec, cred);
  557. if (rc)
  558. goto out;
  559. }
  560. sbsec->def_sid = defcontext_sid;
  561. }
  562. rc = sb_finish_set_opts(sb);
  563. out:
  564. mutex_unlock(&sbsec->lock);
  565. return rc;
  566. out_double_mount:
  567. rc = -EINVAL;
  568. printk(KERN_WARNING "SELinux: mount invalid. Same superblock, different "
  569. "security settings for (dev %s, type %s)\n", sb->s_id, name);
  570. goto out;
  571. }
  572. static void selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
  573. struct super_block *newsb)
  574. {
  575. const struct superblock_security_struct *oldsbsec = oldsb->s_security;
  576. struct superblock_security_struct *newsbsec = newsb->s_security;
  577. int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT);
  578. int set_context = (oldsbsec->flags & CONTEXT_MNT);
  579. int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT);
  580. if (!ss_initialized)
  581. return;
  582. BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
  583. if (newsbsec->flags & SE_SBINITIALIZED)
  584. return;
  585. mutex_lock(&newsbsec->lock);
  586. newsbsec->flags = oldsbsec->flags;
  587. newsbsec->sid = oldsbsec->sid;
  588. newsbsec->def_sid = oldsbsec->def_sid;
  589. newsbsec->behavior = oldsbsec->behavior;
  590. if (set_context) {
  591. u32 sid = oldsbsec->mntpoint_sid;
  592. if (!set_fscontext)
  593. newsbsec->sid = sid;
  594. if (!set_rootcontext) {
  595. struct inode *newinode = newsb->s_root->d_inode;
  596. struct inode_security_struct *newisec = newinode->i_security;
  597. newisec->sid = sid;
  598. }
  599. newsbsec->mntpoint_sid = sid;
  600. }
  601. if (set_rootcontext) {
  602. const struct inode *oldinode = oldsb->s_root->d_inode;
  603. const struct inode_security_struct *oldisec = oldinode->i_security;
  604. struct inode *newinode = newsb->s_root->d_inode;
  605. struct inode_security_struct *newisec = newinode->i_security;
  606. newisec->sid = oldisec->sid;
  607. }
  608. sb_finish_set_opts(newsb);
  609. mutex_unlock(&newsbsec->lock);
  610. }
  611. static int selinux_parse_opts_str(char *options,
  612. struct security_mnt_opts *opts)
  613. {
  614. char *p;
  615. char *context = NULL, *defcontext = NULL;
  616. char *fscontext = NULL, *rootcontext = NULL;
  617. int rc, num_mnt_opts = 0;
  618. opts->num_mnt_opts = 0;
  619. while ((p = strsep(&options, "|")) != NULL) {
  620. int token;
  621. substring_t args[MAX_OPT_ARGS];
  622. if (!*p)
  623. continue;
  624. token = match_token(p, tokens, args);
  625. switch (token) {
  626. case Opt_context:
  627. if (context || defcontext) {
  628. rc = -EINVAL;
  629. printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
  630. goto out_err;
  631. }
  632. context = match_strdup(&args[0]);
  633. if (!context) {
  634. rc = -ENOMEM;
  635. goto out_err;
  636. }
  637. break;
  638. case Opt_fscontext:
  639. if (fscontext) {
  640. rc = -EINVAL;
  641. printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
  642. goto out_err;
  643. }
  644. fscontext = match_strdup(&args[0]);
  645. if (!fscontext) {
  646. rc = -ENOMEM;
  647. goto out_err;
  648. }
  649. break;
  650. case Opt_rootcontext:
  651. if (rootcontext) {
  652. rc = -EINVAL;
  653. printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
  654. goto out_err;
  655. }
  656. rootcontext = match_strdup(&args[0]);
  657. if (!rootcontext) {
  658. rc = -ENOMEM;
  659. goto out_err;
  660. }
  661. break;
  662. case Opt_defcontext:
  663. if (context || defcontext) {
  664. rc = -EINVAL;
  665. printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
  666. goto out_err;
  667. }
  668. defcontext = match_strdup(&args[0]);
  669. if (!defcontext) {
  670. rc = -ENOMEM;
  671. goto out_err;
  672. }
  673. break;
  674. case Opt_labelsupport:
  675. break;
  676. default:
  677. rc = -EINVAL;
  678. printk(KERN_WARNING "SELinux: unknown mount option\n");
  679. goto out_err;
  680. }
  681. }
  682. rc = -ENOMEM;
  683. opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_ATOMIC);
  684. if (!opts->mnt_opts)
  685. goto out_err;
  686. opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int), GFP_ATOMIC);
  687. if (!opts->mnt_opts_flags) {
  688. kfree(opts->mnt_opts);
  689. goto out_err;
  690. }
  691. if (fscontext) {
  692. opts->mnt_opts[num_mnt_opts] = fscontext;
  693. opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT;
  694. }
  695. if (context) {
  696. opts->mnt_opts[num_mnt_opts] = context;
  697. opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT;
  698. }
  699. if (rootcontext) {
  700. opts->mnt_opts[num_mnt_opts] = rootcontext;
  701. opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT;
  702. }
  703. if (defcontext) {
  704. opts->mnt_opts[num_mnt_opts] = defcontext;
  705. opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT;
  706. }
  707. opts->num_mnt_opts = num_mnt_opts;
  708. return 0;
  709. out_err:
  710. kfree(context);
  711. kfree(defcontext);
  712. kfree(fscontext);
  713. kfree(rootcontext);
  714. return rc;
  715. }
  716. static int superblock_doinit(struct super_block *sb, void *data)
  717. {
  718. int rc = 0;
  719. char *options = data;
  720. struct security_mnt_opts opts;
  721. security_init_mnt_opts(&opts);
  722. if (!data)
  723. goto out;
  724. BUG_ON(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA);
  725. rc = selinux_parse_opts_str(options, &opts);
  726. if (rc)
  727. goto out_err;
  728. out:
  729. rc = selinux_set_mnt_opts(sb, &opts);
  730. out_err:
  731. security_free_mnt_opts(&opts);
  732. return rc;
  733. }
  734. static void selinux_write_opts(struct seq_file *m,
  735. struct security_mnt_opts *opts)
  736. {
  737. int i;
  738. char *prefix;
  739. for (i = 0; i < opts->num_mnt_opts; i++) {
  740. char *has_comma;
  741. if (opts->mnt_opts[i])
  742. has_comma = strchr(opts->mnt_opts[i], ',');
  743. else
  744. has_comma = NULL;
  745. switch (opts->mnt_opts_flags[i]) {
  746. case CONTEXT_MNT:
  747. prefix = CONTEXT_STR;
  748. break;
  749. case FSCONTEXT_MNT:
  750. prefix = FSCONTEXT_STR;
  751. break;
  752. case ROOTCONTEXT_MNT:
  753. prefix = ROOTCONTEXT_STR;
  754. break;
  755. case DEFCONTEXT_MNT:
  756. prefix = DEFCONTEXT_STR;
  757. break;
  758. case SE_SBLABELSUPP:
  759. seq_putc(m, ',');
  760. seq_puts(m, LABELSUPP_STR);
  761. continue;
  762. default:
  763. BUG();
  764. return;
  765. };
  766. seq_putc(m, ',');
  767. seq_puts(m, prefix);
  768. if (has_comma)
  769. seq_putc(m, '\"');
  770. seq_puts(m, opts->mnt_opts[i]);
  771. if (has_comma)
  772. seq_putc(m, '\"');
  773. }
  774. }
  775. static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
  776. {
  777. struct security_mnt_opts opts;
  778. int rc;
  779. rc = selinux_get_mnt_opts(sb, &opts);
  780. if (rc) {
  781. if (rc == -EINVAL)
  782. rc = 0;
  783. return rc;
  784. }
  785. selinux_write_opts(m, &opts);
  786. security_free_mnt_opts(&opts);
  787. return rc;
  788. }
  789. static inline u16 inode_mode_to_security_class(umode_t mode)
  790. {
  791. switch (mode & S_IFMT) {
  792. case S_IFSOCK:
  793. return SECCLASS_SOCK_FILE;
  794. case S_IFLNK:
  795. return SECCLASS_LNK_FILE;
  796. case S_IFREG:
  797. return SECCLASS_FILE;
  798. case S_IFBLK:
  799. return SECCLASS_BLK_FILE;
  800. case S_IFDIR:
  801. return SECCLASS_DIR;
  802. case S_IFCHR:
  803. return SECCLASS_CHR_FILE;
  804. case S_IFIFO:
  805. return SECCLASS_FIFO_FILE;
  806. }
  807. return SECCLASS_FILE;
  808. }
  809. static inline int default_protocol_stream(int protocol)
  810. {
  811. return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
  812. }
  813. static inline int default_protocol_dgram(int protocol)
  814. {
  815. return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
  816. }
  817. static inline u16 socket_type_to_security_class(int family, int type, int protocol)
  818. {
  819. switch (family) {
  820. case PF_UNIX:
  821. switch (type) {
  822. case SOCK_STREAM:
  823. case SOCK_SEQPACKET:
  824. return SECCLASS_UNIX_STREAM_SOCKET;
  825. case SOCK_DGRAM:
  826. return SECCLASS_UNIX_DGRAM_SOCKET;
  827. }
  828. break;
  829. case PF_INET:
  830. case PF_INET6:
  831. switch (type) {
  832. case SOCK_STREAM:
  833. if (default_protocol_stream(protocol))
  834. return SECCLASS_TCP_SOCKET;
  835. else
  836. return SECCLASS_RAWIP_SOCKET;
  837. case SOCK_DGRAM:
  838. if (default_protocol_dgram(protocol))
  839. return SECCLASS_UDP_SOCKET;
  840. else
  841. return SECCLASS_RAWIP_SOCKET;
  842. case SOCK_DCCP:
  843. return SECCLASS_DCCP_SOCKET;
  844. default:
  845. return SECCLASS_RAWIP_SOCKET;
  846. }
  847. break;
  848. case PF_NETLINK:
  849. switch (protocol) {
  850. case NETLINK_ROUTE:
  851. return SECCLASS_NETLINK_ROUTE_SOCKET;
  852. case NETLINK_FIREWALL:
  853. return SECCLASS_NETLINK_FIREWALL_SOCKET;
  854. case NETLINK_SOCK_DIAG:
  855. return SECCLASS_NETLINK_TCPDIAG_SOCKET;
  856. case NETLINK_NFLOG:
  857. return SECCLASS_NETLINK_NFLOG_SOCKET;
  858. case NETLINK_XFRM:
  859. return SECCLASS_NETLINK_XFRM_SOCKET;
  860. case NETLINK_SELINUX:
  861. return SECCLASS_NETLINK_SELINUX_SOCKET;
  862. case NETLINK_AUDIT:
  863. return SECCLASS_NETLINK_AUDIT_SOCKET;
  864. case NETLINK_IP6_FW:
  865. return SECCLASS_NETLINK_IP6FW_SOCKET;
  866. case NETLINK_DNRTMSG:
  867. return SECCLASS_NETLINK_DNRT_SOCKET;
  868. case NETLINK_KOBJECT_UEVENT:
  869. return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
  870. default:
  871. return SECCLASS_NETLINK_SOCKET;
  872. }
  873. case PF_PACKET:
  874. return SECCLASS_PACKET_SOCKET;
  875. case PF_KEY:
  876. return SECCLASS_KEY_SOCKET;
  877. case PF_APPLETALK:
  878. return SECCLASS_APPLETALK_SOCKET;
  879. }
  880. return SECCLASS_SOCKET;
  881. }
  882. #ifdef CONFIG_PROC_FS
  883. static int selinux_proc_get_sid(struct dentry *dentry,
  884. u16 tclass,
  885. u32 *sid)
  886. {
  887. int rc;
  888. char *buffer, *path;
  889. buffer = (char *)__get_free_page(GFP_KERNEL);
  890. if (!buffer)
  891. return -ENOMEM;
  892. path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
  893. if (IS_ERR(path))
  894. rc = PTR_ERR(path);
  895. else {
  896. while (path[1] >= '0' && path[1] <= '9') {
  897. path[1] = '/';
  898. path++;
  899. }
  900. rc = security_genfs_sid("proc", path, tclass, sid);
  901. }
  902. free_page((unsigned long)buffer);
  903. return rc;
  904. }
  905. #else
  906. static int selinux_proc_get_sid(struct dentry *dentry,
  907. u16 tclass,
  908. u32 *sid)
  909. {
  910. return -EINVAL;
  911. }
  912. #endif
  913. static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
  914. {
  915. struct superblock_security_struct *sbsec = NULL;
  916. struct inode_security_struct *isec = inode->i_security;
  917. u32 sid;
  918. struct dentry *dentry;
  919. #define INITCONTEXTLEN 255
  920. char *context = NULL;
  921. unsigned len = 0;
  922. int rc = 0;
  923. if (isec->initialized)
  924. goto out;
  925. mutex_lock(&isec->lock);
  926. if (isec->initialized)
  927. goto out_unlock;
  928. sbsec = inode->i_sb->s_security;
  929. if (!(sbsec->flags & SE_SBINITIALIZED)) {
  930. spin_lock(&sbsec->isec_lock);
  931. if (list_empty(&isec->list))
  932. list_add(&isec->list, &sbsec->isec_head);
  933. spin_unlock(&sbsec->isec_lock);
  934. goto out_unlock;
  935. }
  936. switch (sbsec->behavior) {
  937. case SECURITY_FS_USE_XATTR:
  938. if (!inode->i_op->getxattr) {
  939. isec->sid = sbsec->def_sid;
  940. break;
  941. }
  942. if (opt_dentry) {
  943. dentry = dget(opt_dentry);
  944. } else {
  945. dentry = d_find_alias(inode);
  946. }
  947. if (!dentry) {
  948. goto out_unlock;
  949. }
  950. len = INITCONTEXTLEN;
  951. context = kmalloc(len+1, GFP_NOFS);
  952. if (!context) {
  953. rc = -ENOMEM;
  954. dput(dentry);
  955. goto out_unlock;
  956. }
  957. context[len] = '\0';
  958. rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
  959. context, len);
  960. if (rc == -ERANGE) {
  961. kfree(context);
  962. rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
  963. NULL, 0);
  964. if (rc < 0) {
  965. dput(dentry);
  966. goto out_unlock;
  967. }
  968. len = rc;
  969. context = kmalloc(len+1, GFP_NOFS);
  970. if (!context) {
  971. rc = -ENOMEM;
  972. dput(dentry);
  973. goto out_unlock;
  974. }
  975. context[len] = '\0';
  976. rc = inode->i_op->getxattr(dentry,
  977. XATTR_NAME_SELINUX,
  978. context, len);
  979. }
  980. dput(dentry);
  981. if (rc < 0) {
  982. if (rc != -ENODATA) {
  983. printk(KERN_WARNING "SELinux: %s: getxattr returned "
  984. "%d for dev=%s ino=%ld\n", __func__,
  985. -rc, inode->i_sb->s_id, inode->i_ino);
  986. kfree(context);
  987. goto out_unlock;
  988. }
  989. sid = sbsec->def_sid;
  990. rc = 0;
  991. } else {
  992. rc = security_context_to_sid_default(context, rc, &sid,
  993. sbsec->def_sid,
  994. GFP_NOFS);
  995. if (rc) {
  996. char *dev = inode->i_sb->s_id;
  997. unsigned long ino = inode->i_ino;
  998. if (rc == -EINVAL) {
  999. if (printk_ratelimit())
  1000. printk(KERN_NOTICE "SELinux: inode=%lu on dev=%s was found to have an invalid "
  1001. "context=%s. This indicates you may need to relabel the inode or the "
  1002. "filesystem in question.\n", ino, dev, context);
  1003. } else {
  1004. printk(KERN_WARNING "SELinux: %s: context_to_sid(%s) "
  1005. "returned %d for dev=%s ino=%ld\n",
  1006. __func__, context, -rc, dev, ino);
  1007. }
  1008. kfree(context);
  1009. rc = 0;
  1010. break;
  1011. }
  1012. }
  1013. kfree(context);
  1014. isec->sid = sid;
  1015. break;
  1016. case SECURITY_FS_USE_TASK:
  1017. isec->sid = isec->task_sid;
  1018. break;
  1019. case SECURITY_FS_USE_TRANS:
  1020. isec->sid = sbsec->sid;
  1021. isec->sclass = inode_mode_to_security_class(inode->i_mode);
  1022. rc = security_transition_sid(isec->task_sid, sbsec->sid,
  1023. isec->sclass, NULL, &sid);
  1024. if (rc)
  1025. goto out_unlock;
  1026. isec->sid = sid;
  1027. break;
  1028. case SECURITY_FS_USE_MNTPOINT:
  1029. isec->sid = sbsec->mntpoint_sid;
  1030. break;
  1031. default:
  1032. isec->sid = sbsec->sid;
  1033. if ((sbsec->flags & SE_SBPROC) && !S_ISLNK(inode->i_mode)) {
  1034. if (opt_dentry) {
  1035. isec->sclass = inode_mode_to_security_class(inode->i_mode);
  1036. rc = selinux_proc_get_sid(opt_dentry,
  1037. isec->sclass,
  1038. &sid);
  1039. if (rc)
  1040. goto out_unlock;
  1041. isec->sid = sid;
  1042. }
  1043. }
  1044. break;
  1045. }
  1046. isec->initialized = 1;
  1047. out_unlock:
  1048. mutex_unlock(&isec->lock);
  1049. out:
  1050. if (isec->sclass == SECCLASS_FILE)
  1051. isec->sclass = inode_mode_to_security_class(inode->i_mode);
  1052. return rc;
  1053. }
  1054. static inline u32 signal_to_av(int sig)
  1055. {
  1056. u32 perm = 0;
  1057. switch (sig) {
  1058. case SIGCHLD:
  1059. perm = PROCESS__SIGCHLD;
  1060. break;
  1061. case SIGKILL:
  1062. perm = PROCESS__SIGKILL;
  1063. break;
  1064. case SIGSTOP:
  1065. perm = PROCESS__SIGSTOP;
  1066. break;
  1067. default:
  1068. perm = PROCESS__SIGNAL;
  1069. break;
  1070. }
  1071. return perm;
  1072. }
  1073. static int cred_has_perm(const struct cred *actor,
  1074. const struct cred *target,
  1075. u32 perms)
  1076. {
  1077. u32 asid = cred_sid(actor), tsid = cred_sid(target);
  1078. return avc_has_perm(asid, tsid, SECCLASS_PROCESS, perms, NULL);
  1079. }
  1080. static int task_has_perm(const struct task_struct *tsk1,
  1081. const struct task_struct *tsk2,
  1082. u32 perms)
  1083. {
  1084. const struct task_security_struct *__tsec1, *__tsec2;
  1085. u32 sid1, sid2;
  1086. rcu_read_lock();
  1087. __tsec1 = __task_cred(tsk1)->security; sid1 = __tsec1->sid;
  1088. __tsec2 = __task_cred(tsk2)->security; sid2 = __tsec2->sid;
  1089. rcu_read_unlock();
  1090. return avc_has_perm(sid1, sid2, SECCLASS_PROCESS, perms, NULL);
  1091. }
  1092. static int current_has_perm(const struct task_struct *tsk,
  1093. u32 perms)
  1094. {
  1095. u32 sid, tsid;
  1096. sid = current_sid();
  1097. tsid = task_sid(tsk);
  1098. return avc_has_perm(sid, tsid, SECCLASS_PROCESS, perms, NULL);
  1099. }
  1100. #if CAP_LAST_CAP > 63
  1101. #error Fix SELinux to handle capabilities > 63.
  1102. #endif
  1103. static int cred_has_capability(const struct cred *cred,
  1104. int cap, int audit)
  1105. {
  1106. struct common_audit_data ad;
  1107. struct selinux_audit_data sad = {0,};
  1108. struct av_decision avd;
  1109. u16 sclass;
  1110. u32 sid = cred_sid(cred);
  1111. u32 av = CAP_TO_MASK(cap);
  1112. int rc;
  1113. COMMON_AUDIT_DATA_INIT(&ad, CAP);
  1114. ad.selinux_audit_data = &sad;
  1115. ad.tsk = current;
  1116. ad.u.cap = cap;
  1117. switch (CAP_TO_INDEX(cap)) {
  1118. case 0:
  1119. sclass = SECCLASS_CAPABILITY;
  1120. break;
  1121. case 1:
  1122. sclass = SECCLASS_CAPABILITY2;
  1123. break;
  1124. default:
  1125. printk(KERN_ERR
  1126. "SELinux: out of range capability %d\n", cap);
  1127. BUG();
  1128. return -EINVAL;
  1129. }
  1130. rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd);
  1131. if (audit == SECURITY_CAP_AUDIT) {
  1132. int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad, 0);
  1133. if (rc2)
  1134. return rc2;
  1135. }
  1136. return rc;
  1137. }
  1138. static int task_has_system(struct task_struct *tsk,
  1139. u32 perms)
  1140. {
  1141. u32 sid = task_sid(tsk);
  1142. return avc_has_perm(sid, SECINITSID_KERNEL,
  1143. SECCLASS_SYSTEM, perms, NULL);
  1144. }
  1145. static int inode_has_perm(const struct cred *cred,
  1146. struct inode *inode,
  1147. u32 perms,
  1148. struct common_audit_data *adp,
  1149. unsigned flags)
  1150. {
  1151. struct inode_security_struct *isec;
  1152. u32 sid;
  1153. validate_creds(cred);
  1154. if (unlikely(IS_PRIVATE(inode)))
  1155. return 0;
  1156. sid = cred_sid(cred);
  1157. isec = inode->i_security;
  1158. if (unlikely(isec == NULL)) {
  1159. printk(KERN_WARNING "SELinux: inode->i_security is NULL, return 0 to aovid access isec this NULL pointer");
  1160. return 0;
  1161. }
  1162. return avc_has_perm_flags(sid, isec->sid, isec->sclass, perms, adp, flags);
  1163. }
  1164. static int inode_has_perm_noadp(const struct cred *cred,
  1165. struct inode *inode,
  1166. u32 perms,
  1167. unsigned flags)
  1168. {
  1169. struct common_audit_data ad;
  1170. struct selinux_audit_data sad = {0,};
  1171. COMMON_AUDIT_DATA_INIT(&ad, INODE);
  1172. ad.u.inode = inode;
  1173. ad.selinux_audit_data = &sad;
  1174. return inode_has_perm(cred, inode, perms, &ad, flags);
  1175. }
  1176. static inline int dentry_has_perm(const struct cred *cred,
  1177. struct dentry *dentry,
  1178. u32 av)
  1179. {
  1180. struct inode *inode = dentry->d_inode;
  1181. struct common_audit_data ad;
  1182. struct selinux_audit_data sad = {0,};
  1183. COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
  1184. ad.u.dentry = dentry;
  1185. ad.selinux_audit_data = &sad;
  1186. return inode_has_perm(cred, inode, av, &ad, 0);
  1187. }
  1188. static inline int path_has_perm(const struct cred *cred,
  1189. struct path *path,
  1190. u32 av)
  1191. {
  1192. struct inode *inode = path->dentry->d_inode;
  1193. struct common_audit_data ad;
  1194. struct selinux_audit_data sad = {0,};
  1195. COMMON_AUDIT_DATA_INIT(&ad, PATH);
  1196. ad.u.path = *path;
  1197. ad.selinux_audit_data = &sad;
  1198. return inode_has_perm(cred, inode, av, &ad, 0);
  1199. }
  1200. static int file_has_perm(const struct cred *cred,
  1201. struct file *file,
  1202. u32 av)
  1203. {
  1204. struct file_security_struct *fsec = file->f_security;
  1205. struct inode *inode = file->f_path.dentry->d_inode;
  1206. struct common_audit_data ad;
  1207. struct selinux_audit_data sad = {0,};
  1208. u32 sid = cred_sid(cred);
  1209. int rc;
  1210. COMMON_AUDIT_DATA_INIT(&ad, PATH);
  1211. ad.u.path = file->f_path;
  1212. ad.selinux_audit_data = &sad;
  1213. if (sid != fsec->sid) {
  1214. rc = avc_has_perm(sid, fsec->sid,
  1215. SECCLASS_FD,
  1216. FD__USE,
  1217. &ad);
  1218. if (rc)
  1219. goto out;
  1220. }
  1221. rc = 0;
  1222. if (av)
  1223. rc = inode_has_perm(cred, inode, av, &ad, 0);
  1224. out:
  1225. return rc;
  1226. }
  1227. static int may_create(struct inode *dir,
  1228. struct dentry *dentry,
  1229. u16 tclass)
  1230. {
  1231. const struct task_security_struct *tsec = current_security();
  1232. struct inode_security_struct *dsec;
  1233. struct superblock_security_struct *sbsec;
  1234. u32 sid, newsid;
  1235. struct common_audit_data ad;
  1236. struct selinux_audit_data sad = {0,};
  1237. int rc;
  1238. dsec = dir->i_security;
  1239. sbsec = dir->i_sb->s_security;
  1240. sid = tsec->sid;
  1241. newsid = tsec->create_sid;
  1242. COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
  1243. ad.u.dentry = dentry;
  1244. ad.selinux_audit_data = &sad;
  1245. rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR,
  1246. DIR__ADD_NAME | DIR__SEARCH,
  1247. &ad);
  1248. if (rc)
  1249. return rc;
  1250. if (!newsid || !(sbsec->flags & SE_SBLABELSUPP)) {
  1251. rc = security_transition_sid(sid, dsec->sid, tclass,
  1252. &dentry->d_name, &newsid);
  1253. if (rc)
  1254. return rc;
  1255. }
  1256. rc = avc_has_perm(sid, newsid, tclass, FILE__CREATE, &ad);
  1257. if (rc)
  1258. return rc;
  1259. return avc_has_perm(newsid, sbsec->sid,
  1260. SECCLASS_FILESYSTEM,
  1261. FILESYSTEM__ASSOCIATE, &ad);
  1262. }
  1263. static int may_create_key(u32 ksid,
  1264. struct task_struct *ctx)
  1265. {
  1266. u32 sid = task_sid(ctx);
  1267. return avc_has_perm(sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL);
  1268. }
  1269. #define MAY_LINK 0
  1270. #define MAY_UNLINK 1
  1271. #define MAY_RMDIR 2
  1272. static int may_link(struct inode *dir,
  1273. struct dentry *dentry,
  1274. int kind)
  1275. {
  1276. struct inode_security_struct *dsec, *isec;
  1277. struct common_audit_data ad;
  1278. struct selinux_audit_data sad = {0,};
  1279. u32 sid = current_sid();
  1280. u32 av;
  1281. int rc;
  1282. dsec = dir->i_security;
  1283. isec = dentry->d_inode->i_security;
  1284. COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
  1285. ad.u.dentry = dentry;
  1286. ad.selinux_audit_data = &sad;
  1287. av = DIR__SEARCH;
  1288. av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
  1289. rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, av, &ad);
  1290. if (rc)
  1291. return rc;
  1292. switch (kind) {
  1293. case MAY_LINK:
  1294. av = FILE__LINK;
  1295. break;
  1296. case MAY_UNLINK:
  1297. av = FILE__UNLINK;
  1298. break;
  1299. case MAY_RMDIR:
  1300. av = DIR__RMDIR;
  1301. break;
  1302. default:
  1303. printk(KERN_WARNING "SELinux: %s: unrecognized kind %d\n",
  1304. __func__, kind);
  1305. return 0;
  1306. }
  1307. rc = avc_has_perm(sid, isec->sid, isec->sclass, av, &ad);
  1308. return rc;
  1309. }
  1310. static inline int may_rename(struct inode *old_dir,
  1311. struct dentry *old_dentry,
  1312. struct inode *new_dir,
  1313. struct dentry *new_dentry)
  1314. {
  1315. struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
  1316. struct common_audit_data ad;
  1317. struct selinux_audit_data sad = {0,};
  1318. u32 sid = current_sid();
  1319. u32 av;
  1320. int old_is_dir, new_is_dir;
  1321. int rc;
  1322. old_dsec = old_dir->i_security;
  1323. old_isec = old_dentry->d_inode->i_security;
  1324. old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
  1325. new_dsec = new_dir->i_security;
  1326. COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
  1327. ad.selinux_audit_data = &sad;
  1328. ad.u.dentry = old_dentry;
  1329. rc = avc_has_perm(sid, old_dsec->sid, SECCLASS_DIR,
  1330. DIR__REMOVE_NAME | DIR__SEARCH, &ad);
  1331. if (rc)
  1332. return rc;
  1333. rc = avc_has_perm(sid, old_isec->sid,
  1334. old_isec->sclass, FILE__RENAME, &ad);
  1335. if (rc)
  1336. return rc;
  1337. if (old_is_dir && new_dir != old_dir) {
  1338. rc = avc_has_perm(sid, old_isec->sid,
  1339. old_isec->sclass, DIR__REPARENT, &ad);
  1340. if (rc)
  1341. return rc;
  1342. }
  1343. ad.u.dentry = new_dentry;
  1344. av = DIR__ADD_NAME | DIR__SEARCH;
  1345. if (new_dentry->d_inode)
  1346. av |= DIR__REMOVE_NAME;
  1347. rc = avc_has_perm(sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
  1348. if (rc)
  1349. return rc;
  1350. if (new_dentry->d_inode) {
  1351. new_isec = new_dentry->d_inode->i_security;
  1352. new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode);
  1353. rc = avc_has_perm(sid, new_isec->sid,
  1354. new_isec->sclass,
  1355. (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
  1356. if (rc)
  1357. return rc;
  1358. }
  1359. return 0;
  1360. }
  1361. static int superblock_has_perm(const struct cred *cred,
  1362. struct super_block *sb,
  1363. u32 perms,
  1364. struct common_audit_data *ad)
  1365. {
  1366. struct superblock_security_struct *sbsec;
  1367. u32 sid = cred_sid(cred);
  1368. sbsec = sb->s_security;
  1369. return avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
  1370. }
  1371. static inline u32 file_mask_to_av(int mode, int mask)
  1372. {
  1373. u32 av = 0;
  1374. if (!S_ISDIR(mode)) {
  1375. if (mask & MAY_EXEC)
  1376. av |= FILE__EXECUTE;
  1377. if (mask & MAY_READ)
  1378. av |= FILE__READ;
  1379. if (mask & MAY_APPEND)
  1380. av |= FILE__APPEND;
  1381. else if (mask & MAY_WRITE)
  1382. av |= FILE__WRITE;
  1383. } else {
  1384. if (mask & MAY_EXEC)
  1385. av |= DIR__SEARCH;
  1386. if (mask & MAY_WRITE)
  1387. av |= DIR__WRITE;
  1388. if (mask & MAY_READ)
  1389. av |= DIR__READ;
  1390. }
  1391. return av;
  1392. }
  1393. static inline u32 file_to_av(struct file *file)
  1394. {
  1395. u32 av = 0;
  1396. if (file->f_mode & FMODE_READ)
  1397. av |= FILE__READ;
  1398. if (file->f_mode & FMODE_WRITE) {
  1399. if (file->f_flags & O_APPEND)
  1400. av |= FILE__APPEND;
  1401. else
  1402. av |= FILE__WRITE;
  1403. }
  1404. if (!av) {
  1405. av = FILE__IOCTL;
  1406. }
  1407. return av;
  1408. }
  1409. static inline u32 open_file_to_av(struct file *file)
  1410. {
  1411. u32 av = file_to_av(file);
  1412. if (selinux_policycap_openperm)
  1413. av |= FILE__OPEN;
  1414. return av;
  1415. }
  1416. static int selinux_binder_set_context_mgr(struct task_struct *mgr)
  1417. {
  1418. u32 mysid = current_sid();
  1419. u32 mgrsid = task_sid(mgr);
  1420. return avc_has_perm(mysid, mgrsid, SECCLASS_BINDER, BINDER__SET_CONTEXT_MGR, NULL);
  1421. }
  1422. static int selinux_binder_transaction(struct task_struct *from, struct task_struct *to)
  1423. {
  1424. u32 mysid = current_sid();
  1425. u32 fromsid = task_sid(from);
  1426. u32 tosid = task_sid(to);
  1427. int rc;
  1428. if (mysid != fromsid) {
  1429. rc = avc_has_perm(mysid, fromsid, SECCLASS_BINDER, BINDER__IMPERSONATE, NULL);
  1430. if (rc)
  1431. return rc;
  1432. }
  1433. return avc_has_perm(fromsid, tosid, SECCLASS_BINDER, BINDER__CALL, NULL);
  1434. }
  1435. static int selinux_binder_transfer_binder(struct task_struct *from, struct task_struct *to)
  1436. {
  1437. u32 fromsid = task_sid(from);
  1438. u32 tosid = task_sid(to);
  1439. return avc_has_perm(fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER, NULL);
  1440. }
  1441. static int selinux_binder_transfer_file(struct task_struct *from, struct task_struct *to, struct file *file)
  1442. {
  1443. u32 sid = task_sid(to);
  1444. struct file_security_struct *fsec = file->f_security;
  1445. struct inode *inode = file->f_path.dentry->d_inode;
  1446. struct inode_security_struct *isec = inode->i_security;
  1447. struct common_audit_data ad;
  1448. struct selinux_audit_data sad = {0,};
  1449. int rc;
  1450. COMMON_AUDIT_DATA_INIT(&ad, PATH);
  1451. ad.u.path = file->f_path;
  1452. ad.selinux_audit_data = &sad;
  1453. if (sid != fsec->sid) {
  1454. rc = avc_has_perm(sid, fsec->sid,
  1455. SECCLASS_FD,
  1456. FD__USE,
  1457. &ad);
  1458. if (rc)
  1459. return rc;
  1460. }
  1461. if (unlikely(IS_PRIVATE(inode)))
  1462. return 0;
  1463. return avc_has_perm(sid, isec->sid, isec->sclass, file_to_av(file),
  1464. &ad);
  1465. }
  1466. static int selinux_ptrace_access_check(struct task_struct *child,
  1467. unsigned int mode)
  1468. {
  1469. int rc;
  1470. rc = cap_ptrace_access_check(child, mode);
  1471. if (rc)
  1472. return rc;
  1473. if (mode & PTRACE_MODE_READ) {
  1474. u32 sid = current_sid();
  1475. u32 csid = task_sid(child);
  1476. return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ, NULL);
  1477. }
  1478. return current_has_perm(child, PROCESS__PTRACE);
  1479. }
  1480. static int selinux_ptrace_traceme(struct task_struct *parent)
  1481. {
  1482. int rc;
  1483. rc = cap_ptrace_traceme(parent);
  1484. if (rc)
  1485. return rc;
  1486. return task_has_perm(parent, current, PROCESS__PTRACE);
  1487. }
  1488. static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
  1489. kernel_cap_t *inheritable, kernel_cap_t *permitted)
  1490. {
  1491. int error;
  1492. error = current_has_perm(target, PROCESS__GETCAP);
  1493. if (error)
  1494. return error;
  1495. return cap_capget(target, effective, inheritable, permitted);
  1496. }
  1497. static int selinux_capset(struct cred *new, const struct cred *old,
  1498. const kernel_cap_t *effective,
  1499. const kernel_cap_t *inheritable,
  1500. const kernel_cap_t *permitted)
  1501. {
  1502. int error;
  1503. error = cap_capset(new, old,
  1504. effective, inheritable, permitted);
  1505. if (error)
  1506. return error;
  1507. return cred_has_perm(old, new, PROCESS__SETCAP);
  1508. }
  1509. static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
  1510. int cap, int audit)
  1511. {
  1512. int rc;
  1513. rc = cap_capable(cred, ns, cap, audit);
  1514. if (rc)
  1515. return rc;
  1516. return cred_has_capability(cred, cap, audit);
  1517. }
  1518. static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
  1519. {
  1520. const struct cred *cred = current_cred();
  1521. int rc = 0;
  1522. if (!sb)
  1523. return 0;
  1524. switch (cmds) {
  1525. case Q_SYNC:
  1526. case Q_QUOTAON:
  1527. case Q_QUOTAOFF:
  1528. case Q_SETINFO:
  1529. case Q_SETQUOTA:
  1530. rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
  1531. break;
  1532. case Q_GETFMT:
  1533. case Q_GETINFO:
  1534. case Q_GETQUOTA:
  1535. rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
  1536. break;
  1537. default:
  1538. rc = 0;
  1539. break;
  1540. }
  1541. return rc;
  1542. }
  1543. static int selinux_quota_on(struct dentry *dentry)
  1544. {
  1545. const struct cred *cred = current_cred();
  1546. return dentry_has_perm(cred, dentry, FILE__QUOTAON);
  1547. }
  1548. static int selinux_syslog(int type)
  1549. {
  1550. int rc;
  1551. switch (type) {
  1552. case SYSLOG_ACTION_READ_ALL:
  1553. case SYSLOG_ACTION_SIZE_BUFFER:
  1554. rc = task_has_system(current, SYSTEM__SYSLOG_READ);
  1555. break;
  1556. case SYSLOG_ACTION_CONSOLE_OFF:
  1557. case SYSLOG_ACTION_CONSOLE_ON:
  1558. case SYSLOG_ACTION_CONSOLE_LEVEL:
  1559. rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE);
  1560. break;
  1561. case SYSLOG_ACTION_CLOSE:
  1562. case SYSLOG_ACTION_OPEN:
  1563. case SYSLOG_ACTION_READ:
  1564. case SYSLOG_ACTION_READ_CLEAR:
  1565. case SYSLOG_ACTION_CLEAR:
  1566. default:
  1567. rc = task_has_system(current, SYSTEM__SYSLOG_MOD);
  1568. break;
  1569. }
  1570. return rc;
  1571. }
  1572. static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
  1573. {
  1574. int rc, cap_sys_admin = 0;
  1575. rc = selinux_capable(current_cred(), &init_user_ns, CAP_SYS_ADMIN,
  1576. SECURITY_CAP_NOAUDIT);
  1577. if (rc == 0)
  1578. cap_sys_admin = 1;
  1579. return __vm_enough_memory(mm, pages, cap_sys_admin);
  1580. }
  1581. static int selinux_bprm_set_creds(struct linux_binprm *bprm)
  1582. {
  1583. const struct task_security_struct *old_tsec;
  1584. struct task_security_struct *new_tsec;
  1585. struct inode_security_struct *isec;
  1586. struct common_audit_data ad;
  1587. struct selinux_audit_data sad = {0,};
  1588. struct inode *inode = bprm->file->f_path.dentry->d_inode;
  1589. int rc;
  1590. rc = cap_bprm_set_creds(bprm);
  1591. if (rc)
  1592. return rc;
  1593. if (bprm->cred_prepared)
  1594. return 0;
  1595. old_tsec = current_security();
  1596. new_tsec = bprm->cred->security;
  1597. isec = inode->i_security;
  1598. new_tsec->sid = old_tsec->sid;
  1599. new_tsec->osid = old_tsec->sid;
  1600. new_tsec->create_sid = 0;
  1601. new_tsec->keycreate_sid = 0;
  1602. new_tsec->sockcreate_sid = 0;
  1603. if (old_tsec->exec_sid) {
  1604. new_tsec->sid = old_tsec->exec_sid;
  1605. new_tsec->exec_sid = 0;
  1606. } else {
  1607. rc = security_transition_sid(old_tsec->sid, isec->sid,
  1608. SECCLASS_PROCESS, NULL,
  1609. &new_tsec->sid);
  1610. if (rc)
  1611. return rc;
  1612. }
  1613. COMMON_AUDIT_DATA_INIT(&ad, PATH);
  1614. ad.selinux_audit_data = &sad;
  1615. ad.u.path = bprm->file->f_path;
  1616. if (bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID)
  1617. new_tsec->sid = old_tsec->sid;
  1618. if (new_tsec->sid == old_tsec->sid) {
  1619. rc = avc_has_perm(old_tsec->sid, isec->sid,
  1620. SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
  1621. if (rc)
  1622. return rc;
  1623. } else {
  1624. rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
  1625. SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
  1626. if (rc)
  1627. return rc;
  1628. rc = avc_has_perm(new_tsec->sid, isec->sid,
  1629. SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
  1630. if (rc)
  1631. return rc;
  1632. if (bprm->unsafe & LSM_UNSAFE_SHARE) {
  1633. rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
  1634. SECCLASS_PROCESS, PROCESS__SHARE,
  1635. NULL);
  1636. if (rc)
  1637. return -EPERM;
  1638. }
  1639. if (bprm->unsafe &
  1640. (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
  1641. struct task_struct *tracer;
  1642. struct task_security_struct *sec;
  1643. u32 ptsid = 0;
  1644. rcu_read_lock();
  1645. tracer = ptrace_parent(current);
  1646. if (likely(tracer != NULL)) {
  1647. sec = __task_cred(tracer)->security;
  1648. ptsid = sec->sid;
  1649. }
  1650. rcu_read_unlock();
  1651. if (ptsid != 0) {
  1652. rc = avc_has_perm(ptsid, new_tsec->sid,
  1653. SECCLASS_PROCESS,
  1654. PROCESS__PTRACE, NULL);
  1655. if (rc)
  1656. return -EPERM;
  1657. }
  1658. }
  1659. bprm->per_clear |= PER_CLEAR_ON_SETID;
  1660. }
  1661. return 0;
  1662. }
  1663. static int selinux_bprm_secureexec(struct linux_binprm *bprm)
  1664. {
  1665. const struct task_security_struct *tsec = current_security();
  1666. u32 sid, osid;
  1667. int atsecure = 0;
  1668. sid = tsec->sid;
  1669. osid = tsec->osid;
  1670. if (osid != sid) {
  1671. atsecure = avc_has_perm(osid, sid,
  1672. SECCLASS_PROCESS,
  1673. PROCESS__NOATSECURE, NULL);
  1674. }
  1675. return (atsecure || cap_bprm_secureexec(bprm));
  1676. }
  1677. static inline void flush_unauthorized_files(const struct cred *cred,
  1678. struct files_struct *files)
  1679. {
  1680. struct common_audit_data ad;
  1681. struct selinux_audit_data sad = {0,};
  1682. struct file *file, *devnull = NULL;
  1683. struct tty_struct *tty;
  1684. struct fdtable *fdt;
  1685. long j = -1;
  1686. int drop_tty = 0;
  1687. tty = get_current_tty();
  1688. if (tty) {
  1689. spin_lock(&tty_files_lock);
  1690. if (!list_empty(&tty->tty_files)) {
  1691. struct tty_file_private *file_priv;
  1692. struct inode *inode;
  1693. file_priv = list_first_entry(&tty->tty_files,
  1694. struct tty_file_private, list);
  1695. file = file_priv->file;
  1696. inode = file->f_path.dentry->d_inode;
  1697. if (inode_has_perm_noadp(cred, inode,
  1698. FILE__READ | FILE__WRITE, 0)) {
  1699. drop_tty = 1;
  1700. }
  1701. }
  1702. spin_unlock(&tty_files_lock);
  1703. tty_kref_put(tty);
  1704. }
  1705. if (drop_tty)
  1706. no_tty();
  1707. COMMON_AUDIT_DATA_INIT(&ad, INODE);
  1708. ad.selinux_audit_data = &sad;
  1709. spin_lock(&files->file_lock);
  1710. for (;;) {
  1711. unsigned long set, i;
  1712. int fd;
  1713. j++;
  1714. i = j * BITS_PER_LONG;
  1715. fdt = files_fdtable(files);
  1716. if (i >= fdt->max_fds)
  1717. break;
  1718. set = fdt->open_fds[j];
  1719. if (!set)
  1720. continue;
  1721. spin_unlock(&files->file_lock);
  1722. for ( ; set ; i++, set >>= 1) {
  1723. if (set & 1) {
  1724. file = fget(i);
  1725. if (!file)
  1726. continue;
  1727. if (file_has_perm(cred,
  1728. file,
  1729. file_to_av(file))) {
  1730. sys_close(i);
  1731. fd = get_unused_fd();
  1732. if (fd != i) {
  1733. if (fd >= 0)
  1734. put_unused_fd(fd);
  1735. fput(file);
  1736. continue;
  1737. }
  1738. if (devnull) {
  1739. get_file(devnull);
  1740. } else {
  1741. devnull = dentry_open(
  1742. dget(selinux_null),
  1743. mntget(selinuxfs_mount),
  1744. O_RDWR, cred);
  1745. if (IS_ERR(devnull)) {
  1746. devnull = NULL;
  1747. put_unused_fd(fd);
  1748. fput(file);
  1749. continue;
  1750. }
  1751. }
  1752. fd_install(fd, devnull);
  1753. }
  1754. fput(file);
  1755. }
  1756. }
  1757. spin_lock(&files->file_lock);
  1758. }
  1759. spin_unlock(&files->file_lock);
  1760. }
  1761. static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
  1762. {
  1763. struct task_security_struct *new_tsec;
  1764. struct rlimit *rlim, *initrlim;
  1765. int rc, i;
  1766. new_tsec = bprm->cred->security;
  1767. if (new_tsec->sid == new_tsec->osid)
  1768. return;
  1769. flush_unauthorized_files(bprm->cred, current->files);
  1770. current->pdeath_signal = 0;
  1771. rc = avc_has_perm(new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
  1772. PROCESS__RLIMITINH, NULL);
  1773. if (rc) {
  1774. task_lock(current);
  1775. for (i = 0; i < RLIM_NLIMITS; i++) {
  1776. rlim = current->signal->rlim + i;
  1777. initrlim = init_task.signal->rlim + i;
  1778. rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
  1779. }
  1780. task_unlock(current);
  1781. update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
  1782. }
  1783. }
  1784. static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
  1785. {
  1786. const struct task_security_struct *tsec = current_security();
  1787. struct itimerval itimer;
  1788. u32 osid, sid;
  1789. int rc, i;
  1790. osid = tsec->osid;
  1791. sid = tsec->sid;
  1792. if (sid == osid)
  1793. return;
  1794. rc = avc_has_perm(osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
  1795. if (rc) {
  1796. memset(&itimer, 0, sizeof itimer);
  1797. for (i = 0; i < 3; i++)
  1798. do_setitimer(i, &itimer, NULL);
  1799. spin_lock_irq(&current->sighand->siglock);
  1800. if (!(current->signal->flags & SIGNAL_GROUP_EXIT)) {
  1801. __flush_signals(current);
  1802. flush_signal_handlers(current, 1);
  1803. sigemptyset(&current->blocked);
  1804. }
  1805. spin_unlock_irq(&current->sighand->siglock);
  1806. }
  1807. read_lock(&tasklist_lock);
  1808. __wake_up_parent(current, current->real_parent);
  1809. read_unlock(&tasklist_lock);
  1810. }
  1811. static int selinux_sb_alloc_security(struct super_block *sb)
  1812. {
  1813. return superblock_alloc_security(sb);
  1814. }
  1815. static void selinux_sb_free_security(struct super_block *sb)
  1816. {
  1817. superblock_free_security(sb);
  1818. }
  1819. static inline int match_prefix(char *prefix, int plen, char *option, int olen)
  1820. {
  1821. if (plen > olen)
  1822. return 0;
  1823. return !memcmp(prefix, option, plen);
  1824. }
  1825. static inline int selinux_option(char *option, int len)
  1826. {
  1827. return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) ||
  1828. match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) ||
  1829. match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) ||
  1830. match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) ||
  1831. match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len));
  1832. }
  1833. static inline void take_option(char **to, char *from, int *first, int len)
  1834. {
  1835. if (!*first) {
  1836. **to = ',';
  1837. *to += 1;
  1838. } else
  1839. *first = 0;
  1840. memcpy(*to, from, len);
  1841. *to += len;
  1842. }
  1843. static inline void take_selinux_option(char **to, char *from, int *first,
  1844. int len)
  1845. {
  1846. int current_size = 0;
  1847. if (!*first) {
  1848. **to = '|';
  1849. *to += 1;
  1850. } else
  1851. *first = 0;
  1852. while (current_size < len) {
  1853. if (*from != '"') {
  1854. **to = *from;
  1855. *to += 1;
  1856. }
  1857. from += 1;
  1858. current_size += 1;
  1859. }
  1860. }
  1861. static int selinux_sb_copy_data(char *orig, char *copy)
  1862. {
  1863. int fnosec, fsec, rc = 0;
  1864. char *in_save, *in_cur

Large files files are truncated, but you can click here to view the full file