tomoyo.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /*
  2. * security/tomoyo/tomoyo.c
  3. *
  4. * Copyright (C) 2005-2011 NTT DATA CORPORATION
  5. */
  6. #include <linux/lsm_hooks.h>
  7. #include "common.h"
  8. /**
  9. * tomoyo_cred_alloc_blank - Target for security_cred_alloc_blank().
  10. *
  11. * @new: Pointer to "struct cred".
  12. * @gfp: Memory allocation flags.
  13. *
  14. * Returns 0.
  15. */
  16. static int tomoyo_cred_alloc_blank(struct cred *new, gfp_t gfp)
  17. {
  18. new->security = NULL;
  19. return 0;
  20. }
  21. /**
  22. * tomoyo_cred_prepare - Target for security_prepare_creds().
  23. *
  24. * @new: Pointer to "struct cred".
  25. * @old: Pointer to "struct cred".
  26. * @gfp: Memory allocation flags.
  27. *
  28. * Returns 0.
  29. */
  30. static int tomoyo_cred_prepare(struct cred *new, const struct cred *old,
  31. gfp_t gfp)
  32. {
  33. struct tomoyo_domain_info *domain = old->security;
  34. new->security = domain;
  35. if (domain)
  36. atomic_inc(&domain->users);
  37. return 0;
  38. }
  39. /**
  40. * tomoyo_cred_transfer - Target for security_transfer_creds().
  41. *
  42. * @new: Pointer to "struct cred".
  43. * @old: Pointer to "struct cred".
  44. */
  45. static void tomoyo_cred_transfer(struct cred *new, const struct cred *old)
  46. {
  47. tomoyo_cred_prepare(new, old, 0);
  48. }
  49. /**
  50. * tomoyo_cred_free - Target for security_cred_free().
  51. *
  52. * @cred: Pointer to "struct cred".
  53. */
  54. static void tomoyo_cred_free(struct cred *cred)
  55. {
  56. struct tomoyo_domain_info *domain = cred->security;
  57. if (domain)
  58. atomic_dec(&domain->users);
  59. }
  60. /**
  61. * tomoyo_bprm_set_creds - Target for security_bprm_set_creds().
  62. *
  63. * @bprm: Pointer to "struct linux_binprm".
  64. *
  65. * Returns 0 on success, negative value otherwise.
  66. */
  67. static int tomoyo_bprm_set_creds(struct linux_binprm *bprm)
  68. {
  69. /*
  70. * Do only if this function is called for the first time of an execve
  71. * operation.
  72. */
  73. if (bprm->cred_prepared)
  74. return 0;
  75. #ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
  76. /*
  77. * Load policy if /sbin/tomoyo-init exists and /sbin/init is requested
  78. * for the first time.
  79. */
  80. if (!tomoyo_policy_loaded)
  81. tomoyo_load_policy(bprm->filename);
  82. #endif
  83. /*
  84. * Release reference to "struct tomoyo_domain_info" stored inside
  85. * "bprm->cred->security". New reference to "struct tomoyo_domain_info"
  86. * stored inside "bprm->cred->security" will be acquired later inside
  87. * tomoyo_find_next_domain().
  88. */
  89. atomic_dec(&((struct tomoyo_domain_info *)
  90. bprm->cred->security)->users);
  91. /*
  92. * Tell tomoyo_bprm_check_security() is called for the first time of an
  93. * execve operation.
  94. */
  95. bprm->cred->security = NULL;
  96. return 0;
  97. }
  98. /**
  99. * tomoyo_bprm_check_security - Target for security_bprm_check().
  100. *
  101. * @bprm: Pointer to "struct linux_binprm".
  102. *
  103. * Returns 0 on success, negative value otherwise.
  104. */
  105. static int tomoyo_bprm_check_security(struct linux_binprm *bprm)
  106. {
  107. struct tomoyo_domain_info *domain = bprm->cred->security;
  108. /*
  109. * Execute permission is checked against pathname passed to do_execve()
  110. * using current domain.
  111. */
  112. if (!domain) {
  113. const int idx = tomoyo_read_lock();
  114. const int err = tomoyo_find_next_domain(bprm);
  115. tomoyo_read_unlock(idx);
  116. return err;
  117. }
  118. /*
  119. * Read permission is checked against interpreters using next domain.
  120. */
  121. return tomoyo_check_open_permission(domain, &bprm->file->f_path,
  122. O_RDONLY);
  123. }
  124. /**
  125. * tomoyo_inode_getattr - Target for security_inode_getattr().
  126. *
  127. * @mnt: Pointer to "struct vfsmount".
  128. * @dentry: Pointer to "struct dentry".
  129. *
  130. * Returns 0 on success, negative value otherwise.
  131. */
  132. static int tomoyo_inode_getattr(const struct path *path)
  133. {
  134. return tomoyo_path_perm(TOMOYO_TYPE_GETATTR, path, NULL);
  135. }
  136. /**
  137. * tomoyo_path_truncate - Target for security_path_truncate().
  138. *
  139. * @path: Pointer to "struct path".
  140. *
  141. * Returns 0 on success, negative value otherwise.
  142. */
  143. static int tomoyo_path_truncate(struct path *path)
  144. {
  145. return tomoyo_path_perm(TOMOYO_TYPE_TRUNCATE, path, NULL);
  146. }
  147. /**
  148. * tomoyo_path_unlink - Target for security_path_unlink().
  149. *
  150. * @parent: Pointer to "struct path".
  151. * @dentry: Pointer to "struct dentry".
  152. *
  153. * Returns 0 on success, negative value otherwise.
  154. */
  155. static int tomoyo_path_unlink(struct path *parent, struct dentry *dentry)
  156. {
  157. struct path path = { parent->mnt, dentry };
  158. return tomoyo_path_perm(TOMOYO_TYPE_UNLINK, &path, NULL);
  159. }
  160. /**
  161. * tomoyo_path_mkdir - Target for security_path_mkdir().
  162. *
  163. * @parent: Pointer to "struct path".
  164. * @dentry: Pointer to "struct dentry".
  165. * @mode: DAC permission mode.
  166. *
  167. * Returns 0 on success, negative value otherwise.
  168. */
  169. static int tomoyo_path_mkdir(struct path *parent, struct dentry *dentry,
  170. umode_t mode)
  171. {
  172. struct path path = { parent->mnt, dentry };
  173. return tomoyo_path_number_perm(TOMOYO_TYPE_MKDIR, &path,
  174. mode & S_IALLUGO);
  175. }
  176. /**
  177. * tomoyo_path_rmdir - Target for security_path_rmdir().
  178. *
  179. * @parent: Pointer to "struct path".
  180. * @dentry: Pointer to "struct dentry".
  181. *
  182. * Returns 0 on success, negative value otherwise.
  183. */
  184. static int tomoyo_path_rmdir(struct path *parent, struct dentry *dentry)
  185. {
  186. struct path path = { parent->mnt, dentry };
  187. return tomoyo_path_perm(TOMOYO_TYPE_RMDIR, &path, NULL);
  188. }
  189. /**
  190. * tomoyo_path_symlink - Target for security_path_symlink().
  191. *
  192. * @parent: Pointer to "struct path".
  193. * @dentry: Pointer to "struct dentry".
  194. * @old_name: Symlink's content.
  195. *
  196. * Returns 0 on success, negative value otherwise.
  197. */
  198. static int tomoyo_path_symlink(struct path *parent, struct dentry *dentry,
  199. const char *old_name)
  200. {
  201. struct path path = { parent->mnt, dentry };
  202. return tomoyo_path_perm(TOMOYO_TYPE_SYMLINK, &path, old_name);
  203. }
  204. /**
  205. * tomoyo_path_mknod - Target for security_path_mknod().
  206. *
  207. * @parent: Pointer to "struct path".
  208. * @dentry: Pointer to "struct dentry".
  209. * @mode: DAC permission mode.
  210. * @dev: Device attributes.
  211. *
  212. * Returns 0 on success, negative value otherwise.
  213. */
  214. static int tomoyo_path_mknod(struct path *parent, struct dentry *dentry,
  215. umode_t mode, unsigned int dev)
  216. {
  217. struct path path = { parent->mnt, dentry };
  218. int type = TOMOYO_TYPE_CREATE;
  219. const unsigned int perm = mode & S_IALLUGO;
  220. switch (mode & S_IFMT) {
  221. case S_IFCHR:
  222. type = TOMOYO_TYPE_MKCHAR;
  223. break;
  224. case S_IFBLK:
  225. type = TOMOYO_TYPE_MKBLOCK;
  226. break;
  227. default:
  228. goto no_dev;
  229. }
  230. return tomoyo_mkdev_perm(type, &path, perm, dev);
  231. no_dev:
  232. switch (mode & S_IFMT) {
  233. case S_IFIFO:
  234. type = TOMOYO_TYPE_MKFIFO;
  235. break;
  236. case S_IFSOCK:
  237. type = TOMOYO_TYPE_MKSOCK;
  238. break;
  239. }
  240. return tomoyo_path_number_perm(type, &path, perm);
  241. }
  242. /**
  243. * tomoyo_path_link - Target for security_path_link().
  244. *
  245. * @old_dentry: Pointer to "struct dentry".
  246. * @new_dir: Pointer to "struct path".
  247. * @new_dentry: Pointer to "struct dentry".
  248. *
  249. * Returns 0 on success, negative value otherwise.
  250. */
  251. static int tomoyo_path_link(struct dentry *old_dentry, struct path *new_dir,
  252. struct dentry *new_dentry)
  253. {
  254. struct path path1 = { new_dir->mnt, old_dentry };
  255. struct path path2 = { new_dir->mnt, new_dentry };
  256. return tomoyo_path2_perm(TOMOYO_TYPE_LINK, &path1, &path2);
  257. }
  258. /**
  259. * tomoyo_path_rename - Target for security_path_rename().
  260. *
  261. * @old_parent: Pointer to "struct path".
  262. * @old_dentry: Pointer to "struct dentry".
  263. * @new_parent: Pointer to "struct path".
  264. * @new_dentry: Pointer to "struct dentry".
  265. *
  266. * Returns 0 on success, negative value otherwise.
  267. */
  268. static int tomoyo_path_rename(struct path *old_parent,
  269. struct dentry *old_dentry,
  270. struct path *new_parent,
  271. struct dentry *new_dentry)
  272. {
  273. struct path path1 = { old_parent->mnt, old_dentry };
  274. struct path path2 = { new_parent->mnt, new_dentry };
  275. return tomoyo_path2_perm(TOMOYO_TYPE_RENAME, &path1, &path2);
  276. }
  277. /**
  278. * tomoyo_file_fcntl - Target for security_file_fcntl().
  279. *
  280. * @file: Pointer to "struct file".
  281. * @cmd: Command for fcntl().
  282. * @arg: Argument for @cmd.
  283. *
  284. * Returns 0 on success, negative value otherwise.
  285. */
  286. static int tomoyo_file_fcntl(struct file *file, unsigned int cmd,
  287. unsigned long arg)
  288. {
  289. if (!(cmd == F_SETFL && ((arg ^ file->f_flags) & O_APPEND)))
  290. return 0;
  291. return tomoyo_check_open_permission(tomoyo_domain(), &file->f_path,
  292. O_WRONLY | (arg & O_APPEND));
  293. }
  294. /**
  295. * tomoyo_file_open - Target for security_file_open().
  296. *
  297. * @f: Pointer to "struct file".
  298. * @cred: Pointer to "struct cred".
  299. *
  300. * Returns 0 on success, negative value otherwise.
  301. */
  302. static int tomoyo_file_open(struct file *f, const struct cred *cred)
  303. {
  304. int flags = f->f_flags;
  305. /* Don't check read permission here if called from do_execve(). */
  306. if (current->in_execve)
  307. return 0;
  308. return tomoyo_check_open_permission(tomoyo_domain(), &f->f_path, flags);
  309. }
  310. /**
  311. * tomoyo_file_ioctl - Target for security_file_ioctl().
  312. *
  313. * @file: Pointer to "struct file".
  314. * @cmd: Command for ioctl().
  315. * @arg: Argument for @cmd.
  316. *
  317. * Returns 0 on success, negative value otherwise.
  318. */
  319. static int tomoyo_file_ioctl(struct file *file, unsigned int cmd,
  320. unsigned long arg)
  321. {
  322. return tomoyo_path_number_perm(TOMOYO_TYPE_IOCTL, &file->f_path, cmd);
  323. }
  324. /**
  325. * tomoyo_path_chmod - Target for security_path_chmod().
  326. *
  327. * @path: Pointer to "struct path".
  328. * @mode: DAC permission mode.
  329. *
  330. * Returns 0 on success, negative value otherwise.
  331. */
  332. static int tomoyo_path_chmod(struct path *path, umode_t mode)
  333. {
  334. return tomoyo_path_number_perm(TOMOYO_TYPE_CHMOD, path,
  335. mode & S_IALLUGO);
  336. }
  337. /**
  338. * tomoyo_path_chown - Target for security_path_chown().
  339. *
  340. * @path: Pointer to "struct path".
  341. * @uid: Owner ID.
  342. * @gid: Group ID.
  343. *
  344. * Returns 0 on success, negative value otherwise.
  345. */
  346. static int tomoyo_path_chown(struct path *path, kuid_t uid, kgid_t gid)
  347. {
  348. int error = 0;
  349. if (uid_valid(uid))
  350. error = tomoyo_path_number_perm(TOMOYO_TYPE_CHOWN, path,
  351. from_kuid(&init_user_ns, uid));
  352. if (!error && gid_valid(gid))
  353. error = tomoyo_path_number_perm(TOMOYO_TYPE_CHGRP, path,
  354. from_kgid(&init_user_ns, gid));
  355. return error;
  356. }
  357. /**
  358. * tomoyo_path_chroot - Target for security_path_chroot().
  359. *
  360. * @path: Pointer to "struct path".
  361. *
  362. * Returns 0 on success, negative value otherwise.
  363. */
  364. static int tomoyo_path_chroot(struct path *path)
  365. {
  366. return tomoyo_path_perm(TOMOYO_TYPE_CHROOT, path, NULL);
  367. }
  368. /**
  369. * tomoyo_sb_mount - Target for security_sb_mount().
  370. *
  371. * @dev_name: Name of device file. Maybe NULL.
  372. * @path: Pointer to "struct path".
  373. * @type: Name of filesystem type. Maybe NULL.
  374. * @flags: Mount options.
  375. * @data: Optional data. Maybe NULL.
  376. *
  377. * Returns 0 on success, negative value otherwise.
  378. */
  379. static int tomoyo_sb_mount(const char *dev_name, struct path *path,
  380. const char *type, unsigned long flags, void *data)
  381. {
  382. return tomoyo_mount_permission(dev_name, path, type, flags, data);
  383. }
  384. /**
  385. * tomoyo_sb_umount - Target for security_sb_umount().
  386. *
  387. * @mnt: Pointer to "struct vfsmount".
  388. * @flags: Unmount options.
  389. *
  390. * Returns 0 on success, negative value otherwise.
  391. */
  392. static int tomoyo_sb_umount(struct vfsmount *mnt, int flags)
  393. {
  394. struct path path = { mnt, mnt->mnt_root };
  395. return tomoyo_path_perm(TOMOYO_TYPE_UMOUNT, &path, NULL);
  396. }
  397. /**
  398. * tomoyo_sb_pivotroot - Target for security_sb_pivotroot().
  399. *
  400. * @old_path: Pointer to "struct path".
  401. * @new_path: Pointer to "struct path".
  402. *
  403. * Returns 0 on success, negative value otherwise.
  404. */
  405. static int tomoyo_sb_pivotroot(struct path *old_path, struct path *new_path)
  406. {
  407. return tomoyo_path2_perm(TOMOYO_TYPE_PIVOT_ROOT, new_path, old_path);
  408. }
  409. /**
  410. * tomoyo_socket_listen - Check permission for listen().
  411. *
  412. * @sock: Pointer to "struct socket".
  413. * @backlog: Backlog parameter.
  414. *
  415. * Returns 0 on success, negative value otherwise.
  416. */
  417. static int tomoyo_socket_listen(struct socket *sock, int backlog)
  418. {
  419. return tomoyo_socket_listen_permission(sock);
  420. }
  421. /**
  422. * tomoyo_socket_connect - Check permission for connect().
  423. *
  424. * @sock: Pointer to "struct socket".
  425. * @addr: Pointer to "struct sockaddr".
  426. * @addr_len: Size of @addr.
  427. *
  428. * Returns 0 on success, negative value otherwise.
  429. */
  430. static int tomoyo_socket_connect(struct socket *sock, struct sockaddr *addr,
  431. int addr_len)
  432. {
  433. return tomoyo_socket_connect_permission(sock, addr, addr_len);
  434. }
  435. /**
  436. * tomoyo_socket_bind - Check permission for bind().
  437. *
  438. * @sock: Pointer to "struct socket".
  439. * @addr: Pointer to "struct sockaddr".
  440. * @addr_len: Size of @addr.
  441. *
  442. * Returns 0 on success, negative value otherwise.
  443. */
  444. static int tomoyo_socket_bind(struct socket *sock, struct sockaddr *addr,
  445. int addr_len)
  446. {
  447. return tomoyo_socket_bind_permission(sock, addr, addr_len);
  448. }
  449. /**
  450. * tomoyo_socket_sendmsg - Check permission for sendmsg().
  451. *
  452. * @sock: Pointer to "struct socket".
  453. * @msg: Pointer to "struct msghdr".
  454. * @size: Size of message.
  455. *
  456. * Returns 0 on success, negative value otherwise.
  457. */
  458. static int tomoyo_socket_sendmsg(struct socket *sock, struct msghdr *msg,
  459. int size)
  460. {
  461. return tomoyo_socket_sendmsg_permission(sock, msg, size);
  462. }
  463. /*
  464. * tomoyo_security_ops is a "struct security_operations" which is used for
  465. * registering TOMOYO.
  466. */
  467. static struct security_hook_list tomoyo_hooks[] = {
  468. LSM_HOOK_INIT(cred_alloc_blank, tomoyo_cred_alloc_blank),
  469. LSM_HOOK_INIT(cred_prepare, tomoyo_cred_prepare),
  470. LSM_HOOK_INIT(cred_transfer, tomoyo_cred_transfer),
  471. LSM_HOOK_INIT(cred_free, tomoyo_cred_free),
  472. LSM_HOOK_INIT(bprm_set_creds, tomoyo_bprm_set_creds),
  473. LSM_HOOK_INIT(bprm_check_security, tomoyo_bprm_check_security),
  474. LSM_HOOK_INIT(file_fcntl, tomoyo_file_fcntl),
  475. LSM_HOOK_INIT(file_open, tomoyo_file_open),
  476. LSM_HOOK_INIT(path_truncate, tomoyo_path_truncate),
  477. LSM_HOOK_INIT(path_unlink, tomoyo_path_unlink),
  478. LSM_HOOK_INIT(path_mkdir, tomoyo_path_mkdir),
  479. LSM_HOOK_INIT(path_rmdir, tomoyo_path_rmdir),
  480. LSM_HOOK_INIT(path_symlink, tomoyo_path_symlink),
  481. LSM_HOOK_INIT(path_mknod, tomoyo_path_mknod),
  482. LSM_HOOK_INIT(path_link, tomoyo_path_link),
  483. LSM_HOOK_INIT(path_rename, tomoyo_path_rename),
  484. LSM_HOOK_INIT(inode_getattr, tomoyo_inode_getattr),
  485. LSM_HOOK_INIT(file_ioctl, tomoyo_file_ioctl),
  486. LSM_HOOK_INIT(path_chmod, tomoyo_path_chmod),
  487. LSM_HOOK_INIT(path_chown, tomoyo_path_chown),
  488. LSM_HOOK_INIT(path_chroot, tomoyo_path_chroot),
  489. LSM_HOOK_INIT(sb_mount, tomoyo_sb_mount),
  490. LSM_HOOK_INIT(sb_umount, tomoyo_sb_umount),
  491. LSM_HOOK_INIT(sb_pivotroot, tomoyo_sb_pivotroot),
  492. LSM_HOOK_INIT(socket_bind, tomoyo_socket_bind),
  493. LSM_HOOK_INIT(socket_connect, tomoyo_socket_connect),
  494. LSM_HOOK_INIT(socket_listen, tomoyo_socket_listen),
  495. LSM_HOOK_INIT(socket_sendmsg, tomoyo_socket_sendmsg),
  496. };
  497. /* Lock for GC. */
  498. DEFINE_SRCU(tomoyo_ss);
  499. /**
  500. * tomoyo_init - Register TOMOYO Linux as a LSM module.
  501. *
  502. * Returns 0.
  503. */
  504. static int __init tomoyo_init(void)
  505. {
  506. struct cred *cred = (struct cred *) current_cred();
  507. if (!security_module_enable("tomoyo"))
  508. return 0;
  509. /* register ourselves with the security framework */
  510. security_add_hooks(tomoyo_hooks, ARRAY_SIZE(tomoyo_hooks));
  511. printk(KERN_INFO "TOMOYO Linux initialized\n");
  512. cred->security = &tomoyo_kernel_domain;
  513. tomoyo_mm_init();
  514. return 0;
  515. }
  516. security_initcall(tomoyo_init);