tomoyo.c 15 KB

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