file.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431
  1. /*
  2. * security/tomoyo/file.c
  3. *
  4. * Pathname restriction functions.
  5. *
  6. * Copyright (C) 2005-2010 NTT DATA CORPORATION
  7. */
  8. #include "common.h"
  9. #include <linux/slab.h>
  10. /* Keyword array for operations with one pathname. */
  11. static const char *tomoyo_path_keyword[TOMOYO_MAX_PATH_OPERATION] = {
  12. [TOMOYO_TYPE_READ_WRITE] = "read/write",
  13. [TOMOYO_TYPE_EXECUTE] = "execute",
  14. [TOMOYO_TYPE_READ] = "read",
  15. [TOMOYO_TYPE_WRITE] = "write",
  16. [TOMOYO_TYPE_UNLINK] = "unlink",
  17. [TOMOYO_TYPE_RMDIR] = "rmdir",
  18. [TOMOYO_TYPE_TRUNCATE] = "truncate",
  19. [TOMOYO_TYPE_SYMLINK] = "symlink",
  20. [TOMOYO_TYPE_REWRITE] = "rewrite",
  21. [TOMOYO_TYPE_CHROOT] = "chroot",
  22. [TOMOYO_TYPE_UMOUNT] = "unmount",
  23. };
  24. /* Keyword array for operations with one pathname and three numbers. */
  25. static const char *tomoyo_mkdev_keyword
  26. [TOMOYO_MAX_MKDEV_OPERATION] = {
  27. [TOMOYO_TYPE_MKBLOCK] = "mkblock",
  28. [TOMOYO_TYPE_MKCHAR] = "mkchar",
  29. };
  30. /* Keyword array for operations with two pathnames. */
  31. static const char *tomoyo_path2_keyword[TOMOYO_MAX_PATH2_OPERATION] = {
  32. [TOMOYO_TYPE_LINK] = "link",
  33. [TOMOYO_TYPE_RENAME] = "rename",
  34. [TOMOYO_TYPE_PIVOT_ROOT] = "pivot_root",
  35. };
  36. /* Keyword array for operations with one pathname and one number. */
  37. static const char *tomoyo_path_number_keyword
  38. [TOMOYO_MAX_PATH_NUMBER_OPERATION] = {
  39. [TOMOYO_TYPE_CREATE] = "create",
  40. [TOMOYO_TYPE_MKDIR] = "mkdir",
  41. [TOMOYO_TYPE_MKFIFO] = "mkfifo",
  42. [TOMOYO_TYPE_MKSOCK] = "mksock",
  43. [TOMOYO_TYPE_IOCTL] = "ioctl",
  44. [TOMOYO_TYPE_CHMOD] = "chmod",
  45. [TOMOYO_TYPE_CHOWN] = "chown",
  46. [TOMOYO_TYPE_CHGRP] = "chgrp",
  47. };
  48. static const u8 tomoyo_p2mac[TOMOYO_MAX_PATH_OPERATION] = {
  49. [TOMOYO_TYPE_READ_WRITE] = TOMOYO_MAC_FILE_OPEN,
  50. [TOMOYO_TYPE_EXECUTE] = TOMOYO_MAC_FILE_EXECUTE,
  51. [TOMOYO_TYPE_READ] = TOMOYO_MAC_FILE_OPEN,
  52. [TOMOYO_TYPE_WRITE] = TOMOYO_MAC_FILE_OPEN,
  53. [TOMOYO_TYPE_UNLINK] = TOMOYO_MAC_FILE_UNLINK,
  54. [TOMOYO_TYPE_RMDIR] = TOMOYO_MAC_FILE_RMDIR,
  55. [TOMOYO_TYPE_TRUNCATE] = TOMOYO_MAC_FILE_TRUNCATE,
  56. [TOMOYO_TYPE_SYMLINK] = TOMOYO_MAC_FILE_SYMLINK,
  57. [TOMOYO_TYPE_REWRITE] = TOMOYO_MAC_FILE_REWRITE,
  58. [TOMOYO_TYPE_CHROOT] = TOMOYO_MAC_FILE_CHROOT,
  59. [TOMOYO_TYPE_UMOUNT] = TOMOYO_MAC_FILE_UMOUNT,
  60. };
  61. static const u8 tomoyo_pnnn2mac[TOMOYO_MAX_MKDEV_OPERATION] = {
  62. [TOMOYO_TYPE_MKBLOCK] = TOMOYO_MAC_FILE_MKBLOCK,
  63. [TOMOYO_TYPE_MKCHAR] = TOMOYO_MAC_FILE_MKCHAR,
  64. };
  65. static const u8 tomoyo_pp2mac[TOMOYO_MAX_PATH2_OPERATION] = {
  66. [TOMOYO_TYPE_LINK] = TOMOYO_MAC_FILE_LINK,
  67. [TOMOYO_TYPE_RENAME] = TOMOYO_MAC_FILE_RENAME,
  68. [TOMOYO_TYPE_PIVOT_ROOT] = TOMOYO_MAC_FILE_PIVOT_ROOT,
  69. };
  70. static const u8 tomoyo_pn2mac[TOMOYO_MAX_PATH_NUMBER_OPERATION] = {
  71. [TOMOYO_TYPE_CREATE] = TOMOYO_MAC_FILE_CREATE,
  72. [TOMOYO_TYPE_MKDIR] = TOMOYO_MAC_FILE_MKDIR,
  73. [TOMOYO_TYPE_MKFIFO] = TOMOYO_MAC_FILE_MKFIFO,
  74. [TOMOYO_TYPE_MKSOCK] = TOMOYO_MAC_FILE_MKSOCK,
  75. [TOMOYO_TYPE_IOCTL] = TOMOYO_MAC_FILE_IOCTL,
  76. [TOMOYO_TYPE_CHMOD] = TOMOYO_MAC_FILE_CHMOD,
  77. [TOMOYO_TYPE_CHOWN] = TOMOYO_MAC_FILE_CHOWN,
  78. [TOMOYO_TYPE_CHGRP] = TOMOYO_MAC_FILE_CHGRP,
  79. };
  80. void tomoyo_put_name_union(struct tomoyo_name_union *ptr)
  81. {
  82. if (!ptr)
  83. return;
  84. if (ptr->is_group)
  85. tomoyo_put_path_group(ptr->group);
  86. else
  87. tomoyo_put_name(ptr->filename);
  88. }
  89. bool tomoyo_compare_name_union(const struct tomoyo_path_info *name,
  90. const struct tomoyo_name_union *ptr)
  91. {
  92. if (ptr->is_group)
  93. return tomoyo_path_matches_group(name, ptr->group);
  94. return tomoyo_path_matches_pattern(name, ptr->filename);
  95. }
  96. void tomoyo_put_number_union(struct tomoyo_number_union *ptr)
  97. {
  98. if (ptr && ptr->is_group)
  99. tomoyo_put_number_group(ptr->group);
  100. }
  101. bool tomoyo_compare_number_union(const unsigned long value,
  102. const struct tomoyo_number_union *ptr)
  103. {
  104. if (ptr->is_group)
  105. return tomoyo_number_matches_group(value, value, ptr->group);
  106. return value >= ptr->values[0] && value <= ptr->values[1];
  107. }
  108. /**
  109. * tomoyo_path2keyword - Get the name of single path operation.
  110. *
  111. * @operation: Type of operation.
  112. *
  113. * Returns the name of single path operation.
  114. */
  115. const char *tomoyo_path2keyword(const u8 operation)
  116. {
  117. return (operation < TOMOYO_MAX_PATH_OPERATION)
  118. ? tomoyo_path_keyword[operation] : NULL;
  119. }
  120. /**
  121. * tomoyo_mkdev2keyword - Get the name of path/number/number/number operations.
  122. *
  123. * @operation: Type of operation.
  124. *
  125. * Returns the name of path/number/number/number operation.
  126. */
  127. const char *tomoyo_mkdev2keyword(const u8 operation)
  128. {
  129. return (operation < TOMOYO_MAX_MKDEV_OPERATION)
  130. ? tomoyo_mkdev_keyword[operation] : NULL;
  131. }
  132. /**
  133. * tomoyo_path22keyword - Get the name of double path operation.
  134. *
  135. * @operation: Type of operation.
  136. *
  137. * Returns the name of double path operation.
  138. */
  139. const char *tomoyo_path22keyword(const u8 operation)
  140. {
  141. return (operation < TOMOYO_MAX_PATH2_OPERATION)
  142. ? tomoyo_path2_keyword[operation] : NULL;
  143. }
  144. /**
  145. * tomoyo_path_number2keyword - Get the name of path/number operations.
  146. *
  147. * @operation: Type of operation.
  148. *
  149. * Returns the name of path/number operation.
  150. */
  151. const char *tomoyo_path_number2keyword(const u8 operation)
  152. {
  153. return (operation < TOMOYO_MAX_PATH_NUMBER_OPERATION)
  154. ? tomoyo_path_number_keyword[operation] : NULL;
  155. }
  156. static void tomoyo_add_slash(struct tomoyo_path_info *buf)
  157. {
  158. if (buf->is_dir)
  159. return;
  160. /*
  161. * This is OK because tomoyo_encode() reserves space for appending "/".
  162. */
  163. strcat((char *) buf->name, "/");
  164. tomoyo_fill_path_info(buf);
  165. }
  166. /**
  167. * tomoyo_strendswith - Check whether the token ends with the given token.
  168. *
  169. * @name: The token to check.
  170. * @tail: The token to find.
  171. *
  172. * Returns true if @name ends with @tail, false otherwise.
  173. */
  174. static bool tomoyo_strendswith(const char *name, const char *tail)
  175. {
  176. int len;
  177. if (!name || !tail)
  178. return false;
  179. len = strlen(name) - strlen(tail);
  180. return len >= 0 && !strcmp(name + len, tail);
  181. }
  182. /**
  183. * tomoyo_get_realpath - Get realpath.
  184. *
  185. * @buf: Pointer to "struct tomoyo_path_info".
  186. * @path: Pointer to "struct path".
  187. *
  188. * Returns true on success, false otherwise.
  189. */
  190. static bool tomoyo_get_realpath(struct tomoyo_path_info *buf, struct path *path)
  191. {
  192. buf->name = tomoyo_realpath_from_path(path);
  193. if (buf->name) {
  194. tomoyo_fill_path_info(buf);
  195. return true;
  196. }
  197. return false;
  198. }
  199. /**
  200. * tomoyo_audit_path_log - Audit path request log.
  201. *
  202. * @r: Pointer to "struct tomoyo_request_info".
  203. *
  204. * Returns 0 on success, negative value otherwise.
  205. */
  206. static int tomoyo_audit_path_log(struct tomoyo_request_info *r)
  207. {
  208. const char *operation = tomoyo_path_keyword[r->param.path.operation];
  209. const struct tomoyo_path_info *filename = r->param.path.filename;
  210. if (r->granted)
  211. return 0;
  212. tomoyo_warn_log(r, "%s %s", operation, filename->name);
  213. return tomoyo_supervisor(r, "allow_%s %s\n", operation,
  214. tomoyo_file_pattern(filename));
  215. }
  216. /**
  217. * tomoyo_audit_path2_log - Audit path/path request log.
  218. *
  219. * @r: Pointer to "struct tomoyo_request_info".
  220. *
  221. * Returns 0 on success, negative value otherwise.
  222. */
  223. static int tomoyo_audit_path2_log(struct tomoyo_request_info *r)
  224. {
  225. const char *operation = tomoyo_path2_keyword[r->param.path2.operation];
  226. const struct tomoyo_path_info *filename1 = r->param.path2.filename1;
  227. const struct tomoyo_path_info *filename2 = r->param.path2.filename2;
  228. if (r->granted)
  229. return 0;
  230. tomoyo_warn_log(r, "%s %s %s", operation, filename1->name,
  231. filename2->name);
  232. return tomoyo_supervisor(r, "allow_%s %s %s\n", operation,
  233. tomoyo_file_pattern(filename1),
  234. tomoyo_file_pattern(filename2));
  235. }
  236. /**
  237. * tomoyo_audit_mkdev_log - Audit path/number/number/number request log.
  238. *
  239. * @r: Pointer to "struct tomoyo_request_info".
  240. *
  241. * Returns 0 on success, negative value otherwise.
  242. */
  243. static int tomoyo_audit_mkdev_log(struct tomoyo_request_info *r)
  244. {
  245. const char *operation = tomoyo_mkdev2keyword(r->param.mkdev.
  246. operation);
  247. const struct tomoyo_path_info *filename = r->param.mkdev.filename;
  248. const unsigned int major = r->param.mkdev.major;
  249. const unsigned int minor = r->param.mkdev.minor;
  250. const unsigned int mode = r->param.mkdev.mode;
  251. if (r->granted)
  252. return 0;
  253. tomoyo_warn_log(r, "%s %s 0%o %u %u", operation, filename->name, mode,
  254. major, minor);
  255. return tomoyo_supervisor(r, "allow_%s %s 0%o %u %u\n", operation,
  256. tomoyo_file_pattern(filename), mode, major,
  257. minor);
  258. }
  259. /**
  260. * tomoyo_audit_path_number_log - Audit path/number request log.
  261. *
  262. * @r: Pointer to "struct tomoyo_request_info".
  263. * @error: Error code.
  264. *
  265. * Returns 0 on success, negative value otherwise.
  266. */
  267. static int tomoyo_audit_path_number_log(struct tomoyo_request_info *r)
  268. {
  269. const u8 type = r->param.path_number.operation;
  270. u8 radix;
  271. const struct tomoyo_path_info *filename = r->param.path_number.filename;
  272. const char *operation = tomoyo_path_number_keyword[type];
  273. char buffer[64];
  274. if (r->granted)
  275. return 0;
  276. switch (type) {
  277. case TOMOYO_TYPE_CREATE:
  278. case TOMOYO_TYPE_MKDIR:
  279. case TOMOYO_TYPE_MKFIFO:
  280. case TOMOYO_TYPE_MKSOCK:
  281. case TOMOYO_TYPE_CHMOD:
  282. radix = TOMOYO_VALUE_TYPE_OCTAL;
  283. break;
  284. case TOMOYO_TYPE_IOCTL:
  285. radix = TOMOYO_VALUE_TYPE_HEXADECIMAL;
  286. break;
  287. default:
  288. radix = TOMOYO_VALUE_TYPE_DECIMAL;
  289. break;
  290. }
  291. tomoyo_print_ulong(buffer, sizeof(buffer), r->param.path_number.number,
  292. radix);
  293. tomoyo_warn_log(r, "%s %s %s", operation, filename->name, buffer);
  294. return tomoyo_supervisor(r, "allow_%s %s %s\n", operation,
  295. tomoyo_file_pattern(filename), buffer);
  296. }
  297. static int tomoyo_update_path2_acl(const u8 type, const char *filename1,
  298. const char *filename2,
  299. struct tomoyo_domain_info *const domain,
  300. const bool is_delete);
  301. static int tomoyo_update_path_acl(const u8 type, const char *filename,
  302. struct tomoyo_domain_info *const domain,
  303. const bool is_delete);
  304. /*
  305. * tomoyo_globally_readable_list is used for holding list of pathnames which
  306. * are by default allowed to be open()ed for reading by any process.
  307. *
  308. * An entry is added by
  309. *
  310. * # echo 'allow_read /lib/libc-2.5.so' > \
  311. * /sys/kernel/security/tomoyo/exception_policy
  312. *
  313. * and is deleted by
  314. *
  315. * # echo 'delete allow_read /lib/libc-2.5.so' > \
  316. * /sys/kernel/security/tomoyo/exception_policy
  317. *
  318. * and all entries are retrieved by
  319. *
  320. * # grep ^allow_read /sys/kernel/security/tomoyo/exception_policy
  321. *
  322. * In the example above, any process is allowed to
  323. * open("/lib/libc-2.5.so", O_RDONLY).
  324. * One exception is, if the domain which current process belongs to is marked
  325. * as "ignore_global_allow_read", current process can't do so unless explicitly
  326. * given "allow_read /lib/libc-2.5.so" to the domain which current process
  327. * belongs to.
  328. */
  329. LIST_HEAD(tomoyo_globally_readable_list);
  330. static bool tomoyo_same_globally_readable(const struct tomoyo_acl_head *a,
  331. const struct tomoyo_acl_head *b)
  332. {
  333. return container_of(a, struct tomoyo_globally_readable_file_entry,
  334. head)->filename ==
  335. container_of(b, struct tomoyo_globally_readable_file_entry,
  336. head)->filename;
  337. }
  338. /**
  339. * tomoyo_update_globally_readable_entry - Update "struct tomoyo_globally_readable_file_entry" list.
  340. *
  341. * @filename: Filename unconditionally permitted to open() for reading.
  342. * @is_delete: True if it is a delete request.
  343. *
  344. * Returns 0 on success, negative value otherwise.
  345. *
  346. * Caller holds tomoyo_read_lock().
  347. */
  348. static int tomoyo_update_globally_readable_entry(const char *filename,
  349. const bool is_delete)
  350. {
  351. struct tomoyo_globally_readable_file_entry e = { };
  352. int error;
  353. if (!tomoyo_correct_word(filename))
  354. return -EINVAL;
  355. e.filename = tomoyo_get_name(filename);
  356. if (!e.filename)
  357. return -ENOMEM;
  358. error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
  359. &tomoyo_globally_readable_list,
  360. tomoyo_same_globally_readable);
  361. tomoyo_put_name(e.filename);
  362. return error;
  363. }
  364. /**
  365. * tomoyo_globally_readable_file - Check if the file is unconditionnaly permitted to be open()ed for reading.
  366. *
  367. * @filename: The filename to check.
  368. *
  369. * Returns true if any domain can open @filename for reading, false otherwise.
  370. *
  371. * Caller holds tomoyo_read_lock().
  372. */
  373. static bool tomoyo_globally_readable_file(const struct tomoyo_path_info *
  374. filename)
  375. {
  376. struct tomoyo_globally_readable_file_entry *ptr;
  377. bool found = false;
  378. list_for_each_entry_rcu(ptr, &tomoyo_globally_readable_list,
  379. head.list) {
  380. if (!ptr->head.is_deleted &&
  381. tomoyo_path_matches_pattern(filename, ptr->filename)) {
  382. found = true;
  383. break;
  384. }
  385. }
  386. return found;
  387. }
  388. /**
  389. * tomoyo_write_globally_readable_policy - Write "struct tomoyo_globally_readable_file_entry" list.
  390. *
  391. * @data: String to parse.
  392. * @is_delete: True if it is a delete request.
  393. *
  394. * Returns 0 on success, negative value otherwise.
  395. *
  396. * Caller holds tomoyo_read_lock().
  397. */
  398. int tomoyo_write_globally_readable_policy(char *data, const bool is_delete)
  399. {
  400. return tomoyo_update_globally_readable_entry(data, is_delete);
  401. }
  402. /**
  403. * tomoyo_read_globally_readable_policy - Read "struct tomoyo_globally_readable_file_entry" list.
  404. *
  405. * @head: Pointer to "struct tomoyo_io_buffer".
  406. *
  407. * Returns true on success, false otherwise.
  408. *
  409. * Caller holds tomoyo_read_lock().
  410. */
  411. bool tomoyo_read_globally_readable_policy(struct tomoyo_io_buffer *head)
  412. {
  413. struct list_head *pos;
  414. bool done = true;
  415. list_for_each_cookie(pos, head->read_var2,
  416. &tomoyo_globally_readable_list) {
  417. struct tomoyo_globally_readable_file_entry *ptr;
  418. ptr = list_entry(pos,
  419. struct tomoyo_globally_readable_file_entry,
  420. head.list);
  421. if (ptr->head.is_deleted)
  422. continue;
  423. done = tomoyo_io_printf(head, TOMOYO_KEYWORD_ALLOW_READ "%s\n",
  424. ptr->filename->name);
  425. if (!done)
  426. break;
  427. }
  428. return done;
  429. }
  430. /* tomoyo_pattern_list is used for holding list of pathnames which are used for
  431. * converting pathnames to pathname patterns during learning mode.
  432. *
  433. * An entry is added by
  434. *
  435. * # echo 'file_pattern /proc/\$/mounts' > \
  436. * /sys/kernel/security/tomoyo/exception_policy
  437. *
  438. * and is deleted by
  439. *
  440. * # echo 'delete file_pattern /proc/\$/mounts' > \
  441. * /sys/kernel/security/tomoyo/exception_policy
  442. *
  443. * and all entries are retrieved by
  444. *
  445. * # grep ^file_pattern /sys/kernel/security/tomoyo/exception_policy
  446. *
  447. * In the example above, if a process which belongs to a domain which is in
  448. * learning mode requested open("/proc/1/mounts", O_RDONLY),
  449. * "allow_read /proc/\$/mounts" is automatically added to the domain which that
  450. * process belongs to.
  451. *
  452. * It is not a desirable behavior that we have to use /proc/\$/ instead of
  453. * /proc/self/ when current process needs to access only current process's
  454. * information. As of now, LSM version of TOMOYO is using __d_path() for
  455. * calculating pathname. Non LSM version of TOMOYO is using its own function
  456. * which pretends as if /proc/self/ is not a symlink; so that we can forbid
  457. * current process from accessing other process's information.
  458. */
  459. LIST_HEAD(tomoyo_pattern_list);
  460. static bool tomoyo_same_pattern(const struct tomoyo_acl_head *a,
  461. const struct tomoyo_acl_head *b)
  462. {
  463. return container_of(a, struct tomoyo_pattern_entry, head)->pattern ==
  464. container_of(b, struct tomoyo_pattern_entry, head)->pattern;
  465. }
  466. /**
  467. * tomoyo_update_file_pattern_entry - Update "struct tomoyo_pattern_entry" list.
  468. *
  469. * @pattern: Pathname pattern.
  470. * @is_delete: True if it is a delete request.
  471. *
  472. * Returns 0 on success, negative value otherwise.
  473. *
  474. * Caller holds tomoyo_read_lock().
  475. */
  476. static int tomoyo_update_file_pattern_entry(const char *pattern,
  477. const bool is_delete)
  478. {
  479. struct tomoyo_pattern_entry e = { };
  480. int error;
  481. if (!tomoyo_correct_word(pattern))
  482. return -EINVAL;
  483. e.pattern = tomoyo_get_name(pattern);
  484. if (!e.pattern)
  485. return -ENOMEM;
  486. error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
  487. &tomoyo_pattern_list,
  488. tomoyo_same_pattern);
  489. tomoyo_put_name(e.pattern);
  490. return error;
  491. }
  492. /**
  493. * tomoyo_file_pattern - Get patterned pathname.
  494. *
  495. * @filename: The filename to find patterned pathname.
  496. *
  497. * Returns pointer to pathname pattern if matched, @filename otherwise.
  498. *
  499. * Caller holds tomoyo_read_lock().
  500. */
  501. const char *tomoyo_file_pattern(const struct tomoyo_path_info *filename)
  502. {
  503. struct tomoyo_pattern_entry *ptr;
  504. const struct tomoyo_path_info *pattern = NULL;
  505. list_for_each_entry_rcu(ptr, &tomoyo_pattern_list, head.list) {
  506. if (ptr->head.is_deleted)
  507. continue;
  508. if (!tomoyo_path_matches_pattern(filename, ptr->pattern))
  509. continue;
  510. pattern = ptr->pattern;
  511. if (tomoyo_strendswith(pattern->name, "/\\*")) {
  512. /* Do nothing. Try to find the better match. */
  513. } else {
  514. /* This would be the better match. Use this. */
  515. break;
  516. }
  517. }
  518. if (pattern)
  519. filename = pattern;
  520. return filename->name;
  521. }
  522. /**
  523. * tomoyo_write_pattern_policy - Write "struct tomoyo_pattern_entry" list.
  524. *
  525. * @data: String to parse.
  526. * @is_delete: True if it is a delete request.
  527. *
  528. * Returns 0 on success, negative value otherwise.
  529. *
  530. * Caller holds tomoyo_read_lock().
  531. */
  532. int tomoyo_write_pattern_policy(char *data, const bool is_delete)
  533. {
  534. return tomoyo_update_file_pattern_entry(data, is_delete);
  535. }
  536. /**
  537. * tomoyo_read_file_pattern - Read "struct tomoyo_pattern_entry" list.
  538. *
  539. * @head: Pointer to "struct tomoyo_io_buffer".
  540. *
  541. * Returns true on success, false otherwise.
  542. *
  543. * Caller holds tomoyo_read_lock().
  544. */
  545. bool tomoyo_read_file_pattern(struct tomoyo_io_buffer *head)
  546. {
  547. struct list_head *pos;
  548. bool done = true;
  549. list_for_each_cookie(pos, head->read_var2, &tomoyo_pattern_list) {
  550. struct tomoyo_pattern_entry *ptr;
  551. ptr = list_entry(pos, struct tomoyo_pattern_entry, head.list);
  552. if (ptr->head.is_deleted)
  553. continue;
  554. done = tomoyo_io_printf(head, TOMOYO_KEYWORD_FILE_PATTERN
  555. "%s\n", ptr->pattern->name);
  556. if (!done)
  557. break;
  558. }
  559. return done;
  560. }
  561. /*
  562. * tomoyo_no_rewrite_list is used for holding list of pathnames which are by
  563. * default forbidden to modify already written content of a file.
  564. *
  565. * An entry is added by
  566. *
  567. * # echo 'deny_rewrite /var/log/messages' > \
  568. * /sys/kernel/security/tomoyo/exception_policy
  569. *
  570. * and is deleted by
  571. *
  572. * # echo 'delete deny_rewrite /var/log/messages' > \
  573. * /sys/kernel/security/tomoyo/exception_policy
  574. *
  575. * and all entries are retrieved by
  576. *
  577. * # grep ^deny_rewrite /sys/kernel/security/tomoyo/exception_policy
  578. *
  579. * In the example above, if a process requested to rewrite /var/log/messages ,
  580. * the process can't rewrite unless the domain which that process belongs to
  581. * has "allow_rewrite /var/log/messages" entry.
  582. *
  583. * It is not a desirable behavior that we have to add "\040(deleted)" suffix
  584. * when we want to allow rewriting already unlink()ed file. As of now,
  585. * LSM version of TOMOYO is using __d_path() for calculating pathname.
  586. * Non LSM version of TOMOYO is using its own function which doesn't append
  587. * " (deleted)" suffix if the file is already unlink()ed; so that we don't
  588. * need to worry whether the file is already unlink()ed or not.
  589. */
  590. LIST_HEAD(tomoyo_no_rewrite_list);
  591. static bool tomoyo_same_no_rewrite(const struct tomoyo_acl_head *a,
  592. const struct tomoyo_acl_head *b)
  593. {
  594. return container_of(a, struct tomoyo_no_rewrite_entry, head)->pattern
  595. == container_of(b, struct tomoyo_no_rewrite_entry, head)
  596. ->pattern;
  597. }
  598. /**
  599. * tomoyo_update_no_rewrite_entry - Update "struct tomoyo_no_rewrite_entry" list.
  600. *
  601. * @pattern: Pathname pattern that are not rewritable by default.
  602. * @is_delete: True if it is a delete request.
  603. *
  604. * Returns 0 on success, negative value otherwise.
  605. *
  606. * Caller holds tomoyo_read_lock().
  607. */
  608. static int tomoyo_update_no_rewrite_entry(const char *pattern,
  609. const bool is_delete)
  610. {
  611. struct tomoyo_no_rewrite_entry e = { };
  612. int error;
  613. if (!tomoyo_correct_word(pattern))
  614. return -EINVAL;
  615. e.pattern = tomoyo_get_name(pattern);
  616. if (!e.pattern)
  617. return -ENOMEM;
  618. error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
  619. &tomoyo_no_rewrite_list,
  620. tomoyo_same_no_rewrite);
  621. tomoyo_put_name(e.pattern);
  622. return error;
  623. }
  624. /**
  625. * tomoyo_no_rewrite_file - Check if the given pathname is not permitted to be rewrited.
  626. *
  627. * @filename: Filename to check.
  628. *
  629. * Returns true if @filename is specified by "deny_rewrite" directive,
  630. * false otherwise.
  631. *
  632. * Caller holds tomoyo_read_lock().
  633. */
  634. static bool tomoyo_no_rewrite_file(const struct tomoyo_path_info *filename)
  635. {
  636. struct tomoyo_no_rewrite_entry *ptr;
  637. bool found = false;
  638. list_for_each_entry_rcu(ptr, &tomoyo_no_rewrite_list, head.list) {
  639. if (ptr->head.is_deleted)
  640. continue;
  641. if (!tomoyo_path_matches_pattern(filename, ptr->pattern))
  642. continue;
  643. found = true;
  644. break;
  645. }
  646. return found;
  647. }
  648. /**
  649. * tomoyo_write_no_rewrite_policy - Write "struct tomoyo_no_rewrite_entry" list.
  650. *
  651. * @data: String to parse.
  652. * @is_delete: True if it is a delete request.
  653. *
  654. * Returns 0 on success, negative value otherwise.
  655. *
  656. * Caller holds tomoyo_read_lock().
  657. */
  658. int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete)
  659. {
  660. return tomoyo_update_no_rewrite_entry(data, is_delete);
  661. }
  662. /**
  663. * tomoyo_read_no_rewrite_policy - Read "struct tomoyo_no_rewrite_entry" list.
  664. *
  665. * @head: Pointer to "struct tomoyo_io_buffer".
  666. *
  667. * Returns true on success, false otherwise.
  668. *
  669. * Caller holds tomoyo_read_lock().
  670. */
  671. bool tomoyo_read_no_rewrite_policy(struct tomoyo_io_buffer *head)
  672. {
  673. struct list_head *pos;
  674. bool done = true;
  675. list_for_each_cookie(pos, head->read_var2, &tomoyo_no_rewrite_list) {
  676. struct tomoyo_no_rewrite_entry *ptr;
  677. ptr = list_entry(pos, struct tomoyo_no_rewrite_entry,
  678. head.list);
  679. if (ptr->head.is_deleted)
  680. continue;
  681. done = tomoyo_io_printf(head, TOMOYO_KEYWORD_DENY_REWRITE
  682. "%s\n", ptr->pattern->name);
  683. if (!done)
  684. break;
  685. }
  686. return done;
  687. }
  688. static bool tomoyo_check_path_acl(const struct tomoyo_request_info *r,
  689. const struct tomoyo_acl_info *ptr)
  690. {
  691. const struct tomoyo_path_acl *acl = container_of(ptr, typeof(*acl),
  692. head);
  693. return (acl->perm & (1 << r->param.path.operation)) &&
  694. tomoyo_compare_name_union(r->param.path.filename, &acl->name);
  695. }
  696. static bool tomoyo_check_path_number_acl(const struct tomoyo_request_info *r,
  697. const struct tomoyo_acl_info *ptr)
  698. {
  699. const struct tomoyo_path_number_acl *acl =
  700. container_of(ptr, typeof(*acl), head);
  701. return (acl->perm & (1 << r->param.path_number.operation)) &&
  702. tomoyo_compare_number_union(r->param.path_number.number,
  703. &acl->number) &&
  704. tomoyo_compare_name_union(r->param.path_number.filename,
  705. &acl->name);
  706. }
  707. static bool tomoyo_check_path2_acl(const struct tomoyo_request_info *r,
  708. const struct tomoyo_acl_info *ptr)
  709. {
  710. const struct tomoyo_path2_acl *acl =
  711. container_of(ptr, typeof(*acl), head);
  712. return (acl->perm & (1 << r->param.path2.operation)) &&
  713. tomoyo_compare_name_union(r->param.path2.filename1, &acl->name1)
  714. && tomoyo_compare_name_union(r->param.path2.filename2,
  715. &acl->name2);
  716. }
  717. static bool tomoyo_check_mkdev_acl(const struct tomoyo_request_info *r,
  718. const struct tomoyo_acl_info *ptr)
  719. {
  720. const struct tomoyo_mkdev_acl *acl =
  721. container_of(ptr, typeof(*acl), head);
  722. return (acl->perm & (1 << r->param.mkdev.operation)) &&
  723. tomoyo_compare_number_union(r->param.mkdev.mode,
  724. &acl->mode) &&
  725. tomoyo_compare_number_union(r->param.mkdev.major,
  726. &acl->major) &&
  727. tomoyo_compare_number_union(r->param.mkdev.minor,
  728. &acl->minor) &&
  729. tomoyo_compare_name_union(r->param.mkdev.filename,
  730. &acl->name);
  731. }
  732. static bool tomoyo_same_path_acl(const struct tomoyo_acl_info *a,
  733. const struct tomoyo_acl_info *b)
  734. {
  735. const struct tomoyo_path_acl *p1 = container_of(a, typeof(*p1), head);
  736. const struct tomoyo_path_acl *p2 = container_of(b, typeof(*p2), head);
  737. return tomoyo_same_acl_head(&p1->head, &p2->head) &&
  738. tomoyo_same_name_union(&p1->name, &p2->name);
  739. }
  740. static bool tomoyo_merge_path_acl(struct tomoyo_acl_info *a,
  741. struct tomoyo_acl_info *b,
  742. const bool is_delete)
  743. {
  744. u16 * const a_perm = &container_of(a, struct tomoyo_path_acl, head)
  745. ->perm;
  746. u16 perm = *a_perm;
  747. const u16 b_perm = container_of(b, struct tomoyo_path_acl, head)->perm;
  748. if (is_delete) {
  749. perm &= ~b_perm;
  750. if ((perm & TOMOYO_RW_MASK) != TOMOYO_RW_MASK)
  751. perm &= ~(1 << TOMOYO_TYPE_READ_WRITE);
  752. else if (!(perm & (1 << TOMOYO_TYPE_READ_WRITE)))
  753. perm &= ~TOMOYO_RW_MASK;
  754. } else {
  755. perm |= b_perm;
  756. if ((perm & TOMOYO_RW_MASK) == TOMOYO_RW_MASK)
  757. perm |= (1 << TOMOYO_TYPE_READ_WRITE);
  758. else if (perm & (1 << TOMOYO_TYPE_READ_WRITE))
  759. perm |= TOMOYO_RW_MASK;
  760. }
  761. *a_perm = perm;
  762. return !perm;
  763. }
  764. /**
  765. * tomoyo_update_path_acl - Update "struct tomoyo_path_acl" list.
  766. *
  767. * @type: Type of operation.
  768. * @filename: Filename.
  769. * @domain: Pointer to "struct tomoyo_domain_info".
  770. * @is_delete: True if it is a delete request.
  771. *
  772. * Returns 0 on success, negative value otherwise.
  773. *
  774. * Caller holds tomoyo_read_lock().
  775. */
  776. static int tomoyo_update_path_acl(const u8 type, const char *filename,
  777. struct tomoyo_domain_info * const domain,
  778. const bool is_delete)
  779. {
  780. struct tomoyo_path_acl e = {
  781. .head.type = TOMOYO_TYPE_PATH_ACL,
  782. .perm = 1 << type
  783. };
  784. int error;
  785. if (e.perm == (1 << TOMOYO_TYPE_READ_WRITE))
  786. e.perm |= TOMOYO_RW_MASK;
  787. if (!tomoyo_parse_name_union(filename, &e.name))
  788. return -EINVAL;
  789. error = tomoyo_update_domain(&e.head, sizeof(e), is_delete, domain,
  790. tomoyo_same_path_acl,
  791. tomoyo_merge_path_acl);
  792. tomoyo_put_name_union(&e.name);
  793. return error;
  794. }
  795. static bool tomoyo_same_mkdev_acl(const struct tomoyo_acl_info *a,
  796. const struct tomoyo_acl_info *b)
  797. {
  798. const struct tomoyo_mkdev_acl *p1 = container_of(a, typeof(*p1),
  799. head);
  800. const struct tomoyo_mkdev_acl *p2 = container_of(b, typeof(*p2),
  801. head);
  802. return tomoyo_same_acl_head(&p1->head, &p2->head)
  803. && tomoyo_same_name_union(&p1->name, &p2->name)
  804. && tomoyo_same_number_union(&p1->mode, &p2->mode)
  805. && tomoyo_same_number_union(&p1->major, &p2->major)
  806. && tomoyo_same_number_union(&p1->minor, &p2->minor);
  807. }
  808. static bool tomoyo_merge_mkdev_acl(struct tomoyo_acl_info *a,
  809. struct tomoyo_acl_info *b,
  810. const bool is_delete)
  811. {
  812. u8 *const a_perm = &container_of(a, struct tomoyo_mkdev_acl,
  813. head)->perm;
  814. u8 perm = *a_perm;
  815. const u8 b_perm = container_of(b, struct tomoyo_mkdev_acl, head)
  816. ->perm;
  817. if (is_delete)
  818. perm &= ~b_perm;
  819. else
  820. perm |= b_perm;
  821. *a_perm = perm;
  822. return !perm;
  823. }
  824. /**
  825. * tomoyo_update_mkdev_acl - Update "struct tomoyo_mkdev_acl" list.
  826. *
  827. * @type: Type of operation.
  828. * @filename: Filename.
  829. * @mode: Create mode.
  830. * @major: Device major number.
  831. * @minor: Device minor number.
  832. * @domain: Pointer to "struct tomoyo_domain_info".
  833. * @is_delete: True if it is a delete request.
  834. *
  835. * Returns 0 on success, negative value otherwise.
  836. *
  837. * Caller holds tomoyo_read_lock().
  838. */
  839. static int tomoyo_update_mkdev_acl(const u8 type, const char *filename,
  840. char *mode, char *major, char *minor,
  841. struct tomoyo_domain_info * const
  842. domain, const bool is_delete)
  843. {
  844. struct tomoyo_mkdev_acl e = {
  845. .head.type = TOMOYO_TYPE_MKDEV_ACL,
  846. .perm = 1 << type
  847. };
  848. int error = is_delete ? -ENOENT : -ENOMEM;
  849. if (!tomoyo_parse_name_union(filename, &e.name) ||
  850. !tomoyo_parse_number_union(mode, &e.mode) ||
  851. !tomoyo_parse_number_union(major, &e.major) ||
  852. !tomoyo_parse_number_union(minor, &e.minor))
  853. goto out;
  854. error = tomoyo_update_domain(&e.head, sizeof(e), is_delete, domain,
  855. tomoyo_same_mkdev_acl,
  856. tomoyo_merge_mkdev_acl);
  857. out:
  858. tomoyo_put_name_union(&e.name);
  859. tomoyo_put_number_union(&e.mode);
  860. tomoyo_put_number_union(&e.major);
  861. tomoyo_put_number_union(&e.minor);
  862. return error;
  863. }
  864. static bool tomoyo_same_path2_acl(const struct tomoyo_acl_info *a,
  865. const struct tomoyo_acl_info *b)
  866. {
  867. const struct tomoyo_path2_acl *p1 = container_of(a, typeof(*p1), head);
  868. const struct tomoyo_path2_acl *p2 = container_of(b, typeof(*p2), head);
  869. return tomoyo_same_acl_head(&p1->head, &p2->head)
  870. && tomoyo_same_name_union(&p1->name1, &p2->name1)
  871. && tomoyo_same_name_union(&p1->name2, &p2->name2);
  872. }
  873. static bool tomoyo_merge_path2_acl(struct tomoyo_acl_info *a,
  874. struct tomoyo_acl_info *b,
  875. const bool is_delete)
  876. {
  877. u8 * const a_perm = &container_of(a, struct tomoyo_path2_acl, head)
  878. ->perm;
  879. u8 perm = *a_perm;
  880. const u8 b_perm = container_of(b, struct tomoyo_path2_acl, head)->perm;
  881. if (is_delete)
  882. perm &= ~b_perm;
  883. else
  884. perm |= b_perm;
  885. *a_perm = perm;
  886. return !perm;
  887. }
  888. /**
  889. * tomoyo_update_path2_acl - Update "struct tomoyo_path2_acl" list.
  890. *
  891. * @type: Type of operation.
  892. * @filename1: First filename.
  893. * @filename2: Second filename.
  894. * @domain: Pointer to "struct tomoyo_domain_info".
  895. * @is_delete: True if it is a delete request.
  896. *
  897. * Returns 0 on success, negative value otherwise.
  898. *
  899. * Caller holds tomoyo_read_lock().
  900. */
  901. static int tomoyo_update_path2_acl(const u8 type, const char *filename1,
  902. const char *filename2,
  903. struct tomoyo_domain_info * const domain,
  904. const bool is_delete)
  905. {
  906. struct tomoyo_path2_acl e = {
  907. .head.type = TOMOYO_TYPE_PATH2_ACL,
  908. .perm = 1 << type
  909. };
  910. int error = is_delete ? -ENOENT : -ENOMEM;
  911. if (!tomoyo_parse_name_union(filename1, &e.name1) ||
  912. !tomoyo_parse_name_union(filename2, &e.name2))
  913. goto out;
  914. error = tomoyo_update_domain(&e.head, sizeof(e), is_delete, domain,
  915. tomoyo_same_path2_acl,
  916. tomoyo_merge_path2_acl);
  917. out:
  918. tomoyo_put_name_union(&e.name1);
  919. tomoyo_put_name_union(&e.name2);
  920. return error;
  921. }
  922. /**
  923. * tomoyo_path_permission - Check permission for single path operation.
  924. *
  925. * @r: Pointer to "struct tomoyo_request_info".
  926. * @operation: Type of operation.
  927. * @filename: Filename to check.
  928. *
  929. * Returns 0 on success, negative value otherwise.
  930. *
  931. * Caller holds tomoyo_read_lock().
  932. */
  933. int tomoyo_path_permission(struct tomoyo_request_info *r, u8 operation,
  934. const struct tomoyo_path_info *filename)
  935. {
  936. int error;
  937. next:
  938. r->type = tomoyo_p2mac[operation];
  939. r->mode = tomoyo_get_mode(r->profile, r->type);
  940. if (r->mode == TOMOYO_CONFIG_DISABLED)
  941. return 0;
  942. r->param_type = TOMOYO_TYPE_PATH_ACL;
  943. r->param.path.filename = filename;
  944. r->param.path.operation = operation;
  945. do {
  946. tomoyo_check_acl(r, tomoyo_check_path_acl);
  947. if (!r->granted && operation == TOMOYO_TYPE_READ &&
  948. !r->domain->ignore_global_allow_read &&
  949. tomoyo_globally_readable_file(filename))
  950. r->granted = true;
  951. error = tomoyo_audit_path_log(r);
  952. /*
  953. * Do not retry for execute request, for alias may have
  954. * changed.
  955. */
  956. } while (error == TOMOYO_RETRY_REQUEST &&
  957. operation != TOMOYO_TYPE_EXECUTE);
  958. /*
  959. * Since "allow_truncate" doesn't imply "allow_rewrite" permission,
  960. * we need to check "allow_rewrite" permission if the filename is
  961. * specified by "deny_rewrite" keyword.
  962. */
  963. if (!error && operation == TOMOYO_TYPE_TRUNCATE &&
  964. tomoyo_no_rewrite_file(filename)) {
  965. operation = TOMOYO_TYPE_REWRITE;
  966. goto next;
  967. }
  968. return error;
  969. }
  970. static bool tomoyo_same_path_number_acl(const struct tomoyo_acl_info *a,
  971. const struct tomoyo_acl_info *b)
  972. {
  973. const struct tomoyo_path_number_acl *p1 = container_of(a, typeof(*p1),
  974. head);
  975. const struct tomoyo_path_number_acl *p2 = container_of(b, typeof(*p2),
  976. head);
  977. return tomoyo_same_acl_head(&p1->head, &p2->head)
  978. && tomoyo_same_name_union(&p1->name, &p2->name)
  979. && tomoyo_same_number_union(&p1->number, &p2->number);
  980. }
  981. static bool tomoyo_merge_path_number_acl(struct tomoyo_acl_info *a,
  982. struct tomoyo_acl_info *b,
  983. const bool is_delete)
  984. {
  985. u8 * const a_perm = &container_of(a, struct tomoyo_path_number_acl,
  986. head)->perm;
  987. u8 perm = *a_perm;
  988. const u8 b_perm = container_of(b, struct tomoyo_path_number_acl, head)
  989. ->perm;
  990. if (is_delete)
  991. perm &= ~b_perm;
  992. else
  993. perm |= b_perm;
  994. *a_perm = perm;
  995. return !perm;
  996. }
  997. /**
  998. * tomoyo_update_path_number_acl - Update ioctl/chmod/chown/chgrp ACL.
  999. *
  1000. * @type: Type of operation.
  1001. * @filename: Filename.
  1002. * @number: Number.
  1003. * @domain: Pointer to "struct tomoyo_domain_info".
  1004. * @is_delete: True if it is a delete request.
  1005. *
  1006. * Returns 0 on success, negative value otherwise.
  1007. */
  1008. static int tomoyo_update_path_number_acl(const u8 type, const char *filename,
  1009. char *number,
  1010. struct tomoyo_domain_info * const
  1011. domain,
  1012. const bool is_delete)
  1013. {
  1014. struct tomoyo_path_number_acl e = {
  1015. .head.type = TOMOYO_TYPE_PATH_NUMBER_ACL,
  1016. .perm = 1 << type
  1017. };
  1018. int error = is_delete ? -ENOENT : -ENOMEM;
  1019. if (!tomoyo_parse_name_union(filename, &e.name))
  1020. return -EINVAL;
  1021. if (!tomoyo_parse_number_union(number, &e.number))
  1022. goto out;
  1023. error = tomoyo_update_domain(&e.head, sizeof(e), is_delete, domain,
  1024. tomoyo_same_path_number_acl,
  1025. tomoyo_merge_path_number_acl);
  1026. out:
  1027. tomoyo_put_name_union(&e.name);
  1028. tomoyo_put_number_union(&e.number);
  1029. return error;
  1030. }
  1031. /**
  1032. * tomoyo_path_number_perm2 - Check permission for "create", "mkdir", "mkfifo", "mksock", "ioctl", "chmod", "chown", "chgrp".
  1033. *
  1034. * @r: Pointer to "strct tomoyo_request_info".
  1035. * @filename: Filename to check.
  1036. * @number: Number.
  1037. *
  1038. * Returns 0 on success, negative value otherwise.
  1039. *
  1040. * Caller holds tomoyo_read_lock().
  1041. */
  1042. static int tomoyo_path_number_perm2(struct tomoyo_request_info *r,
  1043. const u8 type,
  1044. const struct tomoyo_path_info *filename,
  1045. const unsigned long number)
  1046. {
  1047. int error;
  1048. if (!filename)
  1049. return 0;
  1050. r->param_type = TOMOYO_TYPE_PATH_NUMBER_ACL;
  1051. r->param.path_number.operation = type;
  1052. r->param.path_number.filename = filename;
  1053. r->param.path_number.number = number;
  1054. do {
  1055. tomoyo_check_acl(r, tomoyo_check_path_number_acl);
  1056. error = tomoyo_audit_path_number_log(r);
  1057. } while (error == TOMOYO_RETRY_REQUEST);
  1058. return error;
  1059. }
  1060. /**
  1061. * tomoyo_path_number_perm - Check permission for "create", "mkdir", "mkfifo", "mksock", "ioctl", "chmod", "chown", "chgrp".
  1062. *
  1063. * @type: Type of operation.
  1064. * @path: Pointer to "struct path".
  1065. * @number: Number.
  1066. *
  1067. * Returns 0 on success, negative value otherwise.
  1068. */
  1069. int tomoyo_path_number_perm(const u8 type, struct path *path,
  1070. unsigned long number)
  1071. {
  1072. struct tomoyo_request_info r;
  1073. int error = -ENOMEM;
  1074. struct tomoyo_path_info buf;
  1075. int idx;
  1076. if (tomoyo_init_request_info(&r, NULL, tomoyo_pn2mac[type])
  1077. == TOMOYO_CONFIG_DISABLED || !path->mnt || !path->dentry)
  1078. return 0;
  1079. idx = tomoyo_read_lock();
  1080. if (!tomoyo_get_realpath(&buf, path))
  1081. goto out;
  1082. if (type == TOMOYO_TYPE_MKDIR)
  1083. tomoyo_add_slash(&buf);
  1084. error = tomoyo_path_number_perm2(&r, type, &buf, number);
  1085. out:
  1086. kfree(buf.name);
  1087. tomoyo_read_unlock(idx);
  1088. if (r.mode != TOMOYO_CONFIG_ENFORCING)
  1089. error = 0;
  1090. return error;
  1091. }
  1092. /**
  1093. * tomoyo_check_open_permission - Check permission for "read" and "write".
  1094. *
  1095. * @domain: Pointer to "struct tomoyo_domain_info".
  1096. * @path: Pointer to "struct path".
  1097. * @flag: Flags for open().
  1098. *
  1099. * Returns 0 on success, negative value otherwise.
  1100. */
  1101. int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
  1102. struct path *path, const int flag)
  1103. {
  1104. const u8 acc_mode = ACC_MODE(flag);
  1105. int error = -ENOMEM;
  1106. struct tomoyo_path_info buf;
  1107. struct tomoyo_request_info r;
  1108. int idx;
  1109. if (!path->mnt ||
  1110. (path->dentry->d_inode && S_ISDIR(path->dentry->d_inode->i_mode)))
  1111. return 0;
  1112. buf.name = NULL;
  1113. r.mode = TOMOYO_CONFIG_DISABLED;
  1114. idx = tomoyo_read_lock();
  1115. if (!tomoyo_get_realpath(&buf, path))
  1116. goto out;
  1117. error = 0;
  1118. /*
  1119. * If the filename is specified by "deny_rewrite" keyword,
  1120. * we need to check "allow_rewrite" permission when the filename is not
  1121. * opened for append mode or the filename is truncated at open time.
  1122. */
  1123. if ((acc_mode & MAY_WRITE) && !(flag & O_APPEND)
  1124. && tomoyo_init_request_info(&r, domain, TOMOYO_MAC_FILE_REWRITE)
  1125. != TOMOYO_CONFIG_DISABLED) {
  1126. if (!tomoyo_get_realpath(&buf, path)) {
  1127. error = -ENOMEM;
  1128. goto out;
  1129. }
  1130. if (tomoyo_no_rewrite_file(&buf))
  1131. error = tomoyo_path_permission(&r, TOMOYO_TYPE_REWRITE,
  1132. &buf);
  1133. }
  1134. if (!error && acc_mode &&
  1135. tomoyo_init_request_info(&r, domain, TOMOYO_MAC_FILE_OPEN)
  1136. != TOMOYO_CONFIG_DISABLED) {
  1137. u8 operation;
  1138. if (!buf.name && !tomoyo_get_realpath(&buf, path)) {
  1139. error = -ENOMEM;
  1140. goto out;
  1141. }
  1142. if (acc_mode == (MAY_READ | MAY_WRITE))
  1143. operation = TOMOYO_TYPE_READ_WRITE;
  1144. else if (acc_mode == MAY_READ)
  1145. operation = TOMOYO_TYPE_READ;
  1146. else
  1147. operation = TOMOYO_TYPE_WRITE;
  1148. error = tomoyo_path_permission(&r, operation, &buf);
  1149. }
  1150. out:
  1151. kfree(buf.name);
  1152. tomoyo_read_unlock(idx);
  1153. if (r.mode != TOMOYO_CONFIG_ENFORCING)
  1154. error = 0;
  1155. return error;
  1156. }
  1157. /**
  1158. * tomoyo_path_perm - Check permission for "unlink", "rmdir", "truncate", "symlink", "rewrite", "chroot" and "unmount".
  1159. *
  1160. * @operation: Type of operation.
  1161. * @path: Pointer to "struct path".
  1162. *
  1163. * Returns 0 on success, negative value otherwise.
  1164. */
  1165. int tomoyo_path_perm(const u8 operation, struct path *path)
  1166. {
  1167. int error = -ENOMEM;
  1168. struct tomoyo_path_info buf;
  1169. struct tomoyo_request_info r;
  1170. int idx;
  1171. if (!path->mnt)
  1172. return 0;
  1173. if (tomoyo_init_request_info(&r, NULL, tomoyo_p2mac[operation])
  1174. == TOMOYO_CONFIG_DISABLED)
  1175. return 0;
  1176. buf.name = NULL;
  1177. idx = tomoyo_read_lock();
  1178. if (!tomoyo_get_realpath(&buf, path))
  1179. goto out;
  1180. switch (operation) {
  1181. case TOMOYO_TYPE_REWRITE:
  1182. if (!tomoyo_no_rewrite_file(&buf)) {
  1183. error = 0;
  1184. goto out;
  1185. }
  1186. break;
  1187. case TOMOYO_TYPE_RMDIR:
  1188. case TOMOYO_TYPE_CHROOT:
  1189. case TOMOYO_TYPE_UMOUNT:
  1190. tomoyo_add_slash(&buf);
  1191. break;
  1192. }
  1193. error = tomoyo_path_permission(&r, operation, &buf);
  1194. out:
  1195. kfree(buf.name);
  1196. tomoyo_read_unlock(idx);
  1197. if (r.mode != TOMOYO_CONFIG_ENFORCING)
  1198. error = 0;
  1199. return error;
  1200. }
  1201. /**
  1202. * tomoyo_mkdev_perm - Check permission for "mkblock" and "mkchar".
  1203. *
  1204. * @operation: Type of operation. (TOMOYO_TYPE_MKCHAR or TOMOYO_TYPE_MKBLOCK)
  1205. * @path: Pointer to "struct path".
  1206. * @mode: Create mode.
  1207. * @dev: Device number.
  1208. *
  1209. * Returns 0 on success, negative value otherwise.
  1210. */
  1211. int tomoyo_mkdev_perm(const u8 operation, struct path *path,
  1212. const unsigned int mode, unsigned int dev)
  1213. {
  1214. struct tomoyo_request_info r;
  1215. int error = -ENOMEM;
  1216. struct tomoyo_path_info buf;
  1217. int idx;
  1218. if (!path->mnt ||
  1219. tomoyo_init_request_info(&r, NULL, tomoyo_pnnn2mac[operation])
  1220. == TOMOYO_CONFIG_DISABLED)
  1221. return 0;
  1222. idx = tomoyo_read_lock();
  1223. error = -ENOMEM;
  1224. if (tomoyo_get_realpath(&buf, path)) {
  1225. dev = new_decode_dev(dev);
  1226. r.param_type = TOMOYO_TYPE_MKDEV_ACL;
  1227. r.param.mkdev.filename = &buf;
  1228. r.param.mkdev.operation = operation;
  1229. r.param.mkdev.mode = mode;
  1230. r.param.mkdev.major = MAJOR(dev);
  1231. r.param.mkdev.minor = MINOR(dev);
  1232. tomoyo_check_acl(&r, tomoyo_check_mkdev_acl);
  1233. error = tomoyo_audit_mkdev_log(&r);
  1234. kfree(buf.name);
  1235. }
  1236. tomoyo_read_unlock(idx);
  1237. if (r.mode != TOMOYO_CONFIG_ENFORCING)
  1238. error = 0;
  1239. return error;
  1240. }
  1241. /**
  1242. * tomoyo_path2_perm - Check permission for "rename", "link" and "pivot_root".
  1243. *
  1244. * @operation: Type of operation.
  1245. * @path1: Pointer to "struct path".
  1246. * @path2: Pointer to "struct path".
  1247. *
  1248. * Returns 0 on success, negative value otherwise.
  1249. */
  1250. int tomoyo_path2_perm(const u8 operation, struct path *path1,
  1251. struct path *path2)
  1252. {
  1253. int error = -ENOMEM;
  1254. struct tomoyo_path_info buf1;
  1255. struct tomoyo_path_info buf2;
  1256. struct tomoyo_request_info r;
  1257. int idx;
  1258. if (!path1->mnt || !path2->mnt ||
  1259. tomoyo_init_request_info(&r, NULL, tomoyo_pp2mac[operation])
  1260. == TOMOYO_CONFIG_DISABLED)
  1261. return 0;
  1262. buf1.name = NULL;
  1263. buf2.name = NULL;
  1264. idx = tomoyo_read_lock();
  1265. if (!tomoyo_get_realpath(&buf1, path1) ||
  1266. !tomoyo_get_realpath(&buf2, path2))
  1267. goto out;
  1268. switch (operation) {
  1269. struct dentry *dentry;
  1270. case TOMOYO_TYPE_RENAME:
  1271. case TOMOYO_TYPE_LINK:
  1272. dentry = path1->dentry;
  1273. if (!dentry->d_inode || !S_ISDIR(dentry->d_inode->i_mode))
  1274. break;
  1275. /* fall through */
  1276. case TOMOYO_TYPE_PIVOT_ROOT:
  1277. tomoyo_add_slash(&buf1);
  1278. tomoyo_add_slash(&buf2);
  1279. break;
  1280. }
  1281. r.param_type = TOMOYO_TYPE_PATH2_ACL;
  1282. r.param.path2.operation = operation;
  1283. r.param.path2.filename1 = &buf1;
  1284. r.param.path2.filename2 = &buf2;
  1285. do {
  1286. tomoyo_check_acl(&r, tomoyo_check_path2_acl);
  1287. error = tomoyo_audit_path2_log(&r);
  1288. } while (error == TOMOYO_RETRY_REQUEST);
  1289. out:
  1290. kfree(buf1.name);
  1291. kfree(buf2.name);
  1292. tomoyo_read_unlock(idx);
  1293. if (r.mode != TOMOYO_CONFIG_ENFORCING)
  1294. error = 0;
  1295. return error;
  1296. }
  1297. /**
  1298. * tomoyo_write_file_policy - Update file related list.
  1299. *
  1300. * @data: String to parse.
  1301. * @domain: Pointer to "struct tomoyo_domain_info".
  1302. * @is_delete: True if it is a delete request.
  1303. *
  1304. * Returns 0 on success, negative value otherwise.
  1305. *
  1306. * Caller holds tomoyo_read_lock().
  1307. */
  1308. int tomoyo_write_file_policy(char *data, struct tomoyo_domain_info *domain,
  1309. const bool is_delete)
  1310. {
  1311. char *w[5];
  1312. u8 type;
  1313. if (!tomoyo_tokenize(data, w, sizeof(w)) || !w[1][0])
  1314. return -EINVAL;
  1315. if (strncmp(w[0], "allow_", 6))
  1316. goto out;
  1317. w[0] += 6;
  1318. for (type = 0; type < TOMOYO_MAX_PATH_OPERATION; type++) {
  1319. if (strcmp(w[0], tomoyo_path_keyword[type]))
  1320. continue;
  1321. return tomoyo_update_path_acl(type, w[1], domain, is_delete);
  1322. }
  1323. if (!w[2][0])
  1324. goto out;
  1325. for (type = 0; type < TOMOYO_MAX_PATH2_OPERATION; type++) {
  1326. if (strcmp(w[0], tomoyo_path2_keyword[type]))
  1327. continue;
  1328. return tomoyo_update_path2_acl(type, w[1], w[2], domain,
  1329. is_delete);
  1330. }
  1331. for (type = 0; type < TOMOYO_MAX_PATH_NUMBER_OPERATION; type++) {
  1332. if (strcmp(w[0], tomoyo_path_number_keyword[type]))
  1333. continue;
  1334. return tomoyo_update_path_number_acl(type, w[1], w[2], domain,
  1335. is_delete);
  1336. }
  1337. if (!w[3][0] || !w[4][0])
  1338. goto out;
  1339. for (type = 0; type < TOMOYO_MAX_MKDEV_OPERATION; type++) {
  1340. if (strcmp(w[0], tomoyo_mkdev_keyword[type]))
  1341. continue;
  1342. return tomoyo_update_mkdev_acl(type, w[1], w[2], w[3],
  1343. w[4], domain, is_delete);
  1344. }
  1345. out:
  1346. return -EINVAL;
  1347. }