tomoyo.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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(const struct path *path)
  137. {
  138. return tomoyo_path_perm(TOMOYO_TYPE_GETATTR, path, NULL);
  139. }
  140. /**
  141. * tomoyo_path_truncate - Target for security_path_truncate().
  142. *
  143. * @path: Pointer to "struct path".
  144. *
  145. * Returns 0 on success, negative value otherwise.
  146. */
  147. static int tomoyo_path_truncate(struct path *path)
  148. {
  149. return tomoyo_path_perm(TOMOYO_TYPE_TRUNCATE, path, NULL);
  150. }
  151. /**
  152. * tomoyo_path_unlink - Target for security_path_unlink().
  153. *
  154. * @parent: Pointer to "struct path".
  155. * @dentry: Pointer to "struct dentry".
  156. *
  157. * Returns 0 on success, negative value otherwise.
  158. */
  159. static int tomoyo_path_unlink(struct path *parent, struct dentry *dentry)
  160. {
  161. struct path path = { parent->mnt, dentry };
  162. return tomoyo_path_perm(TOMOYO_TYPE_UNLINK, &path, NULL);
  163. }
  164. /**
  165. * tomoyo_path_mkdir - Target for security_path_mkdir().
  166. *
  167. * @parent: Pointer to "struct path".
  168. * @dentry: Pointer to "struct dentry".
  169. * @mode: DAC permission mode.
  170. *
  171. * Returns 0 on success, negative value otherwise.
  172. */
  173. static int tomoyo_path_mkdir(struct path *parent, struct dentry *dentry,
  174. umode_t mode)
  175. {
  176. struct path path = { parent->mnt, dentry };
  177. return tomoyo_path_number_perm(TOMOYO_TYPE_MKDIR, &path,
  178. mode & S_IALLUGO);
  179. }
  180. /**
  181. * tomoyo_path_rmdir - Target for security_path_rmdir().
  182. *
  183. * @parent: Pointer to "struct path".
  184. * @dentry: Pointer to "struct dentry".
  185. *
  186. * Returns 0 on success, negative value otherwise.
  187. */
  188. static int tomoyo_path_rmdir(struct path *parent, struct dentry *dentry)
  189. {
  190. struct path path = { parent->mnt, dentry };
  191. return tomoyo_path_perm(TOMOYO_TYPE_RMDIR, &path, NULL);
  192. }
  193. /**
  194. * tomoyo_path_symlink - Target for security_path_symlink().
  195. *
  196. * @parent: Pointer to "struct path".
  197. * @dentry: Pointer to "struct dentry".
  198. * @old_name: Symlink's content.
  199. *
  200. * Returns 0 on success, negative value otherwise.
  201. */
  202. static int tomoyo_path_symlink(struct path *parent, struct dentry *dentry,
  203. const char *old_name)
  204. {
  205. struct path path = { parent->mnt, dentry };
  206. return tomoyo_path_perm(TOMOYO_TYPE_SYMLINK, &path, old_name);
  207. }
  208. /**
  209. * tomoyo_path_mknod - Target for security_path_mknod().
  210. *
  211. * @parent: Pointer to "struct path".
  212. * @dentry: Pointer to "struct dentry".
  213. * @mode: DAC permission mode.
  214. * @dev: Device attributes.
  215. *
  216. * Returns 0 on success, negative value otherwise.
  217. */
  218. static int tomoyo_path_mknod(struct path *parent, struct dentry *dentry,
  219. umode_t mode, unsigned int dev)
  220. {
  221. struct path path = { parent->mnt, dentry };
  222. int type = TOMOYO_TYPE_CREATE;
  223. const unsigned int perm = mode & S_IALLUGO;
  224. switch (mode & S_IFMT) {
  225. case S_IFCHR:
  226. type = TOMOYO_TYPE_MKCHAR;
  227. break;
  228. case S_IFBLK:
  229. type = TOMOYO_TYPE_MKBLOCK;
  230. break;
  231. default:
  232. goto no_dev;
  233. }
  234. return tomoyo_mkdev_perm(type, &path, perm, dev);
  235. no_dev:
  236. switch (mode & S_IFMT) {
  237. case S_IFIFO:
  238. type = TOMOYO_TYPE_MKFIFO;
  239. break;
  240. case S_IFSOCK:
  241. type = TOMOYO_TYPE_MKSOCK;
  242. break;
  243. }
  244. return tomoyo_path_number_perm(type, &path, perm);
  245. }
  246. /**
  247. * tomoyo_path_link - Target for security_path_link().
  248. *
  249. * @old_dentry: Pointer to "struct dentry".
  250. * @new_dir: Pointer to "struct path".
  251. * @new_dentry: Pointer to "struct dentry".
  252. *
  253. * Returns 0 on success, negative value otherwise.
  254. */
  255. static int tomoyo_path_link(struct dentry *old_dentry, struct path *new_dir,
  256. struct dentry *new_dentry)
  257. {
  258. struct path path1 = { new_dir->mnt, old_dentry };
  259. struct path path2 = { new_dir->mnt, new_dentry };
  260. return tomoyo_path2_perm(TOMOYO_TYPE_LINK, &path1, &path2);
  261. }
  262. /**
  263. * tomoyo_path_rename - Target for security_path_rename().
  264. *
  265. * @old_parent: Pointer to "struct path".
  266. * @old_dentry: Pointer to "struct dentry".
  267. * @new_parent: Pointer to "struct path".
  268. * @new_dentry: Pointer to "struct dentry".
  269. *
  270. * Returns 0 on success, negative value otherwise.
  271. */
  272. static int tomoyo_path_rename(struct path *old_parent,
  273. struct dentry *old_dentry,
  274. struct path *new_parent,
  275. struct dentry *new_dentry)
  276. {
  277. struct path path1 = { old_parent->mnt, old_dentry };
  278. struct path path2 = { new_parent->mnt, new_dentry };
  279. return tomoyo_path2_perm(TOMOYO_TYPE_RENAME, &path1, &path2);
  280. }
  281. /**
  282. * tomoyo_file_fcntl - Target for security_file_fcntl().
  283. *
  284. * @file: Pointer to "struct file".
  285. * @cmd: Command for fcntl().
  286. * @arg: Argument for @cmd.
  287. *
  288. * Returns 0 on success, negative value otherwise.
  289. */
  290. static int tomoyo_file_fcntl(struct file *file, unsigned int cmd,
  291. unsigned long arg)
  292. {
  293. if (!(cmd == F_SETFL && ((arg ^ file->f_flags) & O_APPEND)))
  294. return 0;
  295. return tomoyo_check_open_permission(tomoyo_domain(), &file->f_path,
  296. O_WRONLY | (arg & O_APPEND));
  297. }
  298. /**
  299. * tomoyo_file_open - Target for security_file_open().
  300. *
  301. * @f: Pointer to "struct file".
  302. * @cred: Pointer to "struct cred".
  303. *
  304. * Returns 0 on success, negative value otherwise.
  305. */
  306. static int tomoyo_file_open(struct file *f, const struct cred *cred)
  307. {
  308. int flags = f->f_flags;
  309. /* Don't check read permission here if called from do_execve(). */
  310. if (current->in_execve)
  311. return 0;
  312. return tomoyo_check_open_permission(tomoyo_domain(), &f->f_path, flags);
  313. }
  314. /**
  315. * tomoyo_file_ioctl - Target for security_file_ioctl().
  316. *
  317. * @file: Pointer to "struct file".
  318. * @cmd: Command for ioctl().
  319. * @arg: Argument for @cmd.
  320. *
  321. * Returns 0 on success, negative value otherwise.
  322. */
  323. static int tomoyo_file_ioctl(struct file *file, unsigned int cmd,
  324. unsigned long arg)
  325. {
  326. return tomoyo_path_number_perm(TOMOYO_TYPE_IOCTL, &file->f_path, cmd);
  327. }
  328. /**
  329. * tomoyo_path_chmod - Target for security_path_chmod().
  330. *
  331. * @path: Pointer to "struct path".
  332. * @mode: DAC permission mode.
  333. *
  334. * Returns 0 on success, negative value otherwise.
  335. */
  336. static int tomoyo_path_chmod(struct path *path, umode_t mode)
  337. {
  338. return tomoyo_path_number_perm(TOMOYO_TYPE_CHMOD, path,
  339. mode & S_IALLUGO);
  340. }
  341. /**
  342. * tomoyo_path_chown - Target for security_path_chown().
  343. *
  344. * @path: Pointer to "struct path".
  345. * @uid: Owner ID.
  346. * @gid: Group ID.
  347. *
  348. * Returns 0 on success, negative value otherwise.
  349. */
  350. static int tomoyo_path_chown(struct path *path, kuid_t uid, kgid_t gid)
  351. {
  352. int error = 0;
  353. if (uid_valid(uid))
  354. error = tomoyo_path_number_perm(TOMOYO_TYPE_CHOWN, path,
  355. from_kuid(&init_user_ns, uid));
  356. if (!error && gid_valid(gid))
  357. error = tomoyo_path_number_perm(TOMOYO_TYPE_CHGRP, path,
  358. from_kgid(&init_user_ns, gid));
  359. return error;
  360. }
  361. /**
  362. * tomoyo_path_chroot - Target for security_path_chroot().
  363. *
  364. * @path: Pointer to "struct path".
  365. *
  366. * Returns 0 on success, negative value otherwise.
  367. */
  368. static int tomoyo_path_chroot(struct path *path)
  369. {
  370. return tomoyo_path_perm(TOMOYO_TYPE_CHROOT, path, NULL);
  371. }
  372. /**
  373. * tomoyo_sb_mount - Target for security_sb_mount().
  374. *
  375. * @dev_name: Name of device file. Maybe NULL.
  376. * @path: Pointer to "struct path".
  377. * @type: Name of filesystem type. Maybe NULL.
  378. * @flags: Mount options.
  379. * @data: Optional data. Maybe NULL.
  380. *
  381. * Returns 0 on success, negative value otherwise.
  382. */
  383. static int tomoyo_sb_mount(const char *dev_name, struct path *path,
  384. const char *type, unsigned long flags, void *data)
  385. {
  386. return tomoyo_mount_permission(dev_name, path, type, flags, data);
  387. }
  388. /**
  389. * tomoyo_sb_umount - Target for security_sb_umount().
  390. *
  391. * @mnt: Pointer to "struct vfsmount".
  392. * @flags: Unmount options.
  393. *
  394. * Returns 0 on success, negative value otherwise.
  395. */
  396. static int tomoyo_sb_umount(struct vfsmount *mnt, int flags)
  397. {
  398. struct path path = { mnt, mnt->mnt_root };
  399. return tomoyo_path_perm(TOMOYO_TYPE_UMOUNT, &path, NULL);
  400. }
  401. /**
  402. * tomoyo_sb_pivotroot - Target for security_sb_pivotroot().
  403. *
  404. * @old_path: Pointer to "struct path".
  405. * @new_path: Pointer to "struct path".
  406. *
  407. * Returns 0 on success, negative value otherwise.
  408. */
  409. static int tomoyo_sb_pivotroot(struct path *old_path, struct path *new_path)
  410. {
  411. return tomoyo_path2_perm(TOMOYO_TYPE_PIVOT_ROOT, new_path, old_path);
  412. }
  413. /**
  414. * tomoyo_socket_listen - Check permission for listen().
  415. *
  416. * @sock: Pointer to "struct socket".
  417. * @backlog: Backlog parameter.
  418. *
  419. * Returns 0 on success, negative value otherwise.
  420. */
  421. static int tomoyo_socket_listen(struct socket *sock, int backlog)
  422. {
  423. return tomoyo_socket_listen_permission(sock);
  424. }
  425. /**
  426. * tomoyo_socket_connect - Check permission for connect().
  427. *
  428. * @sock: Pointer to "struct socket".
  429. * @addr: Pointer to "struct sockaddr".
  430. * @addr_len: Size of @addr.
  431. *
  432. * Returns 0 on success, negative value otherwise.
  433. */
  434. static int tomoyo_socket_connect(struct socket *sock, struct sockaddr *addr,
  435. int addr_len)
  436. {
  437. return tomoyo_socket_connect_permission(sock, addr, addr_len);
  438. }
  439. /**
  440. * tomoyo_socket_bind - Check permission for bind().
  441. *
  442. * @sock: Pointer to "struct socket".
  443. * @addr: Pointer to "struct sockaddr".
  444. * @addr_len: Size of @addr.
  445. *
  446. * Returns 0 on success, negative value otherwise.
  447. */
  448. static int tomoyo_socket_bind(struct socket *sock, struct sockaddr *addr,
  449. int addr_len)
  450. {
  451. return tomoyo_socket_bind_permission(sock, addr, addr_len);
  452. }
  453. /**
  454. * tomoyo_socket_sendmsg - Check permission for sendmsg().
  455. *
  456. * @sock: Pointer to "struct socket".
  457. * @msg: Pointer to "struct msghdr".
  458. * @size: Size of message.
  459. *
  460. * Returns 0 on success, negative value otherwise.
  461. */
  462. static int tomoyo_socket_sendmsg(struct socket *sock, struct msghdr *msg,
  463. int size)
  464. {
  465. return tomoyo_socket_sendmsg_permission(sock, msg, size);
  466. }
  467. /*
  468. * tomoyo_security_ops is a "struct security_operations" which is used for
  469. * registering TOMOYO.
  470. */
  471. static struct security_operations tomoyo_security_ops = {
  472. .name = "tomoyo",
  473. .cred_alloc_blank = tomoyo_cred_alloc_blank,
  474. .cred_prepare = tomoyo_cred_prepare,
  475. .cred_transfer = tomoyo_cred_transfer,
  476. .cred_free = tomoyo_cred_free,
  477. .bprm_set_creds = tomoyo_bprm_set_creds,
  478. .bprm_check_security = tomoyo_bprm_check_security,
  479. .file_fcntl = tomoyo_file_fcntl,
  480. .file_open = tomoyo_file_open,
  481. .path_truncate = tomoyo_path_truncate,
  482. .path_unlink = tomoyo_path_unlink,
  483. .path_mkdir = tomoyo_path_mkdir,
  484. .path_rmdir = tomoyo_path_rmdir,
  485. .path_symlink = tomoyo_path_symlink,
  486. .path_mknod = tomoyo_path_mknod,
  487. .path_link = tomoyo_path_link,
  488. .path_rename = tomoyo_path_rename,
  489. .inode_getattr = tomoyo_inode_getattr,
  490. .file_ioctl = tomoyo_file_ioctl,
  491. .path_chmod = tomoyo_path_chmod,
  492. .path_chown = tomoyo_path_chown,
  493. .path_chroot = tomoyo_path_chroot,
  494. .sb_mount = tomoyo_sb_mount,
  495. .sb_umount = tomoyo_sb_umount,
  496. .sb_pivotroot = tomoyo_sb_pivotroot,
  497. .socket_bind = tomoyo_socket_bind,
  498. .socket_connect = tomoyo_socket_connect,
  499. .socket_listen = tomoyo_socket_listen,
  500. .socket_sendmsg = tomoyo_socket_sendmsg,
  501. };
  502. /* Lock for GC. */
  503. DEFINE_SRCU(tomoyo_ss);
  504. /**
  505. * tomoyo_init - Register TOMOYO Linux as a LSM module.
  506. *
  507. * Returns 0.
  508. */
  509. static int __init tomoyo_init(void)
  510. {
  511. struct cred *cred = (struct cred *) current_cred();
  512. if (!security_module_enable(&tomoyo_security_ops))
  513. return 0;
  514. /* register ourselves with the security framework */
  515. if (register_security(&tomoyo_security_ops))
  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);