tomoyo.c 15 KB

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