apparmorfs.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  1. /*
  2. * AppArmor security module
  3. *
  4. * This file contains AppArmor /sys/kernel/security/apparmor interface functions
  5. *
  6. * Copyright (C) 1998-2008 Novell/SUSE
  7. * Copyright 2009-2010 Canonical Ltd.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation, version 2 of the
  12. * License.
  13. */
  14. #include <linux/ctype.h>
  15. #include <linux/security.h>
  16. #include <linux/vmalloc.h>
  17. #include <linux/module.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/mount.h>
  21. #include <linux/namei.h>
  22. #include <linux/capability.h>
  23. #include <linux/rcupdate.h>
  24. #include <uapi/linux/major.h>
  25. #include <linux/fs.h>
  26. #include "include/apparmor.h"
  27. #include "include/apparmorfs.h"
  28. #include "include/audit.h"
  29. #include "include/context.h"
  30. #include "include/crypto.h"
  31. #include "include/policy.h"
  32. #include "include/policy_ns.h"
  33. #include "include/resource.h"
  34. #include "include/policy_unpack.h"
  35. /**
  36. * aa_mangle_name - mangle a profile name to std profile layout form
  37. * @name: profile name to mangle (NOT NULL)
  38. * @target: buffer to store mangled name, same length as @name (MAYBE NULL)
  39. *
  40. * Returns: length of mangled name
  41. */
  42. static int mangle_name(const char *name, char *target)
  43. {
  44. char *t = target;
  45. while (*name == '/' || *name == '.')
  46. name++;
  47. if (target) {
  48. for (; *name; name++) {
  49. if (*name == '/')
  50. *(t)++ = '.';
  51. else if (isspace(*name))
  52. *(t)++ = '_';
  53. else if (isalnum(*name) || strchr("._-", *name))
  54. *(t)++ = *name;
  55. }
  56. *t = 0;
  57. } else {
  58. int len = 0;
  59. for (; *name; name++) {
  60. if (isalnum(*name) || isspace(*name) ||
  61. strchr("/._-", *name))
  62. len++;
  63. }
  64. return len;
  65. }
  66. return t - target;
  67. }
  68. /**
  69. * aa_simple_write_to_buffer - common routine for getting policy from user
  70. * @op: operation doing the user buffer copy
  71. * @userbuf: user buffer to copy data from (NOT NULL)
  72. * @alloc_size: size of user buffer (REQUIRES: @alloc_size >= @copy_size)
  73. * @copy_size: size of data to copy from user buffer
  74. * @pos: position write is at in the file (NOT NULL)
  75. *
  76. * Returns: kernel buffer containing copy of user buffer data or an
  77. * ERR_PTR on failure.
  78. */
  79. static struct aa_loaddata *aa_simple_write_to_buffer(int op,
  80. const char __user *userbuf,
  81. size_t alloc_size,
  82. size_t copy_size,
  83. loff_t *pos)
  84. {
  85. struct aa_loaddata *data;
  86. BUG_ON(copy_size > alloc_size);
  87. if (*pos != 0)
  88. /* only writes from pos 0, that is complete writes */
  89. return ERR_PTR(-ESPIPE);
  90. /* freed by caller to simple_write_to_buffer */
  91. data = kvmalloc(sizeof(*data) + alloc_size);
  92. if (data == NULL)
  93. return ERR_PTR(-ENOMEM);
  94. kref_init(&data->count);
  95. data->size = copy_size;
  96. data->hash = NULL;
  97. data->abi = 0;
  98. if (copy_from_user(data->data, userbuf, copy_size)) {
  99. kvfree(data);
  100. return ERR_PTR(-EFAULT);
  101. }
  102. return data;
  103. }
  104. static ssize_t policy_update(int binop, const char __user *buf, size_t size,
  105. loff_t *pos)
  106. {
  107. ssize_t error;
  108. struct aa_loaddata *data;
  109. struct aa_profile *profile = aa_current_profile();
  110. int op = binop == PROF_ADD ? OP_PROF_LOAD : OP_PROF_REPL;
  111. /* high level check about policy management - fine grained in
  112. * below after unpack
  113. */
  114. error = aa_may_manage_policy(profile, profile->ns, op);
  115. if (error)
  116. return error;
  117. data = aa_simple_write_to_buffer(op, buf, size, size, pos);
  118. error = PTR_ERR(data);
  119. if (!IS_ERR(data)) {
  120. error = aa_replace_profiles(profile->ns, binop, data);
  121. aa_put_loaddata(data);
  122. }
  123. return error;
  124. }
  125. static ssize_t profile_load(struct file *f, const char __user *buf, size_t size,
  126. loff_t *pos)
  127. {
  128. int error = policy_update(PROF_ADD, buf, size, pos);
  129. return error;
  130. }
  131. static const struct file_operations aa_fs_profile_load = {
  132. .write = profile_load,
  133. .llseek = default_llseek,
  134. };
  135. /* .replace file hook fn to load and/or replace policy */
  136. static ssize_t profile_replace(struct file *f, const char __user *buf,
  137. size_t size, loff_t *pos)
  138. {
  139. int error = policy_update(PROF_REPLACE, buf, size, pos);
  140. return error;
  141. }
  142. static const struct file_operations aa_fs_profile_replace = {
  143. .write = profile_replace,
  144. .llseek = default_llseek,
  145. };
  146. static ssize_t profile_remove(struct file *f, const char __user *buf,
  147. size_t size, loff_t *pos)
  148. {
  149. struct aa_loaddata *data;
  150. struct aa_profile *profile;
  151. ssize_t error;
  152. profile = aa_current_profile();
  153. /* high level check about policy management - fine grained in
  154. * below after unpack
  155. */
  156. error = aa_may_manage_policy(profile, profile->ns, OP_PROF_RM);
  157. if (error)
  158. goto out;
  159. /*
  160. * aa_remove_profile needs a null terminated string so 1 extra
  161. * byte is allocated and the copied data is null terminated.
  162. */
  163. data = aa_simple_write_to_buffer(OP_PROF_RM, buf, size + 1, size,
  164. pos);
  165. error = PTR_ERR(data);
  166. if (!IS_ERR(data)) {
  167. data->data[size] = 0;
  168. error = aa_remove_profiles(profile->ns, data->data, size);
  169. aa_put_loaddata(data);
  170. }
  171. out:
  172. return error;
  173. }
  174. static const struct file_operations aa_fs_profile_remove = {
  175. .write = profile_remove,
  176. .llseek = default_llseek,
  177. };
  178. static int aa_fs_seq_show(struct seq_file *seq, void *v)
  179. {
  180. struct aa_fs_entry *fs_file = seq->private;
  181. if (!fs_file)
  182. return 0;
  183. switch (fs_file->v_type) {
  184. case AA_FS_TYPE_BOOLEAN:
  185. seq_printf(seq, "%s\n", fs_file->v.boolean ? "yes" : "no");
  186. break;
  187. case AA_FS_TYPE_STRING:
  188. seq_printf(seq, "%s\n", fs_file->v.string);
  189. break;
  190. case AA_FS_TYPE_U64:
  191. seq_printf(seq, "%#08lx\n", fs_file->v.u64);
  192. break;
  193. default:
  194. /* Ignore unpritable entry types. */
  195. break;
  196. }
  197. return 0;
  198. }
  199. static int aa_fs_seq_open(struct inode *inode, struct file *file)
  200. {
  201. return single_open(file, aa_fs_seq_show, inode->i_private);
  202. }
  203. const struct file_operations aa_fs_seq_file_ops = {
  204. .owner = THIS_MODULE,
  205. .open = aa_fs_seq_open,
  206. .read = seq_read,
  207. .llseek = seq_lseek,
  208. .release = single_release,
  209. };
  210. static int aa_fs_seq_profile_open(struct inode *inode, struct file *file,
  211. int (*show)(struct seq_file *, void *))
  212. {
  213. struct aa_proxy *proxy = aa_get_proxy(inode->i_private);
  214. int error = single_open(file, show, proxy);
  215. if (error) {
  216. file->private_data = NULL;
  217. aa_put_proxy(proxy);
  218. }
  219. return error;
  220. }
  221. static int aa_fs_seq_profile_release(struct inode *inode, struct file *file)
  222. {
  223. struct seq_file *seq = (struct seq_file *) file->private_data;
  224. if (seq)
  225. aa_put_proxy(seq->private);
  226. return single_release(inode, file);
  227. }
  228. static int aa_fs_seq_profname_show(struct seq_file *seq, void *v)
  229. {
  230. struct aa_proxy *proxy = seq->private;
  231. struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
  232. seq_printf(seq, "%s\n", profile->base.name);
  233. aa_put_profile(profile);
  234. return 0;
  235. }
  236. static int aa_fs_seq_profname_open(struct inode *inode, struct file *file)
  237. {
  238. return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profname_show);
  239. }
  240. static const struct file_operations aa_fs_profname_fops = {
  241. .owner = THIS_MODULE,
  242. .open = aa_fs_seq_profname_open,
  243. .read = seq_read,
  244. .llseek = seq_lseek,
  245. .release = aa_fs_seq_profile_release,
  246. };
  247. static int aa_fs_seq_profmode_show(struct seq_file *seq, void *v)
  248. {
  249. struct aa_proxy *proxy = seq->private;
  250. struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
  251. seq_printf(seq, "%s\n", aa_profile_mode_names[profile->mode]);
  252. aa_put_profile(profile);
  253. return 0;
  254. }
  255. static int aa_fs_seq_profmode_open(struct inode *inode, struct file *file)
  256. {
  257. return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profmode_show);
  258. }
  259. static const struct file_operations aa_fs_profmode_fops = {
  260. .owner = THIS_MODULE,
  261. .open = aa_fs_seq_profmode_open,
  262. .read = seq_read,
  263. .llseek = seq_lseek,
  264. .release = aa_fs_seq_profile_release,
  265. };
  266. static int aa_fs_seq_profattach_show(struct seq_file *seq, void *v)
  267. {
  268. struct aa_proxy *proxy = seq->private;
  269. struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
  270. if (profile->attach)
  271. seq_printf(seq, "%s\n", profile->attach);
  272. else if (profile->xmatch)
  273. seq_puts(seq, "<unknown>\n");
  274. else
  275. seq_printf(seq, "%s\n", profile->base.name);
  276. aa_put_profile(profile);
  277. return 0;
  278. }
  279. static int aa_fs_seq_profattach_open(struct inode *inode, struct file *file)
  280. {
  281. return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profattach_show);
  282. }
  283. static const struct file_operations aa_fs_profattach_fops = {
  284. .owner = THIS_MODULE,
  285. .open = aa_fs_seq_profattach_open,
  286. .read = seq_read,
  287. .llseek = seq_lseek,
  288. .release = aa_fs_seq_profile_release,
  289. };
  290. static int aa_fs_seq_hash_show(struct seq_file *seq, void *v)
  291. {
  292. struct aa_proxy *proxy = seq->private;
  293. struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
  294. unsigned int i, size = aa_hash_size();
  295. if (profile->hash) {
  296. for (i = 0; i < size; i++)
  297. seq_printf(seq, "%.2x", profile->hash[i]);
  298. seq_puts(seq, "\n");
  299. }
  300. aa_put_profile(profile);
  301. return 0;
  302. }
  303. static int aa_fs_seq_hash_open(struct inode *inode, struct file *file)
  304. {
  305. return single_open(file, aa_fs_seq_hash_show, inode->i_private);
  306. }
  307. static const struct file_operations aa_fs_seq_hash_fops = {
  308. .owner = THIS_MODULE,
  309. .open = aa_fs_seq_hash_open,
  310. .read = seq_read,
  311. .llseek = seq_lseek,
  312. .release = single_release,
  313. };
  314. static int aa_fs_seq_show_ns_level(struct seq_file *seq, void *v)
  315. {
  316. struct aa_ns *ns = aa_current_profile()->ns;
  317. seq_printf(seq, "%d\n", ns->level);
  318. return 0;
  319. }
  320. static int aa_fs_seq_open_ns_level(struct inode *inode, struct file *file)
  321. {
  322. return single_open(file, aa_fs_seq_show_ns_level, inode->i_private);
  323. }
  324. static const struct file_operations aa_fs_ns_level = {
  325. .owner = THIS_MODULE,
  326. .open = aa_fs_seq_open_ns_level,
  327. .read = seq_read,
  328. .llseek = seq_lseek,
  329. .release = single_release,
  330. };
  331. static int aa_fs_seq_show_ns_name(struct seq_file *seq, void *v)
  332. {
  333. struct aa_ns *ns = aa_current_profile()->ns;
  334. seq_printf(seq, "%s\n", ns->base.name);
  335. return 0;
  336. }
  337. static int aa_fs_seq_open_ns_name(struct inode *inode, struct file *file)
  338. {
  339. return single_open(file, aa_fs_seq_show_ns_name, inode->i_private);
  340. }
  341. static const struct file_operations aa_fs_ns_name = {
  342. .owner = THIS_MODULE,
  343. .open = aa_fs_seq_open_ns_name,
  344. .read = seq_read,
  345. .llseek = seq_lseek,
  346. .release = single_release,
  347. };
  348. static int rawdata_release(struct inode *inode, struct file *file)
  349. {
  350. /* TODO: switch to loaddata when profile switched to symlink */
  351. aa_put_loaddata(file->private_data);
  352. return 0;
  353. }
  354. static int aa_fs_seq_raw_abi_show(struct seq_file *seq, void *v)
  355. {
  356. struct aa_proxy *proxy = seq->private;
  357. struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
  358. if (profile->rawdata->abi) {
  359. seq_printf(seq, "v%d", profile->rawdata->abi);
  360. seq_puts(seq, "\n");
  361. }
  362. aa_put_profile(profile);
  363. return 0;
  364. }
  365. static int aa_fs_seq_raw_abi_open(struct inode *inode, struct file *file)
  366. {
  367. return aa_fs_seq_profile_open(inode, file, aa_fs_seq_raw_abi_show);
  368. }
  369. static const struct file_operations aa_fs_seq_raw_abi_fops = {
  370. .owner = THIS_MODULE,
  371. .open = aa_fs_seq_raw_abi_open,
  372. .read = seq_read,
  373. .llseek = seq_lseek,
  374. .release = aa_fs_seq_profile_release,
  375. };
  376. static int aa_fs_seq_raw_hash_show(struct seq_file *seq, void *v)
  377. {
  378. struct aa_proxy *proxy = seq->private;
  379. struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
  380. unsigned int i, size = aa_hash_size();
  381. if (profile->rawdata->hash) {
  382. for (i = 0; i < size; i++)
  383. seq_printf(seq, "%.2x", profile->rawdata->hash[i]);
  384. seq_puts(seq, "\n");
  385. }
  386. aa_put_profile(profile);
  387. return 0;
  388. }
  389. static int aa_fs_seq_raw_hash_open(struct inode *inode, struct file *file)
  390. {
  391. return aa_fs_seq_profile_open(inode, file, aa_fs_seq_raw_hash_show);
  392. }
  393. static const struct file_operations aa_fs_seq_raw_hash_fops = {
  394. .owner = THIS_MODULE,
  395. .open = aa_fs_seq_raw_hash_open,
  396. .read = seq_read,
  397. .llseek = seq_lseek,
  398. .release = aa_fs_seq_profile_release,
  399. };
  400. static ssize_t rawdata_read(struct file *file, char __user *buf, size_t size,
  401. loff_t *ppos)
  402. {
  403. struct aa_loaddata *rawdata = file->private_data;
  404. return simple_read_from_buffer(buf, size, ppos, rawdata->data,
  405. rawdata->size);
  406. }
  407. static int rawdata_open(struct inode *inode, struct file *file)
  408. {
  409. struct aa_proxy *proxy = inode->i_private;
  410. struct aa_profile *profile;
  411. if (!policy_view_capable(NULL))
  412. return -EACCES;
  413. profile = aa_get_profile_rcu(&proxy->profile);
  414. file->private_data = aa_get_loaddata(profile->rawdata);
  415. aa_put_profile(profile);
  416. return 0;
  417. }
  418. static const struct file_operations aa_fs_rawdata_fops = {
  419. .open = rawdata_open,
  420. .read = rawdata_read,
  421. .llseek = generic_file_llseek,
  422. .release = rawdata_release,
  423. };
  424. /** fns to setup dynamic per profile/namespace files **/
  425. void __aa_fs_profile_rmdir(struct aa_profile *profile)
  426. {
  427. struct aa_profile *child;
  428. int i;
  429. if (!profile)
  430. return;
  431. list_for_each_entry(child, &profile->base.profiles, base.list)
  432. __aa_fs_profile_rmdir(child);
  433. for (i = AAFS_PROF_SIZEOF - 1; i >= 0; --i) {
  434. struct aa_proxy *proxy;
  435. if (!profile->dents[i])
  436. continue;
  437. proxy = d_inode(profile->dents[i])->i_private;
  438. securityfs_remove(profile->dents[i]);
  439. aa_put_proxy(proxy);
  440. profile->dents[i] = NULL;
  441. }
  442. }
  443. void __aa_fs_profile_migrate_dents(struct aa_profile *old,
  444. struct aa_profile *new)
  445. {
  446. int i;
  447. for (i = 0; i < AAFS_PROF_SIZEOF; i++) {
  448. new->dents[i] = old->dents[i];
  449. if (new->dents[i])
  450. new->dents[i]->d_inode->i_mtime = current_time(new->dents[i]->d_inode);
  451. old->dents[i] = NULL;
  452. }
  453. }
  454. static struct dentry *create_profile_file(struct dentry *dir, const char *name,
  455. struct aa_profile *profile,
  456. const struct file_operations *fops)
  457. {
  458. struct aa_proxy *proxy = aa_get_proxy(profile->proxy);
  459. struct dentry *dent;
  460. dent = securityfs_create_file(name, S_IFREG | 0444, dir, proxy, fops);
  461. if (IS_ERR(dent))
  462. aa_put_proxy(proxy);
  463. return dent;
  464. }
  465. /* requires lock be held */
  466. int __aa_fs_profile_mkdir(struct aa_profile *profile, struct dentry *parent)
  467. {
  468. struct aa_profile *child;
  469. struct dentry *dent = NULL, *dir;
  470. int error;
  471. if (!parent) {
  472. struct aa_profile *p;
  473. p = aa_deref_parent(profile);
  474. dent = prof_dir(p);
  475. /* adding to parent that previously didn't have children */
  476. dent = securityfs_create_dir("profiles", dent);
  477. if (IS_ERR(dent))
  478. goto fail;
  479. prof_child_dir(p) = parent = dent;
  480. }
  481. if (!profile->dirname) {
  482. int len, id_len;
  483. len = mangle_name(profile->base.name, NULL);
  484. id_len = snprintf(NULL, 0, ".%ld", profile->ns->uniq_id);
  485. profile->dirname = kmalloc(len + id_len + 1, GFP_KERNEL);
  486. if (!profile->dirname)
  487. goto fail;
  488. mangle_name(profile->base.name, profile->dirname);
  489. sprintf(profile->dirname + len, ".%ld", profile->ns->uniq_id++);
  490. }
  491. dent = securityfs_create_dir(profile->dirname, parent);
  492. if (IS_ERR(dent))
  493. goto fail;
  494. prof_dir(profile) = dir = dent;
  495. dent = create_profile_file(dir, "name", profile, &aa_fs_profname_fops);
  496. if (IS_ERR(dent))
  497. goto fail;
  498. profile->dents[AAFS_PROF_NAME] = dent;
  499. dent = create_profile_file(dir, "mode", profile, &aa_fs_profmode_fops);
  500. if (IS_ERR(dent))
  501. goto fail;
  502. profile->dents[AAFS_PROF_MODE] = dent;
  503. dent = create_profile_file(dir, "attach", profile,
  504. &aa_fs_profattach_fops);
  505. if (IS_ERR(dent))
  506. goto fail;
  507. profile->dents[AAFS_PROF_ATTACH] = dent;
  508. if (profile->hash) {
  509. dent = create_profile_file(dir, "sha1", profile,
  510. &aa_fs_seq_hash_fops);
  511. if (IS_ERR(dent))
  512. goto fail;
  513. profile->dents[AAFS_PROF_HASH] = dent;
  514. }
  515. if (profile->rawdata) {
  516. dent = create_profile_file(dir, "raw_sha1", profile,
  517. &aa_fs_seq_raw_hash_fops);
  518. if (IS_ERR(dent))
  519. goto fail;
  520. profile->dents[AAFS_PROF_RAW_HASH] = dent;
  521. dent = create_profile_file(dir, "raw_abi", profile,
  522. &aa_fs_seq_raw_abi_fops);
  523. if (IS_ERR(dent))
  524. goto fail;
  525. profile->dents[AAFS_PROF_RAW_ABI] = dent;
  526. dent = securityfs_create_file("raw_data", S_IFREG | 0444, dir,
  527. profile->proxy,
  528. &aa_fs_rawdata_fops);
  529. if (IS_ERR(dent))
  530. goto fail;
  531. profile->dents[AAFS_PROF_RAW_DATA] = dent;
  532. d_inode(dent)->i_size = profile->rawdata->size;
  533. aa_get_proxy(profile->proxy);
  534. }
  535. list_for_each_entry(child, &profile->base.profiles, base.list) {
  536. error = __aa_fs_profile_mkdir(child, prof_child_dir(profile));
  537. if (error)
  538. goto fail2;
  539. }
  540. return 0;
  541. fail:
  542. error = PTR_ERR(dent);
  543. fail2:
  544. __aa_fs_profile_rmdir(profile);
  545. return error;
  546. }
  547. void __aa_fs_ns_rmdir(struct aa_ns *ns)
  548. {
  549. struct aa_ns *sub;
  550. struct aa_profile *child;
  551. int i;
  552. if (!ns)
  553. return;
  554. list_for_each_entry(child, &ns->base.profiles, base.list)
  555. __aa_fs_profile_rmdir(child);
  556. list_for_each_entry(sub, &ns->sub_ns, base.list) {
  557. mutex_lock(&sub->lock);
  558. __aa_fs_ns_rmdir(sub);
  559. mutex_unlock(&sub->lock);
  560. }
  561. for (i = AAFS_NS_SIZEOF - 1; i >= 0; --i) {
  562. securityfs_remove(ns->dents[i]);
  563. ns->dents[i] = NULL;
  564. }
  565. }
  566. int __aa_fs_ns_mkdir(struct aa_ns *ns, struct dentry *parent, const char *name)
  567. {
  568. struct aa_ns *sub;
  569. struct aa_profile *child;
  570. struct dentry *dent, *dir;
  571. int error;
  572. if (!name)
  573. name = ns->base.name;
  574. dent = securityfs_create_dir(name, parent);
  575. if (IS_ERR(dent))
  576. goto fail;
  577. ns_dir(ns) = dir = dent;
  578. dent = securityfs_create_dir("profiles", dir);
  579. if (IS_ERR(dent))
  580. goto fail;
  581. ns_subprofs_dir(ns) = dent;
  582. dent = securityfs_create_dir("namespaces", dir);
  583. if (IS_ERR(dent))
  584. goto fail;
  585. ns_subns_dir(ns) = dent;
  586. list_for_each_entry(child, &ns->base.profiles, base.list) {
  587. error = __aa_fs_profile_mkdir(child, ns_subprofs_dir(ns));
  588. if (error)
  589. goto fail2;
  590. }
  591. list_for_each_entry(sub, &ns->sub_ns, base.list) {
  592. mutex_lock(&sub->lock);
  593. error = __aa_fs_ns_mkdir(sub, ns_subns_dir(ns), NULL);
  594. mutex_unlock(&sub->lock);
  595. if (error)
  596. goto fail2;
  597. }
  598. return 0;
  599. fail:
  600. error = PTR_ERR(dent);
  601. fail2:
  602. __aa_fs_ns_rmdir(ns);
  603. return error;
  604. }
  605. #define list_entry_is_head(pos, head, member) (&pos->member == (head))
  606. /**
  607. * __next_ns - find the next namespace to list
  608. * @root: root namespace to stop search at (NOT NULL)
  609. * @ns: current ns position (NOT NULL)
  610. *
  611. * Find the next namespace from @ns under @root and handle all locking needed
  612. * while switching current namespace.
  613. *
  614. * Returns: next namespace or NULL if at last namespace under @root
  615. * Requires: ns->parent->lock to be held
  616. * NOTE: will not unlock root->lock
  617. */
  618. static struct aa_ns *__next_ns(struct aa_ns *root, struct aa_ns *ns)
  619. {
  620. struct aa_ns *parent, *next;
  621. /* is next namespace a child */
  622. if (!list_empty(&ns->sub_ns)) {
  623. next = list_first_entry(&ns->sub_ns, typeof(*ns), base.list);
  624. mutex_lock(&next->lock);
  625. return next;
  626. }
  627. /* check if the next ns is a sibling, parent, gp, .. */
  628. parent = ns->parent;
  629. while (ns != root) {
  630. mutex_unlock(&ns->lock);
  631. next = list_next_entry(ns, base.list);
  632. if (!list_entry_is_head(next, &parent->sub_ns, base.list)) {
  633. mutex_lock(&next->lock);
  634. return next;
  635. }
  636. ns = parent;
  637. parent = parent->parent;
  638. }
  639. return NULL;
  640. }
  641. /**
  642. * __first_profile - find the first profile in a namespace
  643. * @root: namespace that is root of profiles being displayed (NOT NULL)
  644. * @ns: namespace to start in (NOT NULL)
  645. *
  646. * Returns: unrefcounted profile or NULL if no profile
  647. * Requires: profile->ns.lock to be held
  648. */
  649. static struct aa_profile *__first_profile(struct aa_ns *root,
  650. struct aa_ns *ns)
  651. {
  652. for (; ns; ns = __next_ns(root, ns)) {
  653. if (!list_empty(&ns->base.profiles))
  654. return list_first_entry(&ns->base.profiles,
  655. struct aa_profile, base.list);
  656. }
  657. return NULL;
  658. }
  659. /**
  660. * __next_profile - step to the next profile in a profile tree
  661. * @profile: current profile in tree (NOT NULL)
  662. *
  663. * Perform a depth first traversal on the profile tree in a namespace
  664. *
  665. * Returns: next profile or NULL if done
  666. * Requires: profile->ns.lock to be held
  667. */
  668. static struct aa_profile *__next_profile(struct aa_profile *p)
  669. {
  670. struct aa_profile *parent;
  671. struct aa_ns *ns = p->ns;
  672. /* is next profile a child */
  673. if (!list_empty(&p->base.profiles))
  674. return list_first_entry(&p->base.profiles, typeof(*p),
  675. base.list);
  676. /* is next profile a sibling, parent sibling, gp, sibling, .. */
  677. parent = rcu_dereference_protected(p->parent,
  678. mutex_is_locked(&p->ns->lock));
  679. while (parent) {
  680. p = list_next_entry(p, base.list);
  681. if (!list_entry_is_head(p, &parent->base.profiles, base.list))
  682. return p;
  683. p = parent;
  684. parent = rcu_dereference_protected(parent->parent,
  685. mutex_is_locked(&parent->ns->lock));
  686. }
  687. /* is next another profile in the namespace */
  688. p = list_next_entry(p, base.list);
  689. if (!list_entry_is_head(p, &ns->base.profiles, base.list))
  690. return p;
  691. return NULL;
  692. }
  693. /**
  694. * next_profile - step to the next profile in where ever it may be
  695. * @root: root namespace (NOT NULL)
  696. * @profile: current profile (NOT NULL)
  697. *
  698. * Returns: next profile or NULL if there isn't one
  699. */
  700. static struct aa_profile *next_profile(struct aa_ns *root,
  701. struct aa_profile *profile)
  702. {
  703. struct aa_profile *next = __next_profile(profile);
  704. if (next)
  705. return next;
  706. /* finished all profiles in namespace move to next namespace */
  707. return __first_profile(root, __next_ns(root, profile->ns));
  708. }
  709. /**
  710. * p_start - start a depth first traversal of profile tree
  711. * @f: seq_file to fill
  712. * @pos: current position
  713. *
  714. * Returns: first profile under current namespace or NULL if none found
  715. *
  716. * acquires first ns->lock
  717. */
  718. static void *p_start(struct seq_file *f, loff_t *pos)
  719. {
  720. struct aa_profile *profile = NULL;
  721. struct aa_ns *root = aa_current_profile()->ns;
  722. loff_t l = *pos;
  723. f->private = aa_get_ns(root);
  724. /* find the first profile */
  725. mutex_lock(&root->lock);
  726. profile = __first_profile(root, root);
  727. /* skip to position */
  728. for (; profile && l > 0; l--)
  729. profile = next_profile(root, profile);
  730. return profile;
  731. }
  732. /**
  733. * p_next - read the next profile entry
  734. * @f: seq_file to fill
  735. * @p: profile previously returned
  736. * @pos: current position
  737. *
  738. * Returns: next profile after @p or NULL if none
  739. *
  740. * may acquire/release locks in namespace tree as necessary
  741. */
  742. static void *p_next(struct seq_file *f, void *p, loff_t *pos)
  743. {
  744. struct aa_profile *profile = p;
  745. struct aa_ns *ns = f->private;
  746. (*pos)++;
  747. return next_profile(ns, profile);
  748. }
  749. /**
  750. * p_stop - stop depth first traversal
  751. * @f: seq_file we are filling
  752. * @p: the last profile writen
  753. *
  754. * Release all locking done by p_start/p_next on namespace tree
  755. */
  756. static void p_stop(struct seq_file *f, void *p)
  757. {
  758. struct aa_profile *profile = p;
  759. struct aa_ns *root = f->private, *ns;
  760. if (profile) {
  761. for (ns = profile->ns; ns && ns != root; ns = ns->parent)
  762. mutex_unlock(&ns->lock);
  763. }
  764. mutex_unlock(&root->lock);
  765. aa_put_ns(root);
  766. }
  767. /**
  768. * seq_show_profile - show a profile entry
  769. * @f: seq_file to file
  770. * @p: current position (profile) (NOT NULL)
  771. *
  772. * Returns: error on failure
  773. */
  774. static int seq_show_profile(struct seq_file *f, void *p)
  775. {
  776. struct aa_profile *profile = (struct aa_profile *)p;
  777. struct aa_ns *root = f->private;
  778. if (profile->ns != root)
  779. seq_printf(f, ":%s://", aa_ns_name(root, profile->ns, true));
  780. seq_printf(f, "%s (%s)\n", profile->base.hname,
  781. aa_profile_mode_names[profile->mode]);
  782. return 0;
  783. }
  784. static const struct seq_operations aa_fs_profiles_op = {
  785. .start = p_start,
  786. .next = p_next,
  787. .stop = p_stop,
  788. .show = seq_show_profile,
  789. };
  790. static int profiles_open(struct inode *inode, struct file *file)
  791. {
  792. if (!policy_view_capable(NULL))
  793. return -EACCES;
  794. return seq_open(file, &aa_fs_profiles_op);
  795. }
  796. static int profiles_release(struct inode *inode, struct file *file)
  797. {
  798. return seq_release(inode, file);
  799. }
  800. static const struct file_operations aa_fs_profiles_fops = {
  801. .open = profiles_open,
  802. .read = seq_read,
  803. .llseek = seq_lseek,
  804. .release = profiles_release,
  805. };
  806. /** Base file system setup **/
  807. static struct aa_fs_entry aa_fs_entry_file[] = {
  808. AA_FS_FILE_STRING("mask", "create read write exec append mmap_exec " \
  809. "link lock"),
  810. { }
  811. };
  812. static struct aa_fs_entry aa_fs_entry_domain[] = {
  813. AA_FS_FILE_BOOLEAN("change_hat", 1),
  814. AA_FS_FILE_BOOLEAN("change_hatv", 1),
  815. AA_FS_FILE_BOOLEAN("change_onexec", 1),
  816. AA_FS_FILE_BOOLEAN("change_profile", 1),
  817. AA_FS_FILE_BOOLEAN("fix_binfmt_elf_mmap", 1),
  818. { }
  819. };
  820. static struct aa_fs_entry aa_fs_entry_versions[] = {
  821. AA_FS_FILE_BOOLEAN("v5", 1),
  822. { }
  823. };
  824. static struct aa_fs_entry aa_fs_entry_policy[] = {
  825. AA_FS_DIR("versions", aa_fs_entry_versions),
  826. AA_FS_FILE_BOOLEAN("set_load", 1),
  827. { }
  828. };
  829. static struct aa_fs_entry aa_fs_entry_features[] = {
  830. AA_FS_DIR("policy", aa_fs_entry_policy),
  831. AA_FS_DIR("domain", aa_fs_entry_domain),
  832. AA_FS_DIR("file", aa_fs_entry_file),
  833. AA_FS_FILE_U64("capability", VFS_CAP_FLAGS_MASK),
  834. AA_FS_DIR("rlimit", aa_fs_entry_rlimit),
  835. AA_FS_DIR("caps", aa_fs_entry_caps),
  836. { }
  837. };
  838. static struct aa_fs_entry aa_fs_entry_apparmor[] = {
  839. AA_FS_FILE_FOPS(".load", 0640, &aa_fs_profile_load),
  840. AA_FS_FILE_FOPS(".replace", 0640, &aa_fs_profile_replace),
  841. AA_FS_FILE_FOPS(".remove", 0640, &aa_fs_profile_remove),
  842. AA_FS_FILE_FOPS(".ns_level", 0666, &aa_fs_ns_level),
  843. AA_FS_FILE_FOPS(".ns_name", 0640, &aa_fs_ns_name),
  844. AA_FS_FILE_FOPS("profiles", 0640, &aa_fs_profiles_fops),
  845. AA_FS_DIR("features", aa_fs_entry_features),
  846. { }
  847. };
  848. static struct aa_fs_entry aa_fs_entry =
  849. AA_FS_DIR("apparmor", aa_fs_entry_apparmor);
  850. /**
  851. * aafs_create_file - create a file entry in the apparmor securityfs
  852. * @fs_file: aa_fs_entry to build an entry for (NOT NULL)
  853. * @parent: the parent dentry in the securityfs
  854. *
  855. * Use aafs_remove_file to remove entries created with this fn.
  856. */
  857. static int __init aafs_create_file(struct aa_fs_entry *fs_file,
  858. struct dentry *parent)
  859. {
  860. int error = 0;
  861. fs_file->dentry = securityfs_create_file(fs_file->name,
  862. S_IFREG | fs_file->mode,
  863. parent, fs_file,
  864. fs_file->file_ops);
  865. if (IS_ERR(fs_file->dentry)) {
  866. error = PTR_ERR(fs_file->dentry);
  867. fs_file->dentry = NULL;
  868. }
  869. return error;
  870. }
  871. static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir);
  872. /**
  873. * aafs_create_dir - recursively create a directory entry in the securityfs
  874. * @fs_dir: aa_fs_entry (and all child entries) to build (NOT NULL)
  875. * @parent: the parent dentry in the securityfs
  876. *
  877. * Use aafs_remove_dir to remove entries created with this fn.
  878. */
  879. static int __init aafs_create_dir(struct aa_fs_entry *fs_dir,
  880. struct dentry *parent)
  881. {
  882. struct aa_fs_entry *fs_file;
  883. struct dentry *dir;
  884. int error;
  885. dir = securityfs_create_dir(fs_dir->name, parent);
  886. if (IS_ERR(dir))
  887. return PTR_ERR(dir);
  888. fs_dir->dentry = dir;
  889. for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
  890. if (fs_file->v_type == AA_FS_TYPE_DIR)
  891. error = aafs_create_dir(fs_file, fs_dir->dentry);
  892. else
  893. error = aafs_create_file(fs_file, fs_dir->dentry);
  894. if (error)
  895. goto failed;
  896. }
  897. return 0;
  898. failed:
  899. aafs_remove_dir(fs_dir);
  900. return error;
  901. }
  902. /**
  903. * aafs_remove_file - drop a single file entry in the apparmor securityfs
  904. * @fs_file: aa_fs_entry to detach from the securityfs (NOT NULL)
  905. */
  906. static void __init aafs_remove_file(struct aa_fs_entry *fs_file)
  907. {
  908. if (!fs_file->dentry)
  909. return;
  910. securityfs_remove(fs_file->dentry);
  911. fs_file->dentry = NULL;
  912. }
  913. /**
  914. * aafs_remove_dir - recursively drop a directory entry from the securityfs
  915. * @fs_dir: aa_fs_entry (and all child entries) to detach (NOT NULL)
  916. */
  917. static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir)
  918. {
  919. struct aa_fs_entry *fs_file;
  920. for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
  921. if (fs_file->v_type == AA_FS_TYPE_DIR)
  922. aafs_remove_dir(fs_file);
  923. else
  924. aafs_remove_file(fs_file);
  925. }
  926. aafs_remove_file(fs_dir);
  927. }
  928. /**
  929. * aa_destroy_aafs - cleanup and free aafs
  930. *
  931. * releases dentries allocated by aa_create_aafs
  932. */
  933. void __init aa_destroy_aafs(void)
  934. {
  935. aafs_remove_dir(&aa_fs_entry);
  936. }
  937. #define NULL_FILE_NAME ".null"
  938. struct path aa_null;
  939. static int aa_mk_null_file(struct dentry *parent)
  940. {
  941. struct vfsmount *mount = NULL;
  942. struct dentry *dentry;
  943. struct inode *inode;
  944. int count = 0;
  945. int error = simple_pin_fs(parent->d_sb->s_type, &mount, &count);
  946. if (error)
  947. return error;
  948. inode_lock(d_inode(parent));
  949. dentry = lookup_one_len(NULL_FILE_NAME, parent, strlen(NULL_FILE_NAME));
  950. if (IS_ERR(dentry)) {
  951. error = PTR_ERR(dentry);
  952. goto out;
  953. }
  954. inode = new_inode(parent->d_inode->i_sb);
  955. if (!inode) {
  956. error = -ENOMEM;
  957. goto out1;
  958. }
  959. inode->i_ino = get_next_ino();
  960. inode->i_mode = S_IFCHR | S_IRUGO | S_IWUGO;
  961. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  962. init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO,
  963. MKDEV(MEM_MAJOR, 3));
  964. d_instantiate(dentry, inode);
  965. aa_null.dentry = dget(dentry);
  966. aa_null.mnt = mntget(mount);
  967. error = 0;
  968. out1:
  969. dput(dentry);
  970. out:
  971. inode_unlock(d_inode(parent));
  972. simple_release_fs(&mount, &count);
  973. return error;
  974. }
  975. /**
  976. * aa_create_aafs - create the apparmor security filesystem
  977. *
  978. * dentries created here are released by aa_destroy_aafs
  979. *
  980. * Returns: error on failure
  981. */
  982. static int __init aa_create_aafs(void)
  983. {
  984. int error;
  985. if (!apparmor_initialized)
  986. return 0;
  987. if (aa_fs_entry.dentry) {
  988. AA_ERROR("%s: AppArmor securityfs already exists\n", __func__);
  989. return -EEXIST;
  990. }
  991. /* Populate fs tree. */
  992. error = aafs_create_dir(&aa_fs_entry, NULL);
  993. if (error)
  994. goto error;
  995. error = __aa_fs_ns_mkdir(root_ns, aa_fs_entry.dentry, "policy");
  996. if (error)
  997. goto error;
  998. error = aa_mk_null_file(aa_fs_entry.dentry);
  999. if (error)
  1000. goto error;
  1001. /* TODO: add default profile to apparmorfs */
  1002. /* Report that AppArmor fs is enabled */
  1003. aa_info_message("AppArmor Filesystem Enabled");
  1004. return 0;
  1005. error:
  1006. aa_destroy_aafs();
  1007. AA_ERROR("Error creating AppArmor securityfs\n");
  1008. return error;
  1009. }
  1010. fs_initcall(aa_create_aafs);