PageRenderTime 983ms CodeModel.GetById 18ms 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
  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_curr, *in_end;
  1865. char *sec_curr, *nosec_save, *nosec;
  1866. int open_quote = 0;
  1867. in_curr = orig;
  1868. sec_curr = copy;
  1869. nosec = (char *)get_zeroed_page(GFP_KERNEL);
  1870. if (!nosec) {
  1871. rc = -ENOMEM;
  1872. goto out;
  1873. }
  1874. nosec_save = nosec;
  1875. fnosec = fsec = 1;
  1876. in_save = in_end = orig;
  1877. do {
  1878. if (*in_end == '"')
  1879. open_quote = !open_quote;
  1880. if ((*in_end == ',' && open_quote == 0) ||
  1881. *in_end == '\0') {
  1882. int len = in_end - in_curr;
  1883. if (selinux_option(in_curr, len))
  1884. take_selinux_option(&sec_curr, in_curr, &fsec, len);
  1885. else
  1886. take_option(&nosec, in_curr, &fnosec, len);
  1887. in_curr = in_end + 1;
  1888. }
  1889. } while (*in_end++);
  1890. strcpy(in_save, nosec_save);
  1891. free_page((unsigned long)nosec_save);
  1892. out:
  1893. return rc;
  1894. }
  1895. static int selinux_sb_remount(struct super_block *sb, void *data)
  1896. {
  1897. int rc, i, *flags;
  1898. struct security_mnt_opts opts;
  1899. char *secdata, **mount_options;
  1900. struct superblock_security_struct *sbsec = sb->s_security;
  1901. if (!(sbsec->flags & SE_SBINITIALIZED))
  1902. return 0;
  1903. if (!data)
  1904. return 0;
  1905. if (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
  1906. return 0;
  1907. security_init_mnt_opts(&opts);
  1908. secdata = alloc_secdata();
  1909. if (!secdata)
  1910. return -ENOMEM;
  1911. rc = selinux_sb_copy_data(data, secdata);
  1912. if (rc)
  1913. goto out_free_secdata;
  1914. rc = selinux_parse_opts_str(secdata, &opts);
  1915. if (rc)
  1916. goto out_free_secdata;
  1917. mount_options = opts.mnt_opts;
  1918. flags = opts.mnt_opts_flags;
  1919. for (i = 0; i < opts.num_mnt_opts; i++) {
  1920. u32 sid;
  1921. size_t len;
  1922. if (flags[i] == SE_SBLABELSUPP)
  1923. continue;
  1924. len = strlen(mount_options[i]);
  1925. rc = security_context_to_sid(mount_options[i], len, &sid);
  1926. if (rc) {
  1927. printk(KERN_WARNING "SELinux: security_context_to_sid"
  1928. "(%s) failed for (dev %s, type %s) errno=%d\n",
  1929. mount_options[i], sb->s_id, sb->s_type->name, rc);
  1930. goto out_free_opts;
  1931. }
  1932. rc = -EINVAL;
  1933. switch (flags[i]) {
  1934. case FSCONTEXT_MNT:
  1935. if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
  1936. goto out_bad_option;
  1937. break;
  1938. case CONTEXT_MNT:
  1939. if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
  1940. goto out_bad_option;
  1941. break;
  1942. case ROOTCONTEXT_MNT: {
  1943. struct inode_security_struct *root_isec;
  1944. root_isec = sb->s_root->d_inode->i_security;
  1945. if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
  1946. goto out_bad_option;
  1947. break;
  1948. }
  1949. case DEFCONTEXT_MNT:
  1950. if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
  1951. goto out_bad_option;
  1952. break;
  1953. default:
  1954. goto out_free_opts;
  1955. }
  1956. }
  1957. rc = 0;
  1958. out_free_opts:
  1959. security_free_mnt_opts(&opts);
  1960. out_free_secdata:
  1961. free_secdata(secdata);
  1962. return rc;
  1963. out_bad_option:
  1964. printk(KERN_WARNING "SELinux: unable to change security options "
  1965. "during remount (dev %s, type=%s)\n", sb->s_id,
  1966. sb->s_type->name);
  1967. goto out_free_opts;
  1968. }
  1969. static int selinux_sb_kern_mount(struct super_block *sb, int flags, void *data)
  1970. {
  1971. const struct cred *cred = current_cred();
  1972. struct common_audit_data ad;
  1973. struct selinux_audit_data sad = {0,};
  1974. int rc;
  1975. rc = superblock_doinit(sb, data);
  1976. if (rc)
  1977. return rc;
  1978. if (flags & MS_KERNMOUNT)
  1979. return 0;
  1980. COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
  1981. ad.selinux_audit_data = &sad;
  1982. ad.u.dentry = sb->s_root;
  1983. return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
  1984. }
  1985. static int selinux_sb_statfs(struct dentry *dentry)
  1986. {
  1987. const struct cred *cred = current_cred();
  1988. struct common_audit_data ad;
  1989. struct selinux_audit_data sad = {0,};
  1990. COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
  1991. ad.selinux_audit_data = &sad;
  1992. ad.u.dentry = dentry->d_sb->s_root;
  1993. return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
  1994. }
  1995. static int selinux_mount(char *dev_name,
  1996. struct path *path,
  1997. char *type,
  1998. unsigned long flags,
  1999. void *data)
  2000. {
  2001. const struct cred *cred = current_cred();
  2002. if (flags & MS_REMOUNT)
  2003. return superblock_has_perm(cred, path->dentry->d_sb,
  2004. FILESYSTEM__REMOUNT, NULL);
  2005. else
  2006. return path_has_perm(cred, path, FILE__MOUNTON);
  2007. }
  2008. static int selinux_umount(struct vfsmount *mnt, int flags)
  2009. {
  2010. const struct cred *cred = current_cred();
  2011. return superblock_has_perm(cred, mnt->mnt_sb,
  2012. FILESYSTEM__UNMOUNT, NULL);
  2013. }
  2014. static int selinux_inode_alloc_security(struct inode *inode)
  2015. {
  2016. return inode_alloc_security(inode);
  2017. }
  2018. static void selinux_inode_free_security(struct inode *inode)
  2019. {
  2020. inode_free_security(inode);
  2021. }
  2022. static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
  2023. const struct qstr *qstr, char **name,
  2024. void **value, size_t *len)
  2025. {
  2026. const struct task_security_struct *tsec = current_security();
  2027. struct inode_security_struct *dsec;
  2028. struct superblock_security_struct *sbsec;
  2029. u32 sid, newsid, clen;
  2030. int rc;
  2031. char *namep = NULL, *context;
  2032. dsec = dir->i_security;
  2033. sbsec = dir->i_sb->s_security;
  2034. sid = tsec->sid;
  2035. newsid = tsec->create_sid;
  2036. if ((sbsec->flags & SE_SBINITIALIZED) &&
  2037. (sbsec->behavior == SECURITY_FS_USE_MNTPOINT))
  2038. newsid = sbsec->mntpoint_sid;
  2039. else if (!newsid || !(sbsec->flags & SE_SBLABELSUPP)) {
  2040. rc = security_transition_sid(sid, dsec->sid,
  2041. inode_mode_to_security_class(inode->i_mode),
  2042. qstr, &newsid);
  2043. if (rc) {
  2044. printk(KERN_WARNING "%s: "
  2045. "security_transition_sid failed, rc=%d (dev=%s "
  2046. "ino=%ld)\n",
  2047. __func__,
  2048. -rc, inode->i_sb->s_id, inode->i_ino);
  2049. return rc;
  2050. }
  2051. }
  2052. if (sbsec->flags & SE_SBINITIALIZED) {
  2053. struct inode_security_struct *isec = inode->i_security;
  2054. isec->sclass = inode_mode_to_security_class(inode->i_mode);
  2055. isec->sid = newsid;
  2056. isec->initialized = 1;
  2057. }
  2058. if (!ss_initialized || !(sbsec->flags & SE_SBLABELSUPP))
  2059. return -EOPNOTSUPP;
  2060. if (name) {
  2061. namep = kstrdup(XATTR_SELINUX_SUFFIX, GFP_NOFS);
  2062. if (!namep)
  2063. return -ENOMEM;
  2064. *name = namep;
  2065. }
  2066. if (value && len) {
  2067. rc = security_sid_to_context_force(newsid, &context, &clen);
  2068. if (rc) {
  2069. kfree(namep);
  2070. return rc;
  2071. }
  2072. *value = context;
  2073. *len = clen;
  2074. }
  2075. return 0;
  2076. }
  2077. static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
  2078. {
  2079. return may_create(dir, dentry, SECCLASS_FILE);
  2080. }
  2081. static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
  2082. {
  2083. return may_link(dir, old_dentry, MAY_LINK);
  2084. }
  2085. static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
  2086. {
  2087. return may_link(dir, dentry, MAY_UNLINK);
  2088. }
  2089. static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
  2090. {
  2091. return may_create(dir, dentry, SECCLASS_LNK_FILE);
  2092. }
  2093. static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
  2094. {
  2095. return may_create(dir, dentry, SECCLASS_DIR);
  2096. }
  2097. static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
  2098. {
  2099. return may_link(dir, dentry, MAY_RMDIR);
  2100. }
  2101. static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
  2102. {
  2103. return may_create(dir, dentry, inode_mode_to_security_class(mode));
  2104. }
  2105. static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
  2106. struct inode *new_inode, struct dentry *new_dentry)
  2107. {
  2108. return may_rename(old_inode, old_dentry, new_inode, new_dentry);
  2109. }
  2110. static int selinux_inode_readlink(struct dentry *dentry)
  2111. {
  2112. const struct cred *cred = current_cred();
  2113. return dentry_has_perm(cred, dentry, FILE__READ);
  2114. }
  2115. static int selinux_inode_follow_link(struct dentry *dentry, struct nameidata *nameidata)
  2116. {
  2117. const struct cred *cred = current_cred();
  2118. return dentry_has_perm(cred, dentry, FILE__READ);
  2119. }
  2120. static int selinux_inode_permission(struct inode *inode, int mask)
  2121. {
  2122. const struct cred *cred = current_cred();
  2123. struct common_audit_data ad;
  2124. struct selinux_audit_data sad = {0,};
  2125. u32 perms;
  2126. bool from_access;
  2127. unsigned flags = mask & MAY_NOT_BLOCK;
  2128. from_access = mask & MAY_ACCESS;
  2129. mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
  2130. if (!mask)
  2131. return 0;
  2132. COMMON_AUDIT_DATA_INIT(&ad, INODE);
  2133. ad.selinux_audit_data = &sad;
  2134. ad.u.inode = inode;
  2135. if (from_access)
  2136. ad.selinux_audit_data->auditdeny |= FILE__AUDIT_ACCESS;
  2137. perms = file_mask_to_av(inode->i_mode, mask);
  2138. return inode_has_perm(cred, inode, perms, &ad, flags);
  2139. }
  2140. static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
  2141. {
  2142. const struct cred *cred = current_cred();
  2143. unsigned int ia_valid = iattr->ia_valid;
  2144. if (ia_valid & ATTR_FORCE) {
  2145. ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
  2146. ATTR_FORCE);
  2147. if (!ia_valid)
  2148. return 0;
  2149. }
  2150. if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
  2151. ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
  2152. return dentry_has_perm(cred, dentry, FILE__SETATTR);
  2153. return dentry_has_perm(cred, dentry, FILE__WRITE);
  2154. }
  2155. static int selinux_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
  2156. {
  2157. const struct cred *cred = current_cred();
  2158. struct path path;
  2159. path.dentry = dentry;
  2160. path.mnt = mnt;
  2161. return path_has_perm(cred, &path, FILE__GETATTR);
  2162. }
  2163. static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name)
  2164. {
  2165. const struct cred *cred = current_cred();
  2166. if (!strncmp(name, XATTR_SECURITY_PREFIX,
  2167. sizeof XATTR_SECURITY_PREFIX - 1)) {
  2168. if (!strcmp(name, XATTR_NAME_CAPS)) {
  2169. if (!capable(CAP_SETFCAP))
  2170. return -EPERM;
  2171. } else if (!capable(CAP_SYS_ADMIN)) {
  2172. return -EPERM;
  2173. }
  2174. }
  2175. return dentry_has_perm(cred, dentry, FILE__SETATTR);
  2176. }
  2177. static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
  2178. const void *value, size_t size, int flags)
  2179. {
  2180. struct inode *inode = dentry->d_inode;
  2181. struct inode_security_struct *isec = inode->i_security;
  2182. struct superblock_security_struct *sbsec;
  2183. struct common_audit_data ad;
  2184. struct selinux_audit_data sad = {0,};
  2185. u32 newsid, sid = current_sid();
  2186. int rc = 0;
  2187. if (strcmp(name, XATTR_NAME_SELINUX))
  2188. return selinux_inode_setotherxattr(dentry, name);
  2189. sbsec = inode->i_sb->s_security;
  2190. if (!(sbsec->flags & SE_SBLABELSUPP))
  2191. return -EOPNOTSUPP;
  2192. if (!inode_owner_or_capable(inode))
  2193. return -EPERM;
  2194. COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
  2195. ad.selinux_audit_data = &sad;
  2196. ad.u.dentry = dentry;
  2197. rc = avc_has_perm(sid, isec->sid, isec->sclass,
  2198. FILE__RELABELFROM, &ad);
  2199. if (rc)
  2200. return rc;
  2201. rc = security_context_to_sid(value, size, &newsid);
  2202. if (rc == -EINVAL) {
  2203. if (!capable(CAP_MAC_ADMIN))
  2204. return rc;
  2205. rc = security_context_to_sid_force(value, size, &newsid);
  2206. }
  2207. if (rc)
  2208. return rc;
  2209. rc = avc_has_perm(sid, newsid, isec->sclass,
  2210. FILE__RELABELTO, &ad);
  2211. if (rc)
  2212. return rc;
  2213. rc = security_validate_transition(isec->sid, newsid, sid,
  2214. isec->sclass);
  2215. if (rc)
  2216. return rc;
  2217. return avc_has_perm(newsid,
  2218. sbsec->sid,
  2219. SECCLASS_FILESYSTEM,
  2220. FILESYSTEM__ASSOCIATE,
  2221. &ad);
  2222. }
  2223. static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
  2224. const void *value, size_t size,
  2225. int flags)
  2226. {
  2227. struct inode *inode = dentry->d_inode;
  2228. struct inode_security_struct *isec = inode->i_security;
  2229. u32 newsid;
  2230. int rc;
  2231. if (strcmp(name, XATTR_NAME_SELINUX)) {
  2232. return;
  2233. }
  2234. rc = security_context_to_sid_force(value, size, &newsid);
  2235. if (rc) {
  2236. printk(KERN_ERR "SELinux: unable to map context to SID"
  2237. "for (%s, %lu), rc=%d\n",
  2238. inode->i_sb->s_id, inode->i_ino, -rc);
  2239. return;
  2240. }
  2241. isec->sid = newsid;
  2242. return;
  2243. }
  2244. static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
  2245. {
  2246. const struct cred *cred = current_cred();
  2247. return dentry_has_perm(cred, dentry, FILE__GETATTR);
  2248. }
  2249. static int selinux_inode_listxattr(struct dentry *dentry)
  2250. {
  2251. const struct cred *cred = current_cred();
  2252. return dentry_has_perm(cred, dentry, FILE__GETATTR);
  2253. }
  2254. static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
  2255. {
  2256. if (strcmp(name, XATTR_NAME_SELINUX))
  2257. return selinux_inode_setotherxattr(dentry, name);
  2258. return -EACCES;
  2259. }
  2260. static int selinux_inode_getsecurity(const struct inode *inode, const char *name, void **buffer, bool alloc)
  2261. {
  2262. u32 size;
  2263. int error;
  2264. char *context = NULL;
  2265. struct inode_security_struct *isec = inode->i_security;
  2266. if (strcmp(name, XATTR_SELINUX_SUFFIX))
  2267. return -EOPNOTSUPP;
  2268. error = selinux_capable(current_cred(), &init_user_ns, CAP_MAC_ADMIN,
  2269. SECURITY_CAP_NOAUDIT);
  2270. if (!error)
  2271. error = security_sid_to_context_force(isec->sid, &context,
  2272. &size);
  2273. else
  2274. error = security_sid_to_context(isec->sid, &context, &size);
  2275. if (error)
  2276. return error;
  2277. error = size;
  2278. if (alloc) {
  2279. *buffer = context;
  2280. goto out_nofree;
  2281. }
  2282. kfree(context);
  2283. out_nofree:
  2284. return error;
  2285. }
  2286. static int selinux_inode_setsecurity(struct inode *inode, const char *name,
  2287. const void *value, size_t size, int flags)
  2288. {
  2289. struct inode_security_struct *isec = inode->i_security;
  2290. u32 newsid;
  2291. int rc;
  2292. if (strcmp(name, XATTR_SELINUX_SUFFIX))
  2293. return -EOPNOTSUPP;
  2294. if (!value || !size)
  2295. return -EACCES;
  2296. rc = security_context_to_sid((void *)value, size, &newsid);
  2297. if (rc)
  2298. return rc;
  2299. isec->sid = newsid;
  2300. isec->initialized = 1;
  2301. return 0;
  2302. }
  2303. static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
  2304. {
  2305. const int len = sizeof(XATTR_NAME_SELINUX);
  2306. if (buffer && len <= buffer_size)
  2307. memcpy(buffer, XATTR_NAME_SELINUX, len);
  2308. return len;
  2309. }
  2310. static void selinux_inode_getsecid(const struct inode *inode, u32 *secid)
  2311. {
  2312. struct inode_security_struct *isec = inode->i_security;
  2313. *secid = isec->sid;
  2314. }
  2315. static int selinux_revalidate_file_permission(struct file *file, int mask)
  2316. {
  2317. const struct cred *cred = current_cred();
  2318. struct inode *inode = file->f_path.dentry->d_inode;
  2319. if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
  2320. mask |= MAY_APPEND;
  2321. return file_has_perm(cred, file,
  2322. file_mask_to_av(inode->i_mode, mask));
  2323. }
  2324. static int selinux_file_permission(struct file *file, int mask)
  2325. {
  2326. struct inode *inode = file->f_path.dentry->d_inode;
  2327. struct file_security_struct *fsec = file->f_security;
  2328. struct inode_security_struct *isec = inode->i_security;
  2329. u32 sid = current_sid();
  2330. if (!mask)
  2331. return 0;
  2332. if (sid == fsec->sid && fsec->isid == isec->sid &&
  2333. fsec->pseqno == avc_policy_seqno())
  2334. return 0;
  2335. return selinux_revalidate_file_permission(file, mask);
  2336. }
  2337. static int selinux_file_alloc_security(struct file *file)
  2338. {
  2339. return file_alloc_security(file);
  2340. }
  2341. static void selinux_file_free_security(struct file *file)
  2342. {
  2343. file_free_security(file);
  2344. }
  2345. static int selinux_file_ioctl(struct file *file, unsigned int cmd,
  2346. unsigned long arg)
  2347. {
  2348. const struct cred *cred = current_cred();
  2349. int error = 0;
  2350. switch (cmd) {
  2351. case FIONREAD:
  2352. case FIBMAP:
  2353. case FIGETBSZ:
  2354. case FS_IOC_GETFLAGS:
  2355. case FS_IOC_GETVERSION:
  2356. error = file_has_perm(cred, file, FILE__GETATTR);
  2357. break;
  2358. case FS_IOC_SETFLAGS:
  2359. case FS_IOC_SETVERSION:
  2360. error = file_has_perm(cred, file, FILE__SETATTR);
  2361. break;
  2362. case FIONBIO:
  2363. case FIOASYNC:
  2364. error = file_has_perm(cred, file, 0);
  2365. break;
  2366. case KDSKBENT:
  2367. case KDSKBSENT:
  2368. error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
  2369. SECURITY_CAP_AUDIT);
  2370. break;
  2371. default:
  2372. error = file_has_perm(cred, file, FILE__IOCTL);
  2373. }
  2374. return error;
  2375. }
  2376. static int default_noexec;
  2377. static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
  2378. {
  2379. const struct cred *cred = current_cred();
  2380. int rc = 0;
  2381. if (default_noexec &&
  2382. (prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) {
  2383. rc = cred_has_perm(cred, cred, PROCESS__EXECMEM);
  2384. if (rc)
  2385. goto error;
  2386. }
  2387. if (file) {
  2388. u32 av = FILE__READ;
  2389. if (shared && (prot & PROT_WRITE))
  2390. av |= FILE__WRITE;
  2391. if (prot & PROT_EXEC)
  2392. av |= FILE__EXECUTE;
  2393. return file_has_perm(cred, file, av);
  2394. }
  2395. error:
  2396. return rc;
  2397. }
  2398. static int selinux_file_mmap(struct file *file, unsigned long reqprot,
  2399. unsigned long prot, unsigned long flags,
  2400. unsigned long addr, unsigned long addr_only)
  2401. {
  2402. int rc = 0;
  2403. u32 sid = current_sid();
  2404. if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
  2405. rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT,
  2406. MEMPROTECT__MMAP_ZERO, NULL);
  2407. if (rc)
  2408. return rc;
  2409. }
  2410. rc = cap_file_mmap(file, reqprot, prot, flags, addr, addr_only);
  2411. if (rc || addr_only)
  2412. return rc;
  2413. if (selinux_checkreqprot)
  2414. prot = reqprot;
  2415. return file_map_prot_check(file, prot,
  2416. (flags & MAP_TYPE) == MAP_SHARED);
  2417. }
  2418. static int selinux_file_mprotect(struct vm_area_struct *vma,
  2419. unsigned long reqprot,
  2420. unsigned long prot)
  2421. {
  2422. const struct cred *cred = current_cred();
  2423. if (selinux_checkreqprot)
  2424. prot = reqprot;
  2425. if (default_noexec &&
  2426. (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
  2427. int rc = 0;
  2428. if (vma->vm_start >= vma->vm_mm->start_brk &&
  2429. vma->vm_end <= vma->vm_mm->brk) {
  2430. rc = cred_has_perm(cred, cred, PROCESS__EXECHEAP);
  2431. } else if (!vma->vm_file &&
  2432. vma->vm_start <= vma->vm_mm->start_stack &&
  2433. vma->vm_end >= vma->vm_mm->start_stack) {
  2434. rc = current_has_perm(current, PROCESS__EXECSTACK);
  2435. } else if (vma->vm_file && vma->anon_vma) {
  2436. /*
  2437. * We are making executable a file mapping that has
  2438. * had some COW done. Since pages might have been
  2439. * written, check ability to execute the possibly
  2440. * modified content. This typically should only
  2441. * occur for text relocations.
  2442. */
  2443. rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
  2444. }
  2445. if (rc)
  2446. return rc;
  2447. }
  2448. return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
  2449. }
  2450. static int selinux_file_lock(struct file *file, unsigned int cmd)
  2451. {
  2452. const struct cred *cred = current_cred();
  2453. return file_has_perm(cred, file, FILE__LOCK);
  2454. }
  2455. static int selinux_file_fcntl(struct file *file, unsigned int cmd,
  2456. unsigned long arg)
  2457. {
  2458. const struct cred *cred = current_cred();
  2459. int err = 0;
  2460. switch (cmd) {
  2461. case F_SETFL:
  2462. if (!file->f_path.dentry || !file->f_path.dentry->d_inode) {
  2463. err = -EINVAL;
  2464. break;
  2465. }
  2466. if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
  2467. err = file_has_perm(cred, file, FILE__WRITE);
  2468. break;
  2469. }
  2470. case F_SETOWN:
  2471. case F_SETSIG:
  2472. case F_GETFL:
  2473. case F_GETOWN:
  2474. case F_GETSIG:
  2475. err = file_has_perm(cred, file, 0);
  2476. break;
  2477. case F_GETLK:
  2478. case F_SETLK:
  2479. case F_SETLKW:
  2480. #if BITS_PER_LONG == 32
  2481. case F_GETLK64:
  2482. case F_SETLK64:
  2483. case F_SETLKW64:
  2484. #endif
  2485. if (!file->f_path.dentry || !file->f_path.dentry->d_inode) {
  2486. err = -EINVAL;
  2487. break;
  2488. }
  2489. err = file_has_perm(cred, file, FILE__LOCK);
  2490. break;
  2491. }
  2492. return err;
  2493. }
  2494. static int selinux_file_set_fowner(struct file *file)
  2495. {
  2496. struct file_security_struct *fsec;
  2497. fsec = file->f_security;
  2498. fsec->fown_sid = current_sid();
  2499. return 0;
  2500. }
  2501. static int selinux_file_send_sigiotask(struct task_struct *tsk,
  2502. struct fown_struct *fown, int signum)
  2503. {
  2504. struct file *file;
  2505. u32 sid = task_sid(tsk);
  2506. u32 perm;
  2507. struct file_security_struct *fsec;
  2508. file = container_of(fown, struct file, f_owner);
  2509. fsec = file->f_security;
  2510. if (!signum)
  2511. perm = signal_to_av(SIGIO);
  2512. else
  2513. perm = signal_to_av(signum);
  2514. return avc_has_perm(fsec->fown_sid, sid,
  2515. SECCLASS_PROCESS, perm, NULL);
  2516. }
  2517. static int selinux_file_receive(struct file *file)
  2518. {
  2519. const struct cred *cred = current_cred();
  2520. return file_has_perm(cred, file, file_to_av(file));
  2521. }
  2522. static int selinux_dentry_open(struct file *file, const struct cred *cred)
  2523. {
  2524. struct file_security_struct *fsec;
  2525. struct inode *inode;
  2526. struct inode_security_struct *isec;
  2527. inode = file->f_path.dentry->d_inode;
  2528. fsec = file->f_security;
  2529. isec = inode->i_security;
  2530. fsec->isid = isec->sid;
  2531. fsec->pseqno = avc_policy_seqno();
  2532. return inode_has_perm_noadp(cred, inode, open_file_to_av(file), 0);
  2533. }
  2534. static int selinux_task_create(unsigned long clone_flags)
  2535. {
  2536. return current_has_perm(current, PROCESS__FORK);
  2537. }
  2538. static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
  2539. {
  2540. struct task_security_struct *tsec;
  2541. tsec = kzalloc(sizeof(struct task_security_struct), gfp);
  2542. if (!tsec)
  2543. return -ENOMEM;
  2544. cred->security = tsec;
  2545. return 0;
  2546. }
  2547. static void selinux_cred_free(struct cred *cred)
  2548. {
  2549. struct task_security_struct *tsec = cred->security;
  2550. BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
  2551. cred->security = (void *) 0x7UL;
  2552. kfree(tsec);
  2553. }
  2554. static int selinux_cred_prepare(struct cred *new, const struct cred *old,
  2555. gfp_t gfp)
  2556. {
  2557. const struct task_security_struct *old_tsec;
  2558. struct task_security_struct *tsec;
  2559. old_tsec = old->security;
  2560. tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
  2561. if (!tsec)
  2562. return -ENOMEM;
  2563. new->security = tsec;
  2564. return 0;
  2565. }
  2566. static void selinux_cred_transfer(struct cred *new, const struct cred *old)
  2567. {
  2568. const struct task_security_struct *old_tsec = old->security;
  2569. struct task_security_struct *tsec = new->security;
  2570. *tsec = *old_tsec;
  2571. }
  2572. static int selinux_kernel_act_as(struct cred *new, u32 secid)
  2573. {
  2574. struct task_security_struct *tsec = new->security;
  2575. u32 sid = current_sid();
  2576. int ret;
  2577. ret = avc_has_perm(sid, secid,
  2578. SECCLASS_KERNEL_SERVICE,
  2579. KERNEL_SERVICE__USE_AS_OVERRIDE,
  2580. NULL);
  2581. if (ret == 0) {
  2582. tsec->sid = secid;
  2583. tsec->create_sid = 0;
  2584. tsec->keycreate_sid = 0;
  2585. tsec->sockcreate_sid = 0;
  2586. }
  2587. return ret;
  2588. }
  2589. static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
  2590. {
  2591. struct inode_security_struct *isec = inode->i_security;
  2592. struct task_security_struct *tsec = new->security;
  2593. u32 sid = current_sid();
  2594. int ret;
  2595. ret = avc_has_perm(sid, isec->sid,
  2596. SECCLASS_KERNEL_SERVICE,
  2597. KERNEL_SERVICE__CREATE_FILES_AS,
  2598. NULL);
  2599. if (ret == 0)
  2600. tsec->create_sid = isec->sid;
  2601. return ret;
  2602. }
  2603. static int selinux_kernel_module_request(char *kmod_name)
  2604. {
  2605. u32 sid;
  2606. struct common_audit_data ad;
  2607. struct selinux_audit_data sad = {0,};
  2608. sid = task_sid(current);
  2609. COMMON_AUDIT_DATA_INIT(&ad, KMOD);
  2610. ad.selinux_audit_data = &sad;
  2611. ad.u.kmod_name = kmod_name;
  2612. return avc_has_perm(sid, SECINITSID_KERNEL, SECCLASS_SYSTEM,
  2613. SYSTEM__MODULE_REQUEST, &ad);
  2614. }
  2615. static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
  2616. {
  2617. return current_has_perm(p, PROCESS__SETPGID);
  2618. }
  2619. static int selinux_task_getpgid(struct task_struct *p)
  2620. {
  2621. return current_has_perm(p, PROCESS__GETPGID);
  2622. }
  2623. static int selinux_task_getsid(struct task_struct *p)
  2624. {
  2625. return current_has_perm(p, PROCESS__GETSESSION);
  2626. }
  2627. static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
  2628. {
  2629. *secid = task_sid(p);
  2630. }
  2631. static int selinux_task_setnice(struct task_struct *p, int nice)
  2632. {
  2633. int rc;
  2634. rc = cap_task_setnice(p, nice);
  2635. if (rc)
  2636. return rc;
  2637. return current_has_perm(p, PROCESS__SETSCHED);
  2638. }
  2639. static int selinux_task_setioprio(struct task_struct *p, int ioprio)
  2640. {
  2641. int rc;
  2642. rc = cap_task_setioprio(p, ioprio);
  2643. if (rc)
  2644. return rc;
  2645. return current_has_perm(p, PROCESS__SETSCHED);
  2646. }
  2647. static int selinux_task_getioprio(struct task_struct *p)
  2648. {
  2649. return current_has_perm(p, PROCESS__GETSCHED);
  2650. }
  2651. static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
  2652. struct rlimit *new_rlim)
  2653. {
  2654. struct rlimit *old_rlim = p->signal->rlim + resource;
  2655. if (old_rlim->rlim_max != new_rlim->rlim_max)
  2656. return current_has_perm(p, PROCESS__SETRLIMIT);
  2657. return 0;
  2658. }
  2659. static int selinux_task_setscheduler(struct task_struct *p)
  2660. {
  2661. int rc;
  2662. rc = cap_task_setscheduler(p);
  2663. if (rc)
  2664. return rc;
  2665. return current_has_perm(p, PROCESS__SETSCHED);
  2666. }
  2667. static int selinux_task_getscheduler(struct task_struct *p)
  2668. {
  2669. return current_has_perm(p, PROCESS__GETSCHED);
  2670. }
  2671. static int selinux_task_movememory(struct task_struct *p)
  2672. {
  2673. return current_has_perm(p, PROCESS__SETSCHED);
  2674. }
  2675. static int selinux_task_kill(struct task_struct *p, struct siginfo *info,
  2676. int sig, u32 secid)
  2677. {
  2678. u32 perm;
  2679. int rc;
  2680. if (!sig)
  2681. perm = PROCESS__SIGNULL;
  2682. else
  2683. perm = signal_to_av(sig);
  2684. if (secid)
  2685. rc = avc_has_perm(secid, task_sid(p),
  2686. SECCLASS_PROCESS, perm, NULL);
  2687. else
  2688. rc = current_has_perm(p, perm);
  2689. return rc;
  2690. }
  2691. static int selinux_task_wait(struct task_struct *p)
  2692. {
  2693. return task_has_perm(p, current, PROCESS__SIGCHLD);
  2694. }
  2695. static void selinux_task_to_inode(struct task_struct *p,
  2696. struct inode *inode)
  2697. {
  2698. struct inode_security_struct *isec = inode->i_security;
  2699. u32 sid = task_sid(p);
  2700. isec->sid = sid;
  2701. isec->initialized = 1;
  2702. }
  2703. static int selinux_parse_skb_ipv4(struct sk_buff *skb,
  2704. struct common_audit_data *ad, u8 *proto)
  2705. {
  2706. int offset, ihlen, ret = -EINVAL;
  2707. struct iphdr _iph, *ih;
  2708. offset = skb_network_offset(skb);
  2709. ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
  2710. if (ih == NULL)
  2711. goto out;
  2712. ihlen = ih->ihl * 4;
  2713. if (ihlen < sizeof(_iph))
  2714. goto out;
  2715. ad->u.net->v4info.saddr = ih->saddr;
  2716. ad->u.net->v4info.daddr = ih->daddr;
  2717. ret = 0;
  2718. if (proto)
  2719. *proto = ih->protocol;
  2720. switch (ih->protocol) {
  2721. case IPPROTO_TCP: {
  2722. struct tcphdr _tcph, *th;
  2723. if (ntohs(ih->frag_off) & IP_OFFSET)
  2724. break;
  2725. offset += ihlen;
  2726. th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
  2727. if (th == NULL)
  2728. break;
  2729. ad->u.net->sport = th->source;
  2730. ad->u.net->dport = th->dest;
  2731. break;
  2732. }
  2733. case IPPROTO_UDP: {
  2734. struct udphdr _udph, *uh;
  2735. if (ntohs(ih->frag_off) & IP_OFFSET)
  2736. break;
  2737. offset += ihlen;
  2738. uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
  2739. if (uh == NULL)
  2740. break;
  2741. ad->u.net->sport = uh->source;
  2742. ad->u.net->dport = uh->dest;
  2743. break;
  2744. }
  2745. case IPPROTO_DCCP: {
  2746. struct dccp_hdr _dccph, *dh;
  2747. if (ntohs(ih->frag_off) & IP_OFFSET)
  2748. break;
  2749. offset += ihlen;
  2750. dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
  2751. if (dh == NULL)
  2752. break;
  2753. ad->u.net->sport = dh->dccph_sport;
  2754. ad->u.net->dport = dh->dccph_dport;
  2755. break;
  2756. }
  2757. default:
  2758. break;
  2759. }
  2760. out:
  2761. return ret;
  2762. }
  2763. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  2764. static int selinux_parse_skb_ipv6(struct sk_buff *skb,
  2765. struct common_audit_data *ad, u8 *proto)
  2766. {
  2767. u8 nexthdr;
  2768. int ret = -EINVAL, offset;
  2769. struct ipv6hdr _ipv6h, *ip6;
  2770. __be16 frag_off;
  2771. offset = skb_network_offset(skb);
  2772. ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
  2773. if (ip6 == NULL)
  2774. goto out;
  2775. ad->u.net->v6info.saddr = ip6->saddr;
  2776. ad->u.net->v6info.daddr = ip6->daddr;
  2777. ret = 0;
  2778. nexthdr = ip6->nexthdr;
  2779. offset += sizeof(_ipv6h);
  2780. offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
  2781. if (offset < 0)
  2782. goto out;
  2783. if (proto)
  2784. *proto = nexthdr;
  2785. switch (nexthdr) {
  2786. case IPPROTO_TCP: {
  2787. struct tcphdr _tcph, *th;
  2788. th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
  2789. if (th == NULL)
  2790. break;
  2791. ad->u.net->sport = th->source;
  2792. ad->u.net->dport = th->dest;
  2793. break;
  2794. }
  2795. case IPPROTO_UDP: {
  2796. struct udphdr _udph, *uh;
  2797. uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
  2798. if (uh == NULL)
  2799. break;
  2800. ad->u.net->sport = uh->source;
  2801. ad->u.net->dport = uh->dest;
  2802. break;
  2803. }
  2804. case IPPROTO_DCCP: {
  2805. struct dccp_hdr _dccph, *dh;
  2806. dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
  2807. if (dh == NULL)
  2808. break;
  2809. ad->u.net->sport = dh->dccph_sport;
  2810. ad->u.net->dport = dh->dccph_dport;
  2811. break;
  2812. }
  2813. default:
  2814. break;
  2815. }
  2816. out:
  2817. return ret;
  2818. }
  2819. #endif
  2820. static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
  2821. char **_addrp, int src, u8 *proto)
  2822. {
  2823. char *addrp;
  2824. int ret;
  2825. switch (ad->u.net->family) {
  2826. case PF_INET:
  2827. ret = selinux_parse_skb_ipv4(skb, ad, proto);
  2828. if (ret)
  2829. goto parse_error;
  2830. addrp = (char *)(src ? &ad->u.net->v4info.saddr :
  2831. &ad->u.net->v4info.daddr);
  2832. goto okay;
  2833. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  2834. case PF_INET6:
  2835. ret = selinux_parse_skb_ipv6(skb, ad, proto);
  2836. if (ret)
  2837. goto parse_error;
  2838. addrp = (char *)(src ? &ad->u.net->v6info.saddr :
  2839. &ad->u.net->v6info.daddr);
  2840. goto okay;
  2841. #endif
  2842. default:
  2843. addrp = NULL;
  2844. goto okay;
  2845. }
  2846. parse_error:
  2847. printk(KERN_WARNING
  2848. "SELinux: failure in selinux_parse_skb(),"
  2849. " unable to parse packet\n");
  2850. return ret;
  2851. okay:
  2852. if (_addrp)
  2853. *_addrp = addrp;
  2854. return 0;
  2855. }
  2856. static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
  2857. {
  2858. int err;
  2859. u32 xfrm_sid;
  2860. u32 nlbl_sid;
  2861. u32 nlbl_type;
  2862. selinux_skb_xfrm_sid(skb, &xfrm_sid);
  2863. selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
  2864. err = security_net_peersid_resolve(nlbl_sid, nlbl_type, xfrm_sid, sid);
  2865. if (unlikely(err)) {
  2866. printk(KERN_WARNING
  2867. "SELinux: failure in selinux_skb_peerlbl_sid(),"
  2868. " unable to determine packet's peer label\n");
  2869. return -EACCES;
  2870. }
  2871. return 0;
  2872. }
  2873. static int socket_sockcreate_sid(const struct task_security_struct *tsec,
  2874. u16 secclass, u32 *socksid)
  2875. {
  2876. if (tsec->sockcreate_sid > SECSID_NULL) {
  2877. *socksid = tsec->sockcreate_sid;
  2878. return 0;
  2879. }
  2880. return security_transition_sid(tsec->sid, tsec->sid, secclass, NULL,
  2881. socksid);
  2882. }
  2883. static int sock_has_perm(struct task_struct *task, struct sock *sk, u32 perms)
  2884. {
  2885. struct sk_security_struct *sksec = sk->sk_security;
  2886. struct common_audit_data ad;
  2887. struct selinux_audit_data sad = {0,};
  2888. struct lsm_network_audit net = {0,};
  2889. u32 tsid = task_sid(task);
  2890. if (unlikely(sksec == NULL)) {
  2891. printk(KERN_WARNING "SELinux: sksec is NULL, return 0 to aovid access sksec this NULL pointer");
  2892. return 0;
  2893. }
  2894. if (sksec->sid == SECINITSID_KERNEL)
  2895. return 0;
  2896. COMMON_AUDIT_DATA_INIT(&ad, NET);
  2897. ad.selinux_audit_data = &sad;
  2898. ad.u.net = &net;
  2899. ad.u.net->sk = sk;
  2900. return avc_has_perm(tsid, sksec->sid, sksec->sclass, perms, &ad);
  2901. }
  2902. static int selinux_socket_create(int family, int type,
  2903. int protocol, int kern)
  2904. {
  2905. const struct task_security_struct *tsec = current_security();
  2906. u32 newsid;
  2907. u16 secclass;
  2908. int rc;
  2909. if (kern)
  2910. return 0;
  2911. secclass = socket_type_to_security_class(family, type, protocol);
  2912. rc = socket_sockcreate_sid(tsec, secclass, &newsid);
  2913. if (rc)
  2914. return rc;
  2915. return avc_has_perm(tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
  2916. }
  2917. static int selinux_socket_post_create(struct socket *sock, int family,
  2918. int type, int protocol, int kern)
  2919. {
  2920. const struct task_security_struct *tsec = current_security();
  2921. struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
  2922. struct sk_security_struct *sksec;
  2923. int err = 0;
  2924. isec->sclass = socket_type_to_security_class(family, type, protocol);
  2925. if (kern)
  2926. isec->sid = SECINITSID_KERNEL;
  2927. else {
  2928. err = socket_sockcreate_sid(tsec, isec->sclass, &(isec->sid));
  2929. if (err)
  2930. return err;
  2931. }
  2932. isec->initialized = 1;
  2933. if (sock->sk) {
  2934. sksec = sock->sk->sk_security;
  2935. sksec->sid = isec->sid;
  2936. sksec->sclass = isec->sclass;
  2937. err = selinux_netlbl_socket_post_create(sock->sk, family);
  2938. }
  2939. return err;
  2940. }
  2941. static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
  2942. {
  2943. struct sock *sk = sock->sk;
  2944. u16 family;
  2945. int err;
  2946. err = sock_has_perm(current, sk, SOCKET__BIND);
  2947. if (err)
  2948. goto out;
  2949. family = sk->sk_family;
  2950. if (family == PF_INET || family == PF_INET6) {
  2951. char *addrp;
  2952. struct sk_security_struct *sksec = sk->sk_security;
  2953. struct common_audit_data ad;
  2954. struct selinux_audit_data sad = {0,};
  2955. struct lsm_network_audit net = {0,};
  2956. struct sockaddr_in *addr4 = NULL;
  2957. struct sockaddr_in6 *addr6 = NULL;
  2958. unsigned short snum;
  2959. u32 sid, node_perm;
  2960. if (family == PF_INET) {
  2961. addr4 = (struct sockaddr_in *)address;
  2962. snum = ntohs(addr4->sin_port);
  2963. addrp = (char *)&addr4->sin_addr.s_addr;
  2964. } else {
  2965. addr6 = (struct sockaddr_in6 *)address;
  2966. snum = ntohs(addr6->sin6_port);
  2967. addrp = (char *)&addr6->sin6_addr.s6_addr;
  2968. }
  2969. if (snum) {
  2970. int low, high;
  2971. inet_get_local_port_range(&low, &high);
  2972. if (snum < max(PROT_SOCK, low) || snum > high) {
  2973. err = sel_netport_sid(sk->sk_protocol,
  2974. snum, &sid);
  2975. if (err)
  2976. goto out;
  2977. COMMON_AUDIT_DATA_INIT(&ad, NET);
  2978. ad.selinux_audit_data = &sad;
  2979. ad.u.net = &net;
  2980. ad.u.net->sport = htons(snum);
  2981. ad.u.net->family = family;
  2982. err = avc_has_perm(sksec->sid, sid,
  2983. sksec->sclass,
  2984. SOCKET__NAME_BIND, &ad);
  2985. if (err)
  2986. goto out;
  2987. }
  2988. }
  2989. switch (sksec->sclass) {
  2990. case SECCLASS_TCP_SOCKET:
  2991. node_perm = TCP_SOCKET__NODE_BIND;
  2992. break;
  2993. case SECCLASS_UDP_SOCKET:
  2994. node_perm = UDP_SOCKET__NODE_BIND;
  2995. break;
  2996. case SECCLASS_DCCP_SOCKET:
  2997. node_perm = DCCP_SOCKET__NODE_BIND;
  2998. break;
  2999. default:
  3000. node_perm = RAWIP_SOCKET__NODE_BIND;
  3001. break;
  3002. }
  3003. err = sel_netnode_sid(addrp, family, &sid);
  3004. if (err)
  3005. goto out;
  3006. COMMON_AUDIT_DATA_INIT(&ad, NET);
  3007. ad.selinux_audit_data = &sad;
  3008. ad.u.net = &net;
  3009. ad.u.net->sport = htons(snum);
  3010. ad.u.net->family = family;
  3011. if (family == PF_INET)
  3012. ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
  3013. else
  3014. ad.u.net->v6info.saddr = addr6->sin6_addr;
  3015. err = avc_has_perm(sksec->sid, sid,
  3016. sksec->sclass, node_perm, &ad);
  3017. if (err)
  3018. goto out;
  3019. }
  3020. out:
  3021. return err;
  3022. }
  3023. static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
  3024. {
  3025. struct sock *sk = sock->sk;
  3026. struct sk_security_struct *sksec = sk->sk_security;
  3027. int err;
  3028. err = sock_has_perm(current, sk, SOCKET__CONNECT);
  3029. if (err)
  3030. return err;
  3031. if (sksec->sclass == SECCLASS_TCP_SOCKET ||
  3032. sksec->sclass == SECCLASS_DCCP_SOCKET) {
  3033. struct common_audit_data ad;
  3034. struct selinux_audit_data sad = {0,};
  3035. struct lsm_network_audit net = {0,};
  3036. struct sockaddr_in *addr4 = NULL;
  3037. struct sockaddr_in6 *addr6 = NULL;
  3038. unsigned short snum;
  3039. u32 sid, perm;
  3040. if (sk->sk_family == PF_INET) {
  3041. addr4 = (struct sockaddr_in *)address;
  3042. if (addrlen < sizeof(struct sockaddr_in))
  3043. return -EINVAL;
  3044. snum = ntohs(addr4->sin_port);
  3045. } else {
  3046. addr6 = (struct sockaddr_in6 *)address;
  3047. if (addrlen < SIN6_LEN_RFC2133)
  3048. return -EINVAL;
  3049. snum = ntohs(addr6->sin6_port);
  3050. }
  3051. err = sel_netport_sid(sk->sk_protocol, snum, &sid);
  3052. if (err)
  3053. goto out;
  3054. perm = (sksec->sclass == SECCLASS_TCP_SOCKET) ?
  3055. TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT;
  3056. COMMON_AUDIT_DATA_INIT(&ad, NET);
  3057. ad.selinux_audit_data = &sad;
  3058. ad.u.net = &net;
  3059. ad.u.net->dport = htons(snum);
  3060. ad.u.net->family = sk->sk_family;
  3061. err = avc_has_perm(sksec->sid, sid, sksec->sclass, perm, &ad);
  3062. if (err)
  3063. goto out;
  3064. }
  3065. err = selinux_netlbl_socket_connect(sk, address);
  3066. out:
  3067. return err;
  3068. }
  3069. static int selinux_socket_listen(struct socket *sock, int backlog)
  3070. {
  3071. return sock_has_perm(current, sock->sk, SOCKET__LISTEN);
  3072. }
  3073. static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
  3074. {
  3075. int err;
  3076. struct inode_security_struct *isec;
  3077. struct inode_security_struct *newisec;
  3078. err = sock_has_perm(current, sock->sk, SOCKET__ACCEPT);
  3079. if (err)
  3080. return err;
  3081. newisec = SOCK_INODE(newsock)->i_security;
  3082. isec = SOCK_INODE(sock)->i_security;
  3083. newisec->sclass = isec->sclass;
  3084. newisec->sid = isec->sid;
  3085. newisec->initialized = 1;
  3086. return 0;
  3087. }
  3088. static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
  3089. int size)
  3090. {
  3091. return sock_has_perm(current, sock->sk, SOCKET__WRITE);
  3092. }
  3093. static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
  3094. int size, int flags)
  3095. {
  3096. return sock_has_perm(current, sock->sk, SOCKET__READ);
  3097. }
  3098. static int selinux_socket_getsockname(struct socket *sock)
  3099. {
  3100. return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
  3101. }
  3102. static int selinux_socket_getpeername(struct socket *sock)
  3103. {
  3104. return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
  3105. }
  3106. static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
  3107. {
  3108. int err;
  3109. err = sock_has_perm(current, sock->sk, SOCKET__SETOPT);
  3110. if (err)
  3111. return err;
  3112. return selinux_netlbl_socket_setsockopt(sock, level, optname);
  3113. }
  3114. static int selinux_socket_getsockopt(struct socket *sock, int level,
  3115. int optname)
  3116. {
  3117. return sock_has_perm(current, sock->sk, SOCKET__GETOPT);
  3118. }
  3119. static int selinux_socket_shutdown(struct socket *sock, int how)
  3120. {
  3121. return sock_has_perm(current, sock->sk, SOCKET__SHUTDOWN);
  3122. }
  3123. static int selinux_socket_unix_stream_connect(struct sock *sock,
  3124. struct sock *other,
  3125. struct sock *newsk)
  3126. {
  3127. struct sk_security_struct *sksec_sock = sock->sk_security;
  3128. struct sk_security_struct *sksec_other = other->sk_security;
  3129. struct sk_security_struct *sksec_new = newsk->sk_security;
  3130. struct common_audit_data ad;
  3131. struct selinux_audit_data sad = {0,};
  3132. struct lsm_network_audit net = {0,};
  3133. int err;
  3134. COMMON_AUDIT_DATA_INIT(&ad, NET);
  3135. ad.selinux_audit_data = &sad;
  3136. ad.u.net = &net;
  3137. ad.u.net->sk = other;
  3138. err = avc_has_perm(sksec_sock->sid, sksec_other->sid,
  3139. sksec_other->sclass,
  3140. UNIX_STREAM_SOCKET__CONNECTTO, &ad);
  3141. if (err)
  3142. return err;
  3143. sksec_new->peer_sid = sksec_sock->sid;
  3144. err = security_sid_mls_copy(sksec_other->sid, sksec_sock->sid,
  3145. &sksec_new->sid);
  3146. if (err)
  3147. return err;
  3148. sksec_sock->peer_sid = sksec_new->sid;
  3149. return 0;
  3150. }
  3151. static int selinux_socket_unix_may_send(struct socket *sock,
  3152. struct socket *other)
  3153. {
  3154. struct sk_security_struct *ssec = sock->sk->sk_security;
  3155. struct sk_security_struct *osec = other->sk->sk_security;
  3156. struct common_audit_data ad;
  3157. struct selinux_audit_data sad = {0,};
  3158. struct lsm_network_audit net = {0,};
  3159. COMMON_AUDIT_DATA_INIT(&ad, NET);
  3160. ad.selinux_audit_data = &sad;
  3161. ad.u.net = &net;
  3162. ad.u.net->sk = other->sk;
  3163. return avc_has_perm(ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
  3164. &ad);
  3165. }
  3166. static int selinux_inet_sys_rcv_skb(int ifindex, char *addrp, u16 family,
  3167. u32 peer_sid,
  3168. struct common_audit_data *ad)
  3169. {
  3170. int err;
  3171. u32 if_sid;
  3172. u32 node_sid;
  3173. err = sel_netif_sid(ifindex, &if_sid);
  3174. if (err)
  3175. return err;
  3176. err = avc_has_perm(peer_sid, if_sid,
  3177. SECCLASS_NETIF, NETIF__INGRESS, ad);
  3178. if (err)
  3179. return err;
  3180. err = sel_netnode_sid(addrp, family, &node_sid);
  3181. if (err)
  3182. return err;
  3183. return avc_has_perm(peer_sid, node_sid,
  3184. SECCLASS_NODE, NODE__RECVFROM, ad);
  3185. }
  3186. static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
  3187. u16 family)
  3188. {
  3189. int err = 0;
  3190. struct sk_security_struct *sksec = sk->sk_security;
  3191. u32 sk_sid = sksec->sid;
  3192. struct common_audit_data ad;
  3193. struct selinux_audit_data sad = {0,};
  3194. struct lsm_network_audit net = {0,};
  3195. char *addrp;
  3196. COMMON_AUDIT_DATA_INIT(&ad, NET);
  3197. ad.selinux_audit_data = &sad;
  3198. ad.u.net = &net;
  3199. ad.u.net->netif = skb->skb_iif;
  3200. ad.u.net->family = family;
  3201. err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
  3202. if (err)
  3203. return err;
  3204. if (selinux_secmark_enabled()) {
  3205. err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
  3206. PACKET__RECV, &ad);
  3207. if (err)
  3208. return err;
  3209. }
  3210. err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
  3211. if (err)
  3212. return err;
  3213. err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
  3214. return err;
  3215. }
  3216. static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
  3217. {
  3218. int err;
  3219. struct sk_security_struct *sksec = sk->sk_security;
  3220. u16 family = sk->sk_family;
  3221. u32 sk_sid = sksec->sid;
  3222. struct common_audit_data ad;
  3223. struct selinux_audit_data sad = {0,};
  3224. struct lsm_network_audit net = {0,};
  3225. char *addrp;
  3226. u8 secmark_active;
  3227. u8 peerlbl_active;
  3228. if (family != PF_INET && family != PF_INET6)
  3229. return 0;
  3230. if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
  3231. family = PF_INET;
  3232. if (!selinux_policycap_netpeer)
  3233. return selinux_sock_rcv_skb_compat(sk, skb, family);
  3234. secmark_active = selinux_secmark_enabled();
  3235. peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled();
  3236. if (!secmark_active && !peerlbl_active)
  3237. return 0;
  3238. COMMON_AUDIT_DATA_INIT(&ad, NET);
  3239. ad.selinux_audit_data = &sad;
  3240. ad.u.net = &net;
  3241. ad.u.net->netif = skb->skb_iif;
  3242. ad.u.net->family = family;
  3243. err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
  3244. if (err)
  3245. return err;
  3246. if (peerlbl_active) {
  3247. u32 peer_sid;
  3248. err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
  3249. if (err)
  3250. return err;
  3251. err = selinux_inet_sys_rcv_skb(skb->skb_iif, addrp, family,
  3252. peer_sid, &ad);
  3253. if (err) {
  3254. selinux_netlbl_err(skb, err, 0);
  3255. return err;
  3256. }
  3257. err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER,
  3258. PEER__RECV, &ad);
  3259. if (err)
  3260. selinux_netlbl_err(skb, err, 0);
  3261. }
  3262. if (secmark_active) {
  3263. err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
  3264. PACKET__RECV, &ad);
  3265. if (err)
  3266. return err;
  3267. }
  3268. return err;
  3269. }
  3270. static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
  3271. int __user *optlen, unsigned len)
  3272. {
  3273. int err = 0;
  3274. char *scontext;
  3275. u32 scontext_len;
  3276. struct sk_security_struct *sksec = sock->sk->sk_security;
  3277. u32 peer_sid = SECSID_NULL;
  3278. if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
  3279. sksec->sclass == SECCLASS_TCP_SOCKET)
  3280. peer_sid = sksec->peer_sid;
  3281. if (peer_sid == SECSID_NULL)
  3282. return -ENOPROTOOPT;
  3283. err = security_sid_to_context(peer_sid, &scontext, &scontext_len);
  3284. if (err)
  3285. return err;
  3286. if (scontext_len > len) {
  3287. err = -ERANGE;
  3288. goto out_len;
  3289. }
  3290. if (copy_to_user(optval, scontext, scontext_len))
  3291. err = -EFAULT;
  3292. out_len:
  3293. if (put_user(scontext_len, optlen))
  3294. err = -EFAULT;
  3295. kfree(scontext);
  3296. return err;
  3297. }
  3298. static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
  3299. {
  3300. u32 peer_secid = SECSID_NULL;
  3301. u16 family;
  3302. if (skb && skb->protocol == htons(ETH_P_IP))
  3303. family = PF_INET;
  3304. else if (skb && skb->protocol == htons(ETH_P_IPV6))
  3305. family = PF_INET6;
  3306. else if (sock)
  3307. family = sock->sk->sk_family;
  3308. else
  3309. goto out;
  3310. if (sock && family == PF_UNIX)
  3311. selinux_inode_getsecid(SOCK_INODE(sock), &peer_secid);
  3312. else if (skb)
  3313. selinux_skb_peerlbl_sid(skb, family, &peer_secid);
  3314. out:
  3315. *secid = peer_secid;
  3316. if (peer_secid == SECSID_NULL)
  3317. return -EINVAL;
  3318. return 0;
  3319. }
  3320. static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
  3321. {
  3322. struct sk_security_struct *sksec;
  3323. sksec = kzalloc(sizeof(*sksec), priority);
  3324. if (!sksec)
  3325. return -ENOMEM;
  3326. sksec->peer_sid = SECINITSID_UNLABELED;
  3327. sksec->sid = SECINITSID_UNLABELED;
  3328. selinux_netlbl_sk_security_reset(sksec);
  3329. sk->sk_security = sksec;
  3330. return 0;
  3331. }
  3332. static void selinux_sk_free_security(struct sock *sk)
  3333. {
  3334. struct sk_security_struct *sksec = sk->sk_security;
  3335. sk->sk_security = NULL;
  3336. selinux_netlbl_sk_security_free(sksec);
  3337. kfree(sksec);
  3338. }
  3339. static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
  3340. {
  3341. struct sk_security_struct *sksec = sk->sk_security;
  3342. struct sk_security_struct *newsksec = newsk->sk_security;
  3343. newsksec->sid = sksec->sid;
  3344. newsksec->peer_sid = sksec->peer_sid;
  3345. newsksec->sclass = sksec->sclass;
  3346. selinux_netlbl_sk_security_reset(newsksec);
  3347. }
  3348. static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
  3349. {
  3350. if (!sk)
  3351. *secid = SECINITSID_ANY_SOCKET;
  3352. else {
  3353. struct sk_security_struct *sksec = sk->sk_security;
  3354. *secid = sksec->sid;
  3355. }
  3356. }
  3357. static void selinux_sock_graft(struct sock *sk, struct socket *parent)
  3358. {
  3359. struct inode_security_struct *isec = SOCK_INODE(parent)->i_security;
  3360. struct sk_security_struct *sksec = sk->sk_security;
  3361. if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
  3362. sk->sk_family == PF_UNIX)
  3363. isec->sid = sksec->sid;
  3364. sksec->sclass = isec->sclass;
  3365. }
  3366. static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
  3367. struct request_sock *req)
  3368. {
  3369. struct sk_security_struct *sksec = sk->sk_security;
  3370. int err;
  3371. u16 family = sk->sk_family;
  3372. u32 newsid;
  3373. u32 peersid;
  3374. if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
  3375. family = PF_INET;
  3376. err = selinux_skb_peerlbl_sid(skb, family, &peersid);
  3377. if (err)
  3378. return err;
  3379. if (peersid == SECSID_NULL) {
  3380. req->secid = sksec->sid;
  3381. req->peer_secid = SECSID_NULL;
  3382. } else {
  3383. err = security_sid_mls_copy(sksec->sid, peersid, &newsid);
  3384. if (err)
  3385. return err;
  3386. req->secid = newsid;
  3387. req->peer_secid = peersid;
  3388. }
  3389. return selinux_netlbl_inet_conn_request(req, family);
  3390. }
  3391. static void selinux_inet_csk_clone(struct sock *newsk,
  3392. const struct request_sock *req)
  3393. {
  3394. struct sk_security_struct *newsksec = newsk->sk_security;
  3395. newsksec->sid = req->secid;
  3396. newsksec->peer_sid = req->peer_secid;
  3397. selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
  3398. }
  3399. static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
  3400. {
  3401. u16 family = sk->sk_family;
  3402. struct sk_security_struct *sksec = sk->sk_security;
  3403. if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
  3404. family = PF_INET;
  3405. selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
  3406. }
  3407. static int selinux_secmark_relabel_packet(u32 sid)
  3408. {
  3409. const struct task_security_struct *__tsec;
  3410. u32 tsid;
  3411. __tsec = current_security();
  3412. tsid = __tsec->sid;
  3413. return avc_has_perm(tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, NULL);
  3414. }
  3415. static void selinux_secmark_refcount_inc(void)
  3416. {
  3417. atomic_inc(&selinux_secmark_refcount);
  3418. }
  3419. static void selinux_secmark_refcount_dec(void)
  3420. {
  3421. atomic_dec(&selinux_secmark_refcount);
  3422. }
  3423. static void selinux_req_classify_flow(const struct request_sock *req,
  3424. struct flowi *fl)
  3425. {
  3426. fl->flowi_secid = req->secid;
  3427. }
  3428. static int selinux_tun_dev_create(void)
  3429. {
  3430. u32 sid = current_sid();
  3431. return avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
  3432. NULL);
  3433. }
  3434. static void selinux_tun_dev_post_create(struct sock *sk)
  3435. {
  3436. struct sk_security_struct *sksec = sk->sk_security;
  3437. sksec->sid = current_sid();
  3438. sksec->sclass = SECCLASS_TUN_SOCKET;
  3439. }
  3440. static int selinux_tun_dev_attach(struct sock *sk)
  3441. {
  3442. struct sk_security_struct *sksec = sk->sk_security;
  3443. u32 sid = current_sid();
  3444. int err;
  3445. err = avc_has_perm(sid, sksec->sid, SECCLASS_TUN_SOCKET,
  3446. TUN_SOCKET__RELABELFROM, NULL);
  3447. if (err)
  3448. return err;
  3449. err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET,
  3450. TUN_SOCKET__RELABELTO, NULL);
  3451. if (err)
  3452. return err;
  3453. sksec->sid = sid;
  3454. return 0;
  3455. }
  3456. static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
  3457. {
  3458. int err = 0;
  3459. u32 perm;
  3460. struct nlmsghdr *nlh;
  3461. struct sk_security_struct *sksec = sk->sk_security;
  3462. if (skb->len < NLMSG_SPACE(0)) {
  3463. err = -EINVAL;
  3464. goto out;
  3465. }
  3466. nlh = nlmsg_hdr(skb);
  3467. err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
  3468. if (err) {
  3469. if (err == -EINVAL) {
  3470. audit_log(current->audit_context, GFP_KERNEL, AUDIT_SELINUX_ERR,
  3471. "SELinux: unrecognized netlink message"
  3472. " type=%hu for sclass=%hu\n",
  3473. nlh->nlmsg_type, sksec->sclass);
  3474. if (!selinux_enforcing || security_get_allow_unknown())
  3475. err = 0;
  3476. }
  3477. if (err == -ENOENT)
  3478. err = 0;
  3479. goto out;
  3480. }
  3481. err = sock_has_perm(current, sk, perm);
  3482. out:
  3483. return err;
  3484. }
  3485. #ifdef CONFIG_NETFILTER
  3486. static unsigned int selinux_ip_forward(struct sk_buff *skb, int ifindex,
  3487. u16 family)
  3488. {
  3489. int err;
  3490. char *addrp;
  3491. u32 peer_sid;
  3492. struct common_audit_data ad;
  3493. struct selinux_audit_data sad = {0,};
  3494. struct lsm_network_audit net = {0,};
  3495. u8 secmark_active;
  3496. u8 netlbl_active;
  3497. u8 peerlbl_active;
  3498. if (!selinux_policycap_netpeer)
  3499. return NF_ACCEPT;
  3500. secmark_active = selinux_secmark_enabled();
  3501. netlbl_active = netlbl_enabled();
  3502. peerlbl_active = netlbl_active || selinux_xfrm_enabled();
  3503. if (!secmark_active && !peerlbl_active)
  3504. return NF_ACCEPT;
  3505. if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
  3506. return NF_DROP;
  3507. COMMON_AUDIT_DATA_INIT(&ad, NET);
  3508. ad.selinux_audit_data = &sad;
  3509. ad.u.net = &net;
  3510. ad.u.net->netif = ifindex;
  3511. ad.u.net->family = family;
  3512. if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
  3513. return NF_DROP;
  3514. if (peerlbl_active) {
  3515. err = selinux_inet_sys_rcv_skb(ifindex, addrp, family,
  3516. peer_sid, &ad);
  3517. if (err) {
  3518. selinux_netlbl_err(skb, err, 1);
  3519. return NF_DROP;
  3520. }
  3521. }
  3522. if (secmark_active)
  3523. if (avc_has_perm(peer_sid, skb->secmark,
  3524. SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
  3525. return NF_DROP;
  3526. if (netlbl_active)
  3527. if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
  3528. return NF_DROP;
  3529. return NF_ACCEPT;
  3530. }
  3531. static unsigned int selinux_ipv4_forward(unsigned int hooknum,
  3532. struct sk_buff *skb,
  3533. const struct net_device *in,
  3534. const struct net_device *out,
  3535. int (*okfn)(struct sk_buff *))
  3536. {
  3537. return selinux_ip_forward(skb, in->ifindex, PF_INET);
  3538. }
  3539. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  3540. static unsigned int selinux_ipv6_forward(unsigned int hooknum,
  3541. struct sk_buff *skb,
  3542. const struct net_device *in,
  3543. const struct net_device *out,
  3544. int (*okfn)(struct sk_buff *))
  3545. {
  3546. return selinux_ip_forward(skb, in->ifindex, PF_INET6);
  3547. }
  3548. #endif
  3549. static unsigned int selinux_ip_output(struct sk_buff *skb,
  3550. u16 family)
  3551. {
  3552. u32 sid;
  3553. if (!netlbl_enabled())
  3554. return NF_ACCEPT;
  3555. if (skb->sk) {
  3556. struct sk_security_struct *sksec = skb->sk->sk_security;
  3557. sid = sksec->sid;
  3558. } else
  3559. sid = SECINITSID_KERNEL;
  3560. if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
  3561. return NF_DROP;
  3562. return NF_ACCEPT;
  3563. }
  3564. static unsigned int selinux_ipv4_output(unsigned int hooknum,
  3565. struct sk_buff *skb,
  3566. const struct net_device *in,
  3567. const struct net_device *out,
  3568. int (*okfn)(struct sk_buff *))
  3569. {
  3570. return selinux_ip_output(skb, PF_INET);
  3571. }
  3572. static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
  3573. int ifindex,
  3574. u16 family)
  3575. {
  3576. struct sock *sk = skb->sk;
  3577. struct sk_security_struct *sksec;
  3578. struct common_audit_data ad;
  3579. struct selinux_audit_data sad = {0,};
  3580. struct lsm_network_audit net = {0,};
  3581. char *addrp;
  3582. u8 proto;
  3583. if (sk == NULL)
  3584. return NF_ACCEPT;
  3585. sksec = sk->sk_security;
  3586. COMMON_AUDIT_DATA_INIT(&ad, NET);
  3587. ad.selinux_audit_data = &sad;
  3588. ad.u.net = &net;
  3589. ad.u.net->netif = ifindex;
  3590. ad.u.net->family = family;
  3591. if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
  3592. return NF_DROP;
  3593. if (selinux_secmark_enabled())
  3594. if (avc_has_perm(sksec->sid, skb->secmark,
  3595. SECCLASS_PACKET, PACKET__SEND, &ad))
  3596. return NF_DROP_ERR(-ECONNREFUSED);
  3597. if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
  3598. return NF_DROP_ERR(-ECONNREFUSED);
  3599. return NF_ACCEPT;
  3600. }
  3601. static unsigned int selinux_ip_postroute(struct sk_buff *skb, int ifindex,
  3602. u16 family)
  3603. {
  3604. u32 secmark_perm;
  3605. u32 peer_sid;
  3606. struct sock *sk;
  3607. struct common_audit_data ad;
  3608. struct selinux_audit_data sad = {0,};
  3609. struct lsm_network_audit net = {0,};
  3610. char *addrp;
  3611. u8 secmark_active;
  3612. u8 peerlbl_active;
  3613. if (!selinux_policycap_netpeer)
  3614. return selinux_ip_postroute_compat(skb, ifindex, family);
  3615. #ifdef CONFIG_XFRM
  3616. if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL)
  3617. return NF_ACCEPT;
  3618. #endif
  3619. secmark_active = selinux_secmark_enabled();
  3620. peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled();
  3621. if (!secmark_active && !peerlbl_active)
  3622. return NF_ACCEPT;
  3623. sk = skb->sk;
  3624. if (sk == NULL) {
  3625. if (skb->skb_iif) {
  3626. secmark_perm = PACKET__FORWARD_OUT;
  3627. if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
  3628. return NF_DROP;
  3629. } else {
  3630. secmark_perm = PACKET__SEND;
  3631. peer_sid = SECINITSID_KERNEL;
  3632. }
  3633. } else {
  3634. struct sk_security_struct *sksec = sk->sk_security;
  3635. peer_sid = sksec->sid;
  3636. secmark_perm = PACKET__SEND;
  3637. }
  3638. COMMON_AUDIT_DATA_INIT(&ad, NET);
  3639. ad.selinux_audit_data = &sad;
  3640. ad.u.net = &net;
  3641. ad.u.net->netif = ifindex;
  3642. ad.u.net->family = family;
  3643. if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
  3644. return NF_DROP;
  3645. if (secmark_active)
  3646. if (avc_has_perm(peer_sid, skb->secmark,
  3647. SECCLASS_PACKET, secmark_perm, &ad))
  3648. return NF_DROP_ERR(-ECONNREFUSED);
  3649. if (peerlbl_active) {
  3650. u32 if_sid;
  3651. u32 node_sid;
  3652. if (sel_netif_sid(ifindex, &if_sid))
  3653. return NF_DROP;
  3654. if (avc_has_perm(peer_sid, if_sid,
  3655. SECCLASS_NETIF, NETIF__EGRESS, &ad))
  3656. return NF_DROP_ERR(-ECONNREFUSED);
  3657. if (sel_netnode_sid(addrp, family, &node_sid))
  3658. return NF_DROP;
  3659. if (avc_has_perm(peer_sid, node_sid,
  3660. SECCLASS_NODE, NODE__SENDTO, &ad))
  3661. return NF_DROP_ERR(-ECONNREFUSED);
  3662. }
  3663. return NF_ACCEPT;
  3664. }
  3665. static unsigned int selinux_ipv4_postroute(unsigned int hooknum,
  3666. struct sk_buff *skb,
  3667. const struct net_device *in,
  3668. const struct net_device *out,
  3669. int (*okfn)(struct sk_buff *))
  3670. {
  3671. return selinux_ip_postroute(skb, out->ifindex, PF_INET);
  3672. }
  3673. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  3674. static unsigned int selinux_ipv6_postroute(unsigned int hooknum,
  3675. struct sk_buff *skb,
  3676. const struct net_device *in,
  3677. const struct net_device *out,
  3678. int (*okfn)(struct sk_buff *))
  3679. {
  3680. return selinux_ip_postroute(skb, out->ifindex, PF_INET6);
  3681. }
  3682. #endif
  3683. #endif
  3684. static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
  3685. {
  3686. int err;
  3687. err = cap_netlink_send(sk, skb);
  3688. if (err)
  3689. return err;
  3690. return selinux_nlmsg_perm(sk, skb);
  3691. }
  3692. static int ipc_alloc_security(struct task_struct *task,
  3693. struct kern_ipc_perm *perm,
  3694. u16 sclass)
  3695. {
  3696. struct ipc_security_struct *isec;
  3697. u32 sid;
  3698. isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
  3699. if (!isec)
  3700. return -ENOMEM;
  3701. sid = task_sid(task);
  3702. isec->sclass = sclass;
  3703. isec->sid = sid;
  3704. perm->security = isec;
  3705. return 0;
  3706. }
  3707. static void ipc_free_security(struct kern_ipc_perm *perm)
  3708. {
  3709. struct ipc_security_struct *isec = perm->security;
  3710. perm->security = NULL;
  3711. kfree(isec);
  3712. }
  3713. static int msg_msg_alloc_security(struct msg_msg *msg)
  3714. {
  3715. struct msg_security_struct *msec;
  3716. msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
  3717. if (!msec)
  3718. return -ENOMEM;
  3719. msec->sid = SECINITSID_UNLABELED;
  3720. msg->security = msec;
  3721. return 0;
  3722. }
  3723. static void msg_msg_free_security(struct msg_msg *msg)
  3724. {
  3725. struct msg_security_struct *msec = msg->security;
  3726. msg->security = NULL;
  3727. kfree(msec);
  3728. }
  3729. static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
  3730. u32 perms)
  3731. {
  3732. struct ipc_security_struct *isec;
  3733. struct common_audit_data ad;
  3734. struct selinux_audit_data sad = {0,};
  3735. u32 sid = current_sid();
  3736. isec = ipc_perms->security;
  3737. COMMON_AUDIT_DATA_INIT(&ad, IPC);
  3738. ad.selinux_audit_data = &sad;
  3739. ad.u.ipc_id = ipc_perms->key;
  3740. return avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad);
  3741. }
  3742. static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
  3743. {
  3744. return msg_msg_alloc_security(msg);
  3745. }
  3746. static void selinux_msg_msg_free_security(struct msg_msg *msg)
  3747. {
  3748. msg_msg_free_security(msg);
  3749. }
  3750. static int selinux_msg_queue_alloc_security(struct msg_queue *msq)
  3751. {
  3752. struct ipc_security_struct *isec;
  3753. struct common_audit_data ad;
  3754. struct selinux_audit_data sad = {0,};
  3755. u32 sid = current_sid();
  3756. int rc;
  3757. rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ);
  3758. if (rc)
  3759. return rc;
  3760. isec = msq->q_perm.security;
  3761. COMMON_AUDIT_DATA_INIT(&ad, IPC);
  3762. ad.selinux_audit_data = &sad;
  3763. ad.u.ipc_id = msq->q_perm.key;
  3764. rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
  3765. MSGQ__CREATE, &ad);
  3766. if (rc) {
  3767. ipc_free_security(&msq->q_perm);
  3768. return rc;
  3769. }
  3770. return 0;
  3771. }
  3772. static void selinux_msg_queue_free_security(struct msg_queue *msq)
  3773. {
  3774. ipc_free_security(&msq->q_perm);
  3775. }
  3776. static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg)
  3777. {
  3778. struct ipc_security_struct *isec;
  3779. struct common_audit_data ad;
  3780. struct selinux_audit_data sad = {0,};
  3781. u32 sid = current_sid();
  3782. isec = msq->q_perm.security;
  3783. COMMON_AUDIT_DATA_INIT(&ad, IPC);
  3784. ad.selinux_audit_data = &sad;
  3785. ad.u.ipc_id = msq->q_perm.key;
  3786. return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
  3787. MSGQ__ASSOCIATE, &ad);
  3788. }
  3789. static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd)
  3790. {
  3791. int err;
  3792. int perms;
  3793. switch (cmd) {
  3794. case IPC_INFO:
  3795. case MSG_INFO:
  3796. return task_has_system(current, SYSTEM__IPC_INFO);
  3797. case IPC_STAT:
  3798. case MSG_STAT:
  3799. perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
  3800. break;
  3801. case IPC_SET:
  3802. perms = MSGQ__SETATTR;
  3803. break;
  3804. case IPC_RMID:
  3805. perms = MSGQ__DESTROY;
  3806. break;
  3807. default:
  3808. return 0;
  3809. }
  3810. err = ipc_has_perm(&msq->q_perm, perms);
  3811. return err;
  3812. }
  3813. static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg)
  3814. {
  3815. struct ipc_security_struct *isec;
  3816. struct msg_security_struct *msec;
  3817. struct common_audit_data ad;
  3818. struct selinux_audit_data sad = {0,};
  3819. u32 sid = current_sid();
  3820. int rc;
  3821. isec = msq->q_perm.security;
  3822. msec = msg->security;
  3823. if (msec->sid == SECINITSID_UNLABELED) {
  3824. rc = security_transition_sid(sid, isec->sid, SECCLASS_MSG,
  3825. NULL, &msec->sid);
  3826. if (rc)
  3827. return rc;
  3828. }
  3829. COMMON_AUDIT_DATA_INIT(&ad, IPC);
  3830. ad.selinux_audit_data = &sad;
  3831. ad.u.ipc_id = msq->q_perm.key;
  3832. rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
  3833. MSGQ__WRITE, &ad);
  3834. if (!rc)
  3835. rc = avc_has_perm(sid, msec->sid, SECCLASS_MSG,
  3836. MSG__SEND, &ad);
  3837. if (!rc)
  3838. rc = avc_has_perm(msec->sid, isec->sid, SECCLASS_MSGQ,
  3839. MSGQ__ENQUEUE, &ad);
  3840. return rc;
  3841. }
  3842. static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
  3843. struct task_struct *target,
  3844. long type, int mode)
  3845. {
  3846. struct ipc_security_struct *isec;
  3847. struct msg_security_struct *msec;
  3848. struct common_audit_data ad;
  3849. struct selinux_audit_data sad = {0,};
  3850. u32 sid = task_sid(target);
  3851. int rc;
  3852. isec = msq->q_perm.security;
  3853. msec = msg->security;
  3854. COMMON_AUDIT_DATA_INIT(&ad, IPC);
  3855. ad.selinux_audit_data = &sad;
  3856. ad.u.ipc_id = msq->q_perm.key;
  3857. rc = avc_has_perm(sid, isec->sid,
  3858. SECCLASS_MSGQ, MSGQ__READ, &ad);
  3859. if (!rc)
  3860. rc = avc_has_perm(sid, msec->sid,
  3861. SECCLASS_MSG, MSG__RECEIVE, &ad);
  3862. return rc;
  3863. }
  3864. static int selinux_shm_alloc_security(struct shmid_kernel *shp)
  3865. {
  3866. struct ipc_security_struct *isec;
  3867. struct common_audit_data ad;
  3868. struct selinux_audit_data sad = {0,};
  3869. u32 sid = current_sid();
  3870. int rc;
  3871. rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM);
  3872. if (rc)
  3873. return rc;
  3874. isec = shp->shm_perm.security;
  3875. COMMON_AUDIT_DATA_INIT(&ad, IPC);
  3876. ad.selinux_audit_data = &sad;
  3877. ad.u.ipc_id = shp->shm_perm.key;
  3878. rc = avc_has_perm(sid, isec->sid, SECCLASS_SHM,
  3879. SHM__CREATE, &ad);
  3880. if (rc) {
  3881. ipc_free_security(&shp->shm_perm);
  3882. return rc;
  3883. }
  3884. return 0;
  3885. }
  3886. static void selinux_shm_free_security(struct shmid_kernel *shp)
  3887. {
  3888. ipc_free_security(&shp->shm_perm);
  3889. }
  3890. static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg)
  3891. {
  3892. struct ipc_security_struct *isec;
  3893. struct common_audit_data ad;
  3894. struct selinux_audit_data sad = {0,};
  3895. u32 sid = current_sid();
  3896. isec = shp->shm_perm.security;
  3897. COMMON_AUDIT_DATA_INIT(&ad, IPC);
  3898. ad.selinux_audit_data = &sad;
  3899. ad.u.ipc_id = shp->shm_perm.key;
  3900. return avc_has_perm(sid, isec->sid, SECCLASS_SHM,
  3901. SHM__ASSOCIATE, &ad);
  3902. }
  3903. static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd)
  3904. {
  3905. int perms;
  3906. int err;
  3907. switch (cmd) {
  3908. case IPC_INFO:
  3909. case SHM_INFO:
  3910. return task_has_system(current, SYSTEM__IPC_INFO);
  3911. case IPC_STAT:
  3912. case SHM_STAT:
  3913. perms = SHM__GETATTR | SHM__ASSOCIATE;
  3914. break;
  3915. case IPC_SET:
  3916. perms = SHM__SETATTR;
  3917. break;
  3918. case SHM_LOCK:
  3919. case SHM_UNLOCK:
  3920. perms = SHM__LOCK;
  3921. break;
  3922. case IPC_RMID:
  3923. perms = SHM__DESTROY;
  3924. break;
  3925. default:
  3926. return 0;
  3927. }
  3928. err = ipc_has_perm(&shp->shm_perm, perms);
  3929. return err;
  3930. }
  3931. static int selinux_shm_shmat(struct shmid_kernel *shp,
  3932. char __user *shmaddr, int shmflg)
  3933. {
  3934. u32 perms;
  3935. if (shmflg & SHM_RDONLY)
  3936. perms = SHM__READ;
  3937. else
  3938. perms = SHM__READ | SHM__WRITE;
  3939. return ipc_has_perm(&shp->shm_perm, perms);
  3940. }
  3941. static int selinux_sem_alloc_security(struct sem_array *sma)
  3942. {
  3943. struct ipc_security_struct *isec;
  3944. struct common_audit_data ad;
  3945. struct selinux_audit_data sad = {0,};
  3946. u32 sid = current_sid();
  3947. int rc;
  3948. rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM);
  3949. if (rc)
  3950. return rc;
  3951. isec = sma->sem_perm.security;
  3952. COMMON_AUDIT_DATA_INIT(&ad, IPC);
  3953. ad.selinux_audit_data = &sad;
  3954. ad.u.ipc_id = sma->sem_perm.key;
  3955. rc = avc_has_perm(sid, isec->sid, SECCLASS_SEM,
  3956. SEM__CREATE, &ad);
  3957. if (rc) {
  3958. ipc_free_security(&sma->sem_perm);
  3959. return rc;
  3960. }
  3961. return 0;
  3962. }
  3963. static void selinux_sem_free_security(struct sem_array *sma)
  3964. {
  3965. ipc_free_security(&sma->sem_perm);
  3966. }
  3967. static int selinux_sem_associate(struct sem_array *sma, int semflg)
  3968. {
  3969. struct ipc_security_struct *isec;
  3970. struct common_audit_data ad;
  3971. struct selinux_audit_data sad = {0,};
  3972. u32 sid = current_sid();
  3973. isec = sma->sem_perm.security;
  3974. COMMON_AUDIT_DATA_INIT(&ad, IPC);
  3975. ad.selinux_audit_data = &sad;
  3976. ad.u.ipc_id = sma->sem_perm.key;
  3977. return avc_has_perm(sid, isec->sid, SECCLASS_SEM,
  3978. SEM__ASSOCIATE, &ad);
  3979. }
  3980. static int selinux_sem_semctl(struct sem_array *sma, int cmd)
  3981. {
  3982. int err;
  3983. u32 perms;
  3984. switch (cmd) {
  3985. case IPC_INFO:
  3986. case SEM_INFO:
  3987. return task_has_system(current, SYSTEM__IPC_INFO);
  3988. case GETPID:
  3989. case GETNCNT:
  3990. case GETZCNT:
  3991. perms = SEM__GETATTR;
  3992. break;
  3993. case GETVAL:
  3994. case GETALL:
  3995. perms = SEM__READ;
  3996. break;
  3997. case SETVAL:
  3998. case SETALL:
  3999. perms = SEM__WRITE;
  4000. break;
  4001. case IPC_RMID:
  4002. perms = SEM__DESTROY;
  4003. break;
  4004. case IPC_SET:
  4005. perms = SEM__SETATTR;
  4006. break;
  4007. case IPC_STAT:
  4008. case SEM_STAT:
  4009. perms = SEM__GETATTR | SEM__ASSOCIATE;
  4010. break;
  4011. default:
  4012. return 0;
  4013. }
  4014. err = ipc_has_perm(&sma->sem_perm, perms);
  4015. return err;
  4016. }
  4017. static int selinux_sem_semop(struct sem_array *sma,
  4018. struct sembuf *sops, unsigned nsops, int alter)
  4019. {
  4020. u32 perms;
  4021. if (alter)
  4022. perms = SEM__READ | SEM__WRITE;
  4023. else
  4024. perms = SEM__READ;
  4025. return ipc_has_perm(&sma->sem_perm, perms);
  4026. }
  4027. static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
  4028. {
  4029. u32 av = 0;
  4030. av = 0;
  4031. if (flag & S_IRUGO)
  4032. av |= IPC__UNIX_READ;
  4033. if (flag & S_IWUGO)
  4034. av |= IPC__UNIX_WRITE;
  4035. if (av == 0)
  4036. return 0;
  4037. return ipc_has_perm(ipcp, av);
  4038. }
  4039. static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
  4040. {
  4041. struct ipc_security_struct *isec = ipcp->security;
  4042. *secid = isec->sid;
  4043. }
  4044. static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
  4045. {
  4046. if (inode)
  4047. inode_doinit_with_dentry(inode, dentry);
  4048. }
  4049. static int selinux_getprocattr(struct task_struct *p,
  4050. char *name, char **value)
  4051. {
  4052. const struct task_security_struct *__tsec;
  4053. u32 sid;
  4054. int error;
  4055. unsigned len;
  4056. if (current != p) {
  4057. error = current_has_perm(p, PROCESS__GETATTR);
  4058. if (error)
  4059. return error;
  4060. }
  4061. rcu_read_lock();
  4062. __tsec = __task_cred(p)->security;
  4063. if (!strcmp(name, "current"))
  4064. sid = __tsec->sid;
  4065. else if (!strcmp(name, "prev"))
  4066. sid = __tsec->osid;
  4067. else if (!strcmp(name, "exec"))
  4068. sid = __tsec->exec_sid;
  4069. else if (!strcmp(name, "fscreate"))
  4070. sid = __tsec->create_sid;
  4071. else if (!strcmp(name, "keycreate"))
  4072. sid = __tsec->keycreate_sid;
  4073. else if (!strcmp(name, "sockcreate"))
  4074. sid = __tsec->sockcreate_sid;
  4075. else
  4076. goto invalid;
  4077. rcu_read_unlock();
  4078. if (!sid)
  4079. return 0;
  4080. error = security_sid_to_context(sid, value, &len);
  4081. if (error)
  4082. return error;
  4083. return len;
  4084. invalid:
  4085. rcu_read_unlock();
  4086. return -EINVAL;
  4087. }
  4088. static int selinux_setprocattr(struct task_struct *p,
  4089. char *name, void *value, size_t size)
  4090. {
  4091. struct task_security_struct *tsec;
  4092. struct task_struct *tracer;
  4093. struct cred *new;
  4094. u32 sid = 0, ptsid;
  4095. int error;
  4096. char *str = value;
  4097. if (current != p) {
  4098. return -EACCES;
  4099. }
  4100. if (!strcmp(name, "exec"))
  4101. error = current_has_perm(p, PROCESS__SETEXEC);
  4102. else if (!strcmp(name, "fscreate"))
  4103. error = current_has_perm(p, PROCESS__SETFSCREATE);
  4104. else if (!strcmp(name, "keycreate"))
  4105. error = current_has_perm(p, PROCESS__SETKEYCREATE);
  4106. else if (!strcmp(name, "sockcreate"))
  4107. error = current_has_perm(p, PROCESS__SETSOCKCREATE);
  4108. else if (!strcmp(name, "current"))
  4109. error = current_has_perm(p, PROCESS__SETCURRENT);
  4110. else
  4111. error = -EINVAL;
  4112. if (error)
  4113. return error;
  4114. if (size && str[1] && str[1] != '\n') {
  4115. if (str[size-1] == '\n') {
  4116. str[size-1] = 0;
  4117. size--;
  4118. }
  4119. error = security_context_to_sid(value, size, &sid);
  4120. if (error == -EINVAL && !strcmp(name, "fscreate")) {
  4121. if (!capable(CAP_MAC_ADMIN))
  4122. return error;
  4123. error = security_context_to_sid_force(value, size,
  4124. &sid);
  4125. }
  4126. if (error)
  4127. return error;
  4128. }
  4129. new = prepare_creds();
  4130. if (!new)
  4131. return -ENOMEM;
  4132. tsec = new->security;
  4133. if (!strcmp(name, "exec")) {
  4134. tsec->exec_sid = sid;
  4135. } else if (!strcmp(name, "fscreate")) {
  4136. tsec->create_sid = sid;
  4137. } else if (!strcmp(name, "keycreate")) {
  4138. error = may_create_key(sid, p);
  4139. if (error)
  4140. goto abort_change;
  4141. tsec->keycreate_sid = sid;
  4142. } else if (!strcmp(name, "sockcreate")) {
  4143. tsec->sockcreate_sid = sid;
  4144. } else if (!strcmp(name, "current")) {
  4145. error = -EINVAL;
  4146. if (sid == 0)
  4147. goto abort_change;
  4148. error = -EPERM;
  4149. if (!current_is_single_threaded()) {
  4150. error = security_bounded_transition(tsec->sid, sid);
  4151. if (error)
  4152. goto abort_change;
  4153. }
  4154. error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS,
  4155. PROCESS__DYNTRANSITION, NULL);
  4156. if (error)
  4157. goto abort_change;
  4158. ptsid = 0;
  4159. task_lock(p);
  4160. tracer = ptrace_parent(p);
  4161. if (tracer)
  4162. ptsid = task_sid(tracer);
  4163. task_unlock(p);
  4164. if (tracer) {
  4165. error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS,
  4166. PROCESS__PTRACE, NULL);
  4167. if (error)
  4168. goto abort_change;
  4169. }
  4170. tsec->sid = sid;
  4171. } else {
  4172. error = -EINVAL;
  4173. goto abort_change;
  4174. }
  4175. commit_creds(new);
  4176. return size;
  4177. abort_change:
  4178. abort_creds(new);
  4179. return error;
  4180. }
  4181. static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
  4182. {
  4183. return security_sid_to_context(secid, secdata, seclen);
  4184. }
  4185. static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
  4186. {
  4187. return security_context_to_sid(secdata, seclen, secid);
  4188. }
  4189. static void selinux_release_secctx(char *secdata, u32 seclen)
  4190. {
  4191. kfree(secdata);
  4192. }
  4193. static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
  4194. {
  4195. return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0);
  4196. }
  4197. static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
  4198. {
  4199. return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
  4200. }
  4201. static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
  4202. {
  4203. int len = 0;
  4204. len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
  4205. ctx, true);
  4206. if (len < 0)
  4207. return len;
  4208. *ctxlen = len;
  4209. return 0;
  4210. }
  4211. #ifdef CONFIG_KEYS
  4212. static int selinux_key_alloc(struct key *k, const struct cred *cred,
  4213. unsigned long flags)
  4214. {
  4215. const struct task_security_struct *tsec;
  4216. struct key_security_struct *ksec;
  4217. ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
  4218. if (!ksec)
  4219. return -ENOMEM;
  4220. tsec = cred->security;
  4221. if (tsec->keycreate_sid)
  4222. ksec->sid = tsec->keycreate_sid;
  4223. else
  4224. ksec->sid = tsec->sid;
  4225. k->security = ksec;
  4226. return 0;
  4227. }
  4228. static void selinux_key_free(struct key *k)
  4229. {
  4230. struct key_security_struct *ksec = k->security;
  4231. k->security = NULL;
  4232. kfree(ksec);
  4233. }
  4234. static int selinux_key_permission(key_ref_t key_ref,
  4235. const struct cred *cred,
  4236. key_perm_t perm)
  4237. {
  4238. struct key *key;
  4239. struct key_security_struct *ksec;
  4240. u32 sid;
  4241. if (perm == 0)
  4242. return 0;
  4243. sid = cred_sid(cred);
  4244. key = key_ref_to_ptr(key_ref);
  4245. ksec = key->security;
  4246. return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, perm, NULL);
  4247. }
  4248. static int selinux_key_getsecurity(struct key *key, char **_buffer)
  4249. {
  4250. struct key_security_struct *ksec = key->security;
  4251. char *context = NULL;
  4252. unsigned len;
  4253. int rc;
  4254. rc = security_sid_to_context(ksec->sid, &context, &len);
  4255. if (!rc)
  4256. rc = len;
  4257. *_buffer = context;
  4258. return rc;
  4259. }
  4260. #endif
  4261. static struct security_operations selinux_ops = {
  4262. .name = "selinux",
  4263. .binder_set_context_mgr = selinux_binder_set_context_mgr,
  4264. .binder_transaction = selinux_binder_transaction,
  4265. .binder_transfer_binder = selinux_binder_transfer_binder,
  4266. .binder_transfer_file = selinux_binder_transfer_file,
  4267. .ptrace_access_check = selinux_ptrace_access_check,
  4268. .ptrace_traceme = selinux_ptrace_traceme,
  4269. .capget = selinux_capget,
  4270. .capset = selinux_capset,
  4271. .capable = selinux_capable,
  4272. .quotactl = selinux_quotactl,
  4273. .quota_on = selinux_quota_on,
  4274. .syslog = selinux_syslog,
  4275. .vm_enough_memory = selinux_vm_enough_memory,
  4276. .netlink_send = selinux_netlink_send,
  4277. .bprm_set_creds = selinux_bprm_set_creds,
  4278. .bprm_committing_creds = selinux_bprm_committing_creds,
  4279. .bprm_committed_creds = selinux_bprm_committed_creds,
  4280. .bprm_secureexec = selinux_bprm_secureexec,
  4281. .sb_alloc_security = selinux_sb_alloc_security,
  4282. .sb_free_security = selinux_sb_free_security,
  4283. .sb_copy_data = selinux_sb_copy_data,
  4284. .sb_remount = selinux_sb_remount,
  4285. .sb_kern_mount = selinux_sb_kern_mount,
  4286. .sb_show_options = selinux_sb_show_options,
  4287. .sb_statfs = selinux_sb_statfs,
  4288. .sb_mount = selinux_mount,
  4289. .sb_umount = selinux_umount,
  4290. .sb_set_mnt_opts = selinux_set_mnt_opts,
  4291. .sb_clone_mnt_opts = selinux_sb_clone_mnt_opts,
  4292. .sb_parse_opts_str = selinux_parse_opts_str,
  4293. .inode_alloc_security = selinux_inode_alloc_security,
  4294. .inode_free_security = selinux_inode_free_security,
  4295. .inode_init_security = selinux_inode_init_security,
  4296. .inode_create = selinux_inode_create,
  4297. .inode_link = selinux_inode_link,
  4298. .inode_unlink = selinux_inode_unlink,
  4299. .inode_symlink = selinux_inode_symlink,
  4300. .inode_mkdir = selinux_inode_mkdir,
  4301. .inode_rmdir = selinux_inode_rmdir,
  4302. .inode_mknod = selinux_inode_mknod,
  4303. .inode_rename = selinux_inode_rename,
  4304. .inode_readlink = selinux_inode_readlink,
  4305. .inode_follow_link = selinux_inode_follow_link,
  4306. .inode_permission = selinux_inode_permission,
  4307. .inode_setattr = selinux_inode_setattr,
  4308. .inode_getattr = selinux_inode_getattr,
  4309. .inode_setxattr = selinux_inode_setxattr,
  4310. .inode_post_setxattr = selinux_inode_post_setxattr,
  4311. .inode_getxattr = selinux_inode_getxattr,
  4312. .inode_listxattr = selinux_inode_listxattr,
  4313. .inode_removexattr = selinux_inode_removexattr,
  4314. .inode_getsecurity = selinux_inode_getsecurity,
  4315. .inode_setsecurity = selinux_inode_setsecurity,
  4316. .inode_listsecurity = selinux_inode_listsecurity,
  4317. .inode_getsecid = selinux_inode_getsecid,
  4318. .file_permission = selinux_file_permission,
  4319. .file_alloc_security = selinux_file_alloc_security,
  4320. .file_free_security = selinux_file_free_security,
  4321. .file_ioctl = selinux_file_ioctl,
  4322. .file_mmap = selinux_file_mmap,
  4323. .file_mprotect = selinux_file_mprotect,
  4324. .file_lock = selinux_file_lock,
  4325. .file_fcntl = selinux_file_fcntl,
  4326. .file_set_fowner = selinux_file_set_fowner,
  4327. .file_send_sigiotask = selinux_file_send_sigiotask,
  4328. .file_receive = selinux_file_receive,
  4329. .dentry_open = selinux_dentry_open,
  4330. .task_create = selinux_task_create,
  4331. .cred_alloc_blank = selinux_cred_alloc_blank,
  4332. .cred_free = selinux_cred_free,
  4333. .cred_prepare = selinux_cred_prepare,
  4334. .cred_transfer = selinux_cred_transfer,
  4335. .kernel_act_as = selinux_kernel_act_as,
  4336. .kernel_create_files_as = selinux_kernel_create_files_as,
  4337. .kernel_module_request = selinux_kernel_module_request,
  4338. .task_setpgid = selinux_task_setpgid,
  4339. .task_getpgid = selinux_task_getpgid,
  4340. .task_getsid = selinux_task_getsid,
  4341. .task_getsecid = selinux_task_getsecid,
  4342. .task_setnice = selinux_task_setnice,
  4343. .task_setioprio = selinux_task_setioprio,
  4344. .task_getioprio = selinux_task_getioprio,
  4345. .task_setrlimit = selinux_task_setrlimit,
  4346. .task_setscheduler = selinux_task_setscheduler,
  4347. .task_getscheduler = selinux_task_getscheduler,
  4348. .task_movememory = selinux_task_movememory,
  4349. .task_kill = selinux_task_kill,
  4350. .task_wait = selinux_task_wait,
  4351. .task_to_inode = selinux_task_to_inode,
  4352. .ipc_permission = selinux_ipc_permission,
  4353. .ipc_getsecid = selinux_ipc_getsecid,
  4354. .msg_msg_alloc_security = selinux_msg_msg_alloc_security,
  4355. .msg_msg_free_security = selinux_msg_msg_free_security,
  4356. .msg_queue_alloc_security = selinux_msg_queue_alloc_security,
  4357. .msg_queue_free_security = selinux_msg_queue_free_security,
  4358. .msg_queue_associate = selinux_msg_queue_associate,
  4359. .msg_queue_msgctl = selinux_msg_queue_msgctl,
  4360. .msg_queue_msgsnd = selinux_msg_queue_msgsnd,
  4361. .msg_queue_msgrcv = selinux_msg_queue_msgrcv,
  4362. .shm_alloc_security = selinux_shm_alloc_security,
  4363. .shm_free_security = selinux_shm_free_security,
  4364. .shm_associate = selinux_shm_associate,
  4365. .shm_shmctl = selinux_shm_shmctl,
  4366. .shm_shmat = selinux_shm_shmat,
  4367. .sem_alloc_security = selinux_sem_alloc_security,
  4368. .sem_free_security = selinux_sem_free_security,
  4369. .sem_associate = selinux_sem_associate,
  4370. .sem_semctl = selinux_sem_semctl,
  4371. .sem_semop = selinux_sem_semop,
  4372. .d_instantiate = selinux_d_instantiate,
  4373. .getprocattr = selinux_getprocattr,
  4374. .setprocattr = selinux_setprocattr,
  4375. .secid_to_secctx = selinux_secid_to_secctx,
  4376. .secctx_to_secid = selinux_secctx_to_secid,
  4377. .release_secctx = selinux_release_secctx,
  4378. .inode_notifysecctx = selinux_inode_notifysecctx,
  4379. .inode_setsecctx = selinux_inode_setsecctx,
  4380. .inode_getsecctx = selinux_inode_getsecctx,
  4381. .unix_stream_connect = selinux_socket_unix_stream_connect,
  4382. .unix_may_send = selinux_socket_unix_may_send,
  4383. .socket_create = selinux_socket_create,
  4384. .socket_post_create = selinux_socket_post_create,
  4385. .socket_bind = selinux_socket_bind,
  4386. .socket_connect = selinux_socket_connect,
  4387. .socket_listen = selinux_socket_listen,
  4388. .socket_accept = selinux_socket_accept,
  4389. .socket_sendmsg = selinux_socket_sendmsg,
  4390. .socket_recvmsg = selinux_socket_recvmsg,
  4391. .socket_getsockname = selinux_socket_getsockname,
  4392. .socket_getpeername = selinux_socket_getpeername,
  4393. .socket_getsockopt = selinux_socket_getsockopt,
  4394. .socket_setsockopt = selinux_socket_setsockopt,
  4395. .socket_shutdown = selinux_socket_shutdown,
  4396. .socket_sock_rcv_skb = selinux_socket_sock_rcv_skb,
  4397. .socket_getpeersec_stream = selinux_socket_getpeersec_stream,
  4398. .socket_getpeersec_dgram = selinux_socket_getpeersec_dgram,
  4399. .sk_alloc_security = selinux_sk_alloc_security,
  4400. .sk_free_security = selinux_sk_free_security,
  4401. .sk_clone_security = selinux_sk_clone_security,
  4402. .sk_getsecid = selinux_sk_getsecid,
  4403. .sock_graft = selinux_sock_graft,
  4404. .inet_conn_request = selinux_inet_conn_request,
  4405. .inet_csk_clone = selinux_inet_csk_clone,
  4406. .inet_conn_established = selinux_inet_conn_established,
  4407. .secmark_relabel_packet = selinux_secmark_relabel_packet,
  4408. .secmark_refcount_inc = selinux_secmark_refcount_inc,
  4409. .secmark_refcount_dec = selinux_secmark_refcount_dec,
  4410. .req_classify_flow = selinux_req_classify_flow,
  4411. .tun_dev_create = selinux_tun_dev_create,
  4412. .tun_dev_post_create = selinux_tun_dev_post_create,
  4413. .tun_dev_attach = selinux_tun_dev_attach,
  4414. #ifdef CONFIG_SECURITY_NETWORK_XFRM
  4415. .xfrm_policy_alloc_security = selinux_xfrm_policy_alloc,
  4416. .xfrm_policy_clone_security = selinux_xfrm_policy_clone,
  4417. .xfrm_policy_free_security = selinux_xfrm_policy_free,
  4418. .xfrm_policy_delete_security = selinux_xfrm_policy_delete,
  4419. .xfrm_state_alloc_security = selinux_xfrm_state_alloc,
  4420. .xfrm_state_free_security = selinux_xfrm_state_free,
  4421. .xfrm_state_delete_security = selinux_xfrm_state_delete,
  4422. .xfrm_policy_lookup = selinux_xfrm_policy_lookup,
  4423. .xfrm_state_pol_flow_match = selinux_xfrm_state_pol_flow_match,
  4424. .xfrm_decode_session = selinux_xfrm_decode_session,
  4425. #endif
  4426. #ifdef CONFIG_KEYS
  4427. .key_alloc = selinux_key_alloc,
  4428. .key_free = selinux_key_free,
  4429. .key_permission = selinux_key_permission,
  4430. .key_getsecurity = selinux_key_getsecurity,
  4431. #endif
  4432. #ifdef CONFIG_AUDIT
  4433. .audit_rule_init = selinux_audit_rule_init,
  4434. .audit_rule_known = selinux_audit_rule_known,
  4435. .audit_rule_match = selinux_audit_rule_match,
  4436. .audit_rule_free = selinux_audit_rule_free,
  4437. #endif
  4438. };
  4439. static __init int selinux_init(void)
  4440. {
  4441. if (!security_module_enable(&selinux_ops)) {
  4442. selinux_enabled = 0;
  4443. return 0;
  4444. }
  4445. if (!selinux_enabled) {
  4446. printk(KERN_INFO "SELinux: Disabled at boot.\n");
  4447. return 0;
  4448. }
  4449. printk(KERN_INFO "SELinux: Initializing.\n");
  4450. cred_init_security();
  4451. default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
  4452. sel_inode_cache = kmem_cache_create("selinux_inode_security",
  4453. sizeof(struct inode_security_struct),
  4454. 0, SLAB_PANIC, NULL);
  4455. avc_init();
  4456. if (register_security(&selinux_ops))
  4457. panic("SELinux: Unable to register with kernel.\n");
  4458. if (selinux_enforcing)
  4459. printk(KERN_DEBUG "SELinux: Starting in enforcing mode\n");
  4460. else
  4461. printk(KERN_DEBUG "SELinux: Starting in permissive mode\n");
  4462. return 0;
  4463. }
  4464. static void delayed_superblock_init(struct super_block *sb, void *unused)
  4465. {
  4466. superblock_doinit(sb, NULL);
  4467. }
  4468. void selinux_complete_init(void)
  4469. {
  4470. printk(KERN_DEBUG "SELinux: Completing initialization.\n");
  4471. printk(KERN_DEBUG "SELinux: Setting up existing superblocks.\n");
  4472. iterate_supers(delayed_superblock_init, NULL);
  4473. }
  4474. security_initcall(selinux_init);
  4475. #if defined(CONFIG_NETFILTER)
  4476. static struct nf_hook_ops selinux_ipv4_ops[] = {
  4477. {
  4478. .hook = selinux_ipv4_postroute,
  4479. .owner = THIS_MODULE,
  4480. .pf = PF_INET,
  4481. .hooknum = NF_INET_POST_ROUTING,
  4482. .priority = NF_IP_PRI_SELINUX_LAST,
  4483. },
  4484. {
  4485. .hook = selinux_ipv4_forward,
  4486. .owner = THIS_MODULE,
  4487. .pf = PF_INET,
  4488. .hooknum = NF_INET_FORWARD,
  4489. .priority = NF_IP_PRI_SELINUX_FIRST,
  4490. },
  4491. {
  4492. .hook = selinux_ipv4_output,
  4493. .owner = THIS_MODULE,
  4494. .pf = PF_INET,
  4495. .hooknum = NF_INET_LOCAL_OUT,
  4496. .priority = NF_IP_PRI_SELINUX_FIRST,
  4497. }
  4498. };
  4499. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  4500. static struct nf_hook_ops selinux_ipv6_ops[] = {
  4501. {
  4502. .hook = selinux_ipv6_postroute,
  4503. .owner = THIS_MODULE,
  4504. .pf = PF_INET6,
  4505. .hooknum = NF_INET_POST_ROUTING,
  4506. .priority = NF_IP6_PRI_SELINUX_LAST,
  4507. },
  4508. {
  4509. .hook = selinux_ipv6_forward,
  4510. .owner = THIS_MODULE,
  4511. .pf = PF_INET6,
  4512. .hooknum = NF_INET_FORWARD,
  4513. .priority = NF_IP6_PRI_SELINUX_FIRST,
  4514. }
  4515. };
  4516. #endif
  4517. static int __init selinux_nf_ip_init(void)
  4518. {
  4519. int err = 0;
  4520. if (!selinux_enabled)
  4521. goto out;
  4522. printk(KERN_DEBUG "SELinux: Registering netfilter hooks\n");
  4523. err = nf_register_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
  4524. if (err)
  4525. panic("SELinux: nf_register_hooks for IPv4: error %d\n", err);
  4526. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  4527. err = nf_register_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
  4528. if (err)
  4529. panic("SELinux: nf_register_hooks for IPv6: error %d\n", err);
  4530. #endif
  4531. out:
  4532. return err;
  4533. }
  4534. __initcall(selinux_nf_ip_init);
  4535. #ifdef CONFIG_SECURITY_SELINUX_DISABLE
  4536. static void selinux_nf_ip_exit(void)
  4537. {
  4538. printk(KERN_DEBUG "SELinux: Unregistering netfilter hooks\n");
  4539. nf_unregister_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
  4540. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  4541. nf_unregister_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
  4542. #endif
  4543. }
  4544. #endif
  4545. #else
  4546. #ifdef CONFIG_SECURITY_SELINUX_DISABLE
  4547. #define selinux_nf_ip_exit()
  4548. #endif
  4549. #endif
  4550. #ifdef CONFIG_SECURITY_SELINUX_DISABLE
  4551. static int selinux_disabled;
  4552. int selinux_disable(void)
  4553. {
  4554. if (ss_initialized) {
  4555. return -EINVAL;
  4556. }
  4557. if (selinux_disabled) {
  4558. return -EINVAL;
  4559. }
  4560. printk(KERN_INFO "SELinux: Disabled at runtime.\n");
  4561. selinux_disabled = 1;
  4562. selinux_enabled = 0;
  4563. reset_security_ops();
  4564. avc_disable();
  4565. selinux_nf_ip_exit();
  4566. exit_sel_fs();
  4567. return 0;
  4568. }
  4569. #endif