tomoyo.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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. umode_t 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. umode_t 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. * @path: Pointer to "struct path".
  333. * @mode: DAC permission mode.
  334. *
  335. * Returns 0 on success, negative value otherwise.
  336. */
  337. static int tomoyo_path_chmod(struct path *path, umode_t mode)
  338. {
  339. return tomoyo_path_number_perm(TOMOYO_TYPE_CHMOD, path,
  340. mode & S_IALLUGO);
  341. }
  342. /**
  343. * tomoyo_path_chown - Target for security_path_chown().
  344. *
  345. * @path: Pointer to "struct path".
  346. * @uid: Owner ID.
  347. * @gid: Group ID.
  348. *
  349. * Returns 0 on success, negative value otherwise.
  350. */
  351. static int tomoyo_path_chown(struct path *path, uid_t uid, gid_t gid)
  352. {
  353. int error = 0;
  354. if (uid != (uid_t) -1)
  355. error = tomoyo_path_number_perm(TOMOYO_TYPE_CHOWN, path, uid);
  356. if (!error && gid != (gid_t) -1)
  357. error = tomoyo_path_number_perm(TOMOYO_TYPE_CHGRP, path, gid);
  358. return error;
  359. }
  360. /**
  361. * tomoyo_path_chroot - Target for security_path_chroot().
  362. *
  363. * @path: Pointer to "struct path".
  364. *
  365. * Returns 0 on success, negative value otherwise.
  366. */
  367. static int tomoyo_path_chroot(struct path *path)
  368. {
  369. return tomoyo_path_perm(TOMOYO_TYPE_CHROOT, path, NULL);
  370. }
  371. /**
  372. * tomoyo_sb_mount - Target for security_sb_mount().
  373. *
  374. * @dev_name: Name of device file. Maybe NULL.
  375. * @path: Pointer to "struct path".
  376. * @type: Name of filesystem type. Maybe NULL.
  377. * @flags: Mount options.
  378. * @data: Optional data. Maybe NULL.
  379. *
  380. * Returns 0 on success, negative value otherwise.
  381. */
  382. static int tomoyo_sb_mount(const char *dev_name, struct path *path,
  383. const char *type, unsigned long flags, void *data)
  384. {
  385. return tomoyo_mount_permission(dev_name, path, type, flags, data);
  386. }
  387. /**
  388. * tomoyo_sb_umount - Target for security_sb_umount().
  389. *
  390. * @mnt: Pointer to "struct vfsmount".
  391. * @flags: Unmount options.
  392. *
  393. * Returns 0 on success, negative value otherwise.
  394. */
  395. static int tomoyo_sb_umount(struct vfsmount *mnt, int flags)
  396. {
  397. struct path path = { mnt, mnt->mnt_root };
  398. return tomoyo_path_perm(TOMOYO_TYPE_UMOUNT, &path, NULL);
  399. }
  400. /**
  401. * tomoyo_sb_pivotroot - Target for security_sb_pivotroot().
  402. *
  403. * @old_path: Pointer to "struct path".
  404. * @new_path: Pointer to "struct path".
  405. *
  406. * Returns 0 on success, negative value otherwise.
  407. */
  408. static int tomoyo_sb_pivotroot(struct path *old_path, struct path *new_path)
  409. {
  410. return tomoyo_path2_perm(TOMOYO_TYPE_PIVOT_ROOT, new_path, old_path);
  411. }
  412. /**
  413. * tomoyo_socket_listen - Check permission for listen().
  414. *
  415. * @sock: Pointer to "struct socket".
  416. * @backlog: Backlog parameter.
  417. *
  418. * Returns 0 on success, negative value otherwise.
  419. */
  420. static int tomoyo_socket_listen(struct socket *sock, int backlog)
  421. {
  422. return tomoyo_socket_listen_permission(sock);
  423. }
  424. /**
  425. * tomoyo_socket_connect - Check permission for connect().
  426. *
  427. * @sock: Pointer to "struct socket".
  428. * @addr: Pointer to "struct sockaddr".
  429. * @addr_len: Size of @addr.
  430. *
  431. * Returns 0 on success, negative value otherwise.
  432. */
  433. static int tomoyo_socket_connect(struct socket *sock, struct sockaddr *addr,
  434. int addr_len)
  435. {
  436. return tomoyo_socket_connect_permission(sock, addr, addr_len);
  437. }
  438. /**
  439. * tomoyo_socket_bind - Check permission for bind().
  440. *
  441. * @sock: Pointer to "struct socket".
  442. * @addr: Pointer to "struct sockaddr".
  443. * @addr_len: Size of @addr.
  444. *
  445. * Returns 0 on success, negative value otherwise.
  446. */
  447. static int tomoyo_socket_bind(struct socket *sock, struct sockaddr *addr,
  448. int addr_len)
  449. {
  450. return tomoyo_socket_bind_permission(sock, addr, addr_len);
  451. }
  452. /**
  453. * tomoyo_socket_sendmsg - Check permission for sendmsg().
  454. *
  455. * @sock: Pointer to "struct socket".
  456. * @msg: Pointer to "struct msghdr".
  457. * @size: Size of message.
  458. *
  459. * Returns 0 on success, negative value otherwise.
  460. */
  461. static int tomoyo_socket_sendmsg(struct socket *sock, struct msghdr *msg,
  462. int size)
  463. {
  464. return tomoyo_socket_sendmsg_permission(sock, msg, size);
  465. }
  466. /*
  467. * tomoyo_security_ops is a "struct security_operations" which is used for
  468. * registering TOMOYO.
  469. */
  470. static struct security_operations tomoyo_security_ops = {
  471. .name = "tomoyo",
  472. .cred_alloc_blank = tomoyo_cred_alloc_blank,
  473. .cred_prepare = tomoyo_cred_prepare,
  474. .cred_transfer = tomoyo_cred_transfer,
  475. .cred_free = tomoyo_cred_free,
  476. .bprm_set_creds = tomoyo_bprm_set_creds,
  477. .bprm_check_security = tomoyo_bprm_check_security,
  478. .file_fcntl = tomoyo_file_fcntl,
  479. .dentry_open = tomoyo_dentry_open,
  480. .path_truncate = tomoyo_path_truncate,
  481. .path_unlink = tomoyo_path_unlink,
  482. .path_mkdir = tomoyo_path_mkdir,
  483. .path_rmdir = tomoyo_path_rmdir,
  484. .path_symlink = tomoyo_path_symlink,
  485. .path_mknod = tomoyo_path_mknod,
  486. .path_link = tomoyo_path_link,
  487. .path_rename = tomoyo_path_rename,
  488. .inode_getattr = tomoyo_inode_getattr,
  489. .file_ioctl = tomoyo_file_ioctl,
  490. .path_chmod = tomoyo_path_chmod,
  491. .path_chown = tomoyo_path_chown,
  492. .path_chroot = tomoyo_path_chroot,
  493. .sb_mount = tomoyo_sb_mount,
  494. .sb_umount = tomoyo_sb_umount,
  495. .sb_pivotroot = tomoyo_sb_pivotroot,
  496. .socket_bind = tomoyo_socket_bind,
  497. .socket_connect = tomoyo_socket_connect,
  498. .socket_listen = tomoyo_socket_listen,
  499. .socket_sendmsg = tomoyo_socket_sendmsg,
  500. };
  501. /* Lock for GC. */
  502. struct srcu_struct tomoyo_ss;
  503. /**
  504. * tomoyo_init - Register TOMOYO Linux as a LSM module.
  505. *
  506. * Returns 0.
  507. */
  508. static int __init tomoyo_init(void)
  509. {
  510. struct cred *cred = (struct cred *) current_cred();
  511. if (!security_module_enable(&tomoyo_security_ops))
  512. return 0;
  513. /* register ourselves with the security framework */
  514. if (register_security(&tomoyo_security_ops) ||
  515. init_srcu_struct(&tomoyo_ss))
  516. panic("Failure registering TOMOYO Linux");
  517. printk(KERN_INFO "TOMOYO Linux initialized\n");
  518. cred->security = &tomoyo_kernel_domain;
  519. tomoyo_mm_init();
  520. return 0;
  521. }
  522. security_initcall(tomoyo_init);