apparmorfs.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  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/namei.h>
  21. #include <linux/capability.h>
  22. #include <linux/rcupdate.h>
  23. #include "include/apparmor.h"
  24. #include "include/apparmorfs.h"
  25. #include "include/audit.h"
  26. #include "include/context.h"
  27. #include "include/crypto.h"
  28. #include "include/policy.h"
  29. #include "include/policy_ns.h"
  30. #include "include/resource.h"
  31. /**
  32. * aa_mangle_name - mangle a profile name to std profile layout form
  33. * @name: profile name to mangle (NOT NULL)
  34. * @target: buffer to store mangled name, same length as @name (MAYBE NULL)
  35. *
  36. * Returns: length of mangled name
  37. */
  38. static int mangle_name(char *name, char *target)
  39. {
  40. char *t = target;
  41. while (*name == '/' || *name == '.')
  42. name++;
  43. if (target) {
  44. for (; *name; name++) {
  45. if (*name == '/')
  46. *(t)++ = '.';
  47. else if (isspace(*name))
  48. *(t)++ = '_';
  49. else if (isalnum(*name) || strchr("._-", *name))
  50. *(t)++ = *name;
  51. }
  52. *t = 0;
  53. } else {
  54. int len = 0;
  55. for (; *name; name++) {
  56. if (isalnum(*name) || isspace(*name) ||
  57. strchr("/._-", *name))
  58. len++;
  59. }
  60. return len;
  61. }
  62. return t - target;
  63. }
  64. /**
  65. * aa_simple_write_to_buffer - common routine for getting policy from user
  66. * @op: operation doing the user buffer copy
  67. * @userbuf: user buffer to copy data from (NOT NULL)
  68. * @alloc_size: size of user buffer (REQUIRES: @alloc_size >= @copy_size)
  69. * @copy_size: size of data to copy from user buffer
  70. * @pos: position write is at in the file (NOT NULL)
  71. *
  72. * Returns: kernel buffer containing copy of user buffer data or an
  73. * ERR_PTR on failure.
  74. */
  75. static char *aa_simple_write_to_buffer(int op, const char __user *userbuf,
  76. size_t alloc_size, size_t copy_size,
  77. loff_t *pos)
  78. {
  79. char *data;
  80. BUG_ON(copy_size > alloc_size);
  81. if (*pos != 0)
  82. /* only writes from pos 0, that is complete writes */
  83. return ERR_PTR(-ESPIPE);
  84. /*
  85. * Don't allow profile load/replace/remove from profiles that don't
  86. * have CAP_MAC_ADMIN
  87. */
  88. if (!aa_may_manage_policy(op))
  89. return ERR_PTR(-EACCES);
  90. /* freed by caller to simple_write_to_buffer */
  91. data = kvmalloc(alloc_size);
  92. if (data == NULL)
  93. return ERR_PTR(-ENOMEM);
  94. if (copy_from_user(data, userbuf, copy_size)) {
  95. kvfree(data);
  96. return ERR_PTR(-EFAULT);
  97. }
  98. return data;
  99. }
  100. /* .load file hook fn to load policy */
  101. static ssize_t profile_load(struct file *f, const char __user *buf, size_t size,
  102. loff_t *pos)
  103. {
  104. char *data;
  105. ssize_t error;
  106. data = aa_simple_write_to_buffer(OP_PROF_LOAD, buf, size, size, pos);
  107. error = PTR_ERR(data);
  108. if (!IS_ERR(data)) {
  109. error = aa_replace_profiles(data, size, PROF_ADD);
  110. kvfree(data);
  111. }
  112. return error;
  113. }
  114. static const struct file_operations aa_fs_profile_load = {
  115. .write = profile_load,
  116. .llseek = default_llseek,
  117. };
  118. /* .replace file hook fn to load and/or replace policy */
  119. static ssize_t profile_replace(struct file *f, const char __user *buf,
  120. size_t size, loff_t *pos)
  121. {
  122. char *data;
  123. ssize_t error;
  124. data = aa_simple_write_to_buffer(OP_PROF_REPL, buf, size, size, pos);
  125. error = PTR_ERR(data);
  126. if (!IS_ERR(data)) {
  127. error = aa_replace_profiles(data, size, PROF_REPLACE);
  128. kvfree(data);
  129. }
  130. return error;
  131. }
  132. static const struct file_operations aa_fs_profile_replace = {
  133. .write = profile_replace,
  134. .llseek = default_llseek,
  135. };
  136. /* .remove file hook fn to remove loaded policy */
  137. static ssize_t profile_remove(struct file *f, const char __user *buf,
  138. size_t size, loff_t *pos)
  139. {
  140. char *data;
  141. ssize_t error;
  142. /*
  143. * aa_remove_profile needs a null terminated string so 1 extra
  144. * byte is allocated and the copied data is null terminated.
  145. */
  146. data = aa_simple_write_to_buffer(OP_PROF_RM, buf, size + 1, size, pos);
  147. error = PTR_ERR(data);
  148. if (!IS_ERR(data)) {
  149. data[size] = 0;
  150. error = aa_remove_profiles(data, size);
  151. kvfree(data);
  152. }
  153. return error;
  154. }
  155. static const struct file_operations aa_fs_profile_remove = {
  156. .write = profile_remove,
  157. .llseek = default_llseek,
  158. };
  159. static int aa_fs_seq_show(struct seq_file *seq, void *v)
  160. {
  161. struct aa_fs_entry *fs_file = seq->private;
  162. if (!fs_file)
  163. return 0;
  164. switch (fs_file->v_type) {
  165. case AA_FS_TYPE_BOOLEAN:
  166. seq_printf(seq, "%s\n", fs_file->v.boolean ? "yes" : "no");
  167. break;
  168. case AA_FS_TYPE_STRING:
  169. seq_printf(seq, "%s\n", fs_file->v.string);
  170. break;
  171. case AA_FS_TYPE_U64:
  172. seq_printf(seq, "%#08lx\n", fs_file->v.u64);
  173. break;
  174. default:
  175. /* Ignore unpritable entry types. */
  176. break;
  177. }
  178. return 0;
  179. }
  180. static int aa_fs_seq_open(struct inode *inode, struct file *file)
  181. {
  182. return single_open(file, aa_fs_seq_show, inode->i_private);
  183. }
  184. const struct file_operations aa_fs_seq_file_ops = {
  185. .owner = THIS_MODULE,
  186. .open = aa_fs_seq_open,
  187. .read = seq_read,
  188. .llseek = seq_lseek,
  189. .release = single_release,
  190. };
  191. static int aa_fs_seq_profile_open(struct inode *inode, struct file *file,
  192. int (*show)(struct seq_file *, void *))
  193. {
  194. struct aa_proxy *proxy = aa_get_proxy(inode->i_private);
  195. int error = single_open(file, show, proxy);
  196. if (error) {
  197. file->private_data = NULL;
  198. aa_put_proxy(proxy);
  199. }
  200. return error;
  201. }
  202. static int aa_fs_seq_profile_release(struct inode *inode, struct file *file)
  203. {
  204. struct seq_file *seq = (struct seq_file *) file->private_data;
  205. if (seq)
  206. aa_put_proxy(seq->private);
  207. return single_release(inode, file);
  208. }
  209. static int aa_fs_seq_profname_show(struct seq_file *seq, void *v)
  210. {
  211. struct aa_proxy *proxy = seq->private;
  212. struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
  213. seq_printf(seq, "%s\n", profile->base.name);
  214. aa_put_profile(profile);
  215. return 0;
  216. }
  217. static int aa_fs_seq_profname_open(struct inode *inode, struct file *file)
  218. {
  219. return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profname_show);
  220. }
  221. static const struct file_operations aa_fs_profname_fops = {
  222. .owner = THIS_MODULE,
  223. .open = aa_fs_seq_profname_open,
  224. .read = seq_read,
  225. .llseek = seq_lseek,
  226. .release = aa_fs_seq_profile_release,
  227. };
  228. static int aa_fs_seq_profmode_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", aa_profile_mode_names[profile->mode]);
  233. aa_put_profile(profile);
  234. return 0;
  235. }
  236. static int aa_fs_seq_profmode_open(struct inode *inode, struct file *file)
  237. {
  238. return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profmode_show);
  239. }
  240. static const struct file_operations aa_fs_profmode_fops = {
  241. .owner = THIS_MODULE,
  242. .open = aa_fs_seq_profmode_open,
  243. .read = seq_read,
  244. .llseek = seq_lseek,
  245. .release = aa_fs_seq_profile_release,
  246. };
  247. static int aa_fs_seq_profattach_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. if (profile->attach)
  252. seq_printf(seq, "%s\n", profile->attach);
  253. else if (profile->xmatch)
  254. seq_puts(seq, "<unknown>\n");
  255. else
  256. seq_printf(seq, "%s\n", profile->base.name);
  257. aa_put_profile(profile);
  258. return 0;
  259. }
  260. static int aa_fs_seq_profattach_open(struct inode *inode, struct file *file)
  261. {
  262. return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profattach_show);
  263. }
  264. static const struct file_operations aa_fs_profattach_fops = {
  265. .owner = THIS_MODULE,
  266. .open = aa_fs_seq_profattach_open,
  267. .read = seq_read,
  268. .llseek = seq_lseek,
  269. .release = aa_fs_seq_profile_release,
  270. };
  271. static int aa_fs_seq_hash_show(struct seq_file *seq, void *v)
  272. {
  273. struct aa_proxy *proxy = seq->private;
  274. struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
  275. unsigned int i, size = aa_hash_size();
  276. if (profile->hash) {
  277. for (i = 0; i < size; i++)
  278. seq_printf(seq, "%.2x", profile->hash[i]);
  279. seq_puts(seq, "\n");
  280. }
  281. aa_put_profile(profile);
  282. return 0;
  283. }
  284. static int aa_fs_seq_hash_open(struct inode *inode, struct file *file)
  285. {
  286. return single_open(file, aa_fs_seq_hash_show, inode->i_private);
  287. }
  288. static const struct file_operations aa_fs_seq_hash_fops = {
  289. .owner = THIS_MODULE,
  290. .open = aa_fs_seq_hash_open,
  291. .read = seq_read,
  292. .llseek = seq_lseek,
  293. .release = single_release,
  294. };
  295. /** fns to setup dynamic per profile/namespace files **/
  296. void __aa_fs_profile_rmdir(struct aa_profile *profile)
  297. {
  298. struct aa_profile *child;
  299. int i;
  300. if (!profile)
  301. return;
  302. list_for_each_entry(child, &profile->base.profiles, base.list)
  303. __aa_fs_profile_rmdir(child);
  304. for (i = AAFS_PROF_SIZEOF - 1; i >= 0; --i) {
  305. struct aa_proxy *proxy;
  306. if (!profile->dents[i])
  307. continue;
  308. proxy = d_inode(profile->dents[i])->i_private;
  309. securityfs_remove(profile->dents[i]);
  310. aa_put_proxy(proxy);
  311. profile->dents[i] = NULL;
  312. }
  313. }
  314. void __aa_fs_profile_migrate_dents(struct aa_profile *old,
  315. struct aa_profile *new)
  316. {
  317. int i;
  318. for (i = 0; i < AAFS_PROF_SIZEOF; i++) {
  319. new->dents[i] = old->dents[i];
  320. if (new->dents[i])
  321. new->dents[i]->d_inode->i_mtime = current_time(new->dents[i]->d_inode);
  322. old->dents[i] = NULL;
  323. }
  324. }
  325. static struct dentry *create_profile_file(struct dentry *dir, const char *name,
  326. struct aa_profile *profile,
  327. const struct file_operations *fops)
  328. {
  329. struct aa_proxy *proxy = aa_get_proxy(profile->proxy);
  330. struct dentry *dent;
  331. dent = securityfs_create_file(name, S_IFREG | 0444, dir, proxy, fops);
  332. if (IS_ERR(dent))
  333. aa_put_proxy(proxy);
  334. return dent;
  335. }
  336. /* requires lock be held */
  337. int __aa_fs_profile_mkdir(struct aa_profile *profile, struct dentry *parent)
  338. {
  339. struct aa_profile *child;
  340. struct dentry *dent = NULL, *dir;
  341. int error;
  342. if (!parent) {
  343. struct aa_profile *p;
  344. p = aa_deref_parent(profile);
  345. dent = prof_dir(p);
  346. /* adding to parent that previously didn't have children */
  347. dent = securityfs_create_dir("profiles", dent);
  348. if (IS_ERR(dent))
  349. goto fail;
  350. prof_child_dir(p) = parent = dent;
  351. }
  352. if (!profile->dirname) {
  353. int len, id_len;
  354. len = mangle_name(profile->base.name, NULL);
  355. id_len = snprintf(NULL, 0, ".%ld", profile->ns->uniq_id);
  356. profile->dirname = kmalloc(len + id_len + 1, GFP_KERNEL);
  357. if (!profile->dirname)
  358. goto fail;
  359. mangle_name(profile->base.name, profile->dirname);
  360. sprintf(profile->dirname + len, ".%ld", profile->ns->uniq_id++);
  361. }
  362. dent = securityfs_create_dir(profile->dirname, parent);
  363. if (IS_ERR(dent))
  364. goto fail;
  365. prof_dir(profile) = dir = dent;
  366. dent = create_profile_file(dir, "name", profile, &aa_fs_profname_fops);
  367. if (IS_ERR(dent))
  368. goto fail;
  369. profile->dents[AAFS_PROF_NAME] = dent;
  370. dent = create_profile_file(dir, "mode", profile, &aa_fs_profmode_fops);
  371. if (IS_ERR(dent))
  372. goto fail;
  373. profile->dents[AAFS_PROF_MODE] = dent;
  374. dent = create_profile_file(dir, "attach", profile,
  375. &aa_fs_profattach_fops);
  376. if (IS_ERR(dent))
  377. goto fail;
  378. profile->dents[AAFS_PROF_ATTACH] = dent;
  379. if (profile->hash) {
  380. dent = create_profile_file(dir, "sha1", profile,
  381. &aa_fs_seq_hash_fops);
  382. if (IS_ERR(dent))
  383. goto fail;
  384. profile->dents[AAFS_PROF_HASH] = dent;
  385. }
  386. list_for_each_entry(child, &profile->base.profiles, base.list) {
  387. error = __aa_fs_profile_mkdir(child, prof_child_dir(profile));
  388. if (error)
  389. goto fail2;
  390. }
  391. return 0;
  392. fail:
  393. error = PTR_ERR(dent);
  394. fail2:
  395. __aa_fs_profile_rmdir(profile);
  396. return error;
  397. }
  398. void __aa_fs_ns_rmdir(struct aa_ns *ns)
  399. {
  400. struct aa_ns *sub;
  401. struct aa_profile *child;
  402. int i;
  403. if (!ns)
  404. return;
  405. list_for_each_entry(child, &ns->base.profiles, base.list)
  406. __aa_fs_profile_rmdir(child);
  407. list_for_each_entry(sub, &ns->sub_ns, base.list) {
  408. mutex_lock(&sub->lock);
  409. __aa_fs_ns_rmdir(sub);
  410. mutex_unlock(&sub->lock);
  411. }
  412. for (i = AAFS_NS_SIZEOF - 1; i >= 0; --i) {
  413. securityfs_remove(ns->dents[i]);
  414. ns->dents[i] = NULL;
  415. }
  416. }
  417. int __aa_fs_ns_mkdir(struct aa_ns *ns, struct dentry *parent, const char *name)
  418. {
  419. struct aa_ns *sub;
  420. struct aa_profile *child;
  421. struct dentry *dent, *dir;
  422. int error;
  423. if (!name)
  424. name = ns->base.name;
  425. dent = securityfs_create_dir(name, parent);
  426. if (IS_ERR(dent))
  427. goto fail;
  428. ns_dir(ns) = dir = dent;
  429. dent = securityfs_create_dir("profiles", dir);
  430. if (IS_ERR(dent))
  431. goto fail;
  432. ns_subprofs_dir(ns) = dent;
  433. dent = securityfs_create_dir("namespaces", dir);
  434. if (IS_ERR(dent))
  435. goto fail;
  436. ns_subns_dir(ns) = dent;
  437. list_for_each_entry(child, &ns->base.profiles, base.list) {
  438. error = __aa_fs_profile_mkdir(child, ns_subprofs_dir(ns));
  439. if (error)
  440. goto fail2;
  441. }
  442. list_for_each_entry(sub, &ns->sub_ns, base.list) {
  443. mutex_lock(&sub->lock);
  444. error = __aa_fs_ns_mkdir(sub, ns_subns_dir(ns), NULL);
  445. mutex_unlock(&sub->lock);
  446. if (error)
  447. goto fail2;
  448. }
  449. return 0;
  450. fail:
  451. error = PTR_ERR(dent);
  452. fail2:
  453. __aa_fs_ns_rmdir(ns);
  454. return error;
  455. }
  456. #define list_entry_is_head(pos, head, member) (&pos->member == (head))
  457. /**
  458. * __next_ns - find the next namespace to list
  459. * @root: root namespace to stop search at (NOT NULL)
  460. * @ns: current ns position (NOT NULL)
  461. *
  462. * Find the next namespace from @ns under @root and handle all locking needed
  463. * while switching current namespace.
  464. *
  465. * Returns: next namespace or NULL if at last namespace under @root
  466. * Requires: ns->parent->lock to be held
  467. * NOTE: will not unlock root->lock
  468. */
  469. static struct aa_ns *__next_ns(struct aa_ns *root, struct aa_ns *ns)
  470. {
  471. struct aa_ns *parent, *next;
  472. /* is next namespace a child */
  473. if (!list_empty(&ns->sub_ns)) {
  474. next = list_first_entry(&ns->sub_ns, typeof(*ns), base.list);
  475. mutex_lock(&next->lock);
  476. return next;
  477. }
  478. /* check if the next ns is a sibling, parent, gp, .. */
  479. parent = ns->parent;
  480. while (ns != root) {
  481. mutex_unlock(&ns->lock);
  482. next = list_next_entry(ns, base.list);
  483. if (!list_entry_is_head(next, &parent->sub_ns, base.list)) {
  484. mutex_lock(&next->lock);
  485. return next;
  486. }
  487. ns = parent;
  488. parent = parent->parent;
  489. }
  490. return NULL;
  491. }
  492. /**
  493. * __first_profile - find the first profile in a namespace
  494. * @root: namespace that is root of profiles being displayed (NOT NULL)
  495. * @ns: namespace to start in (NOT NULL)
  496. *
  497. * Returns: unrefcounted profile or NULL if no profile
  498. * Requires: profile->ns.lock to be held
  499. */
  500. static struct aa_profile *__first_profile(struct aa_ns *root,
  501. struct aa_ns *ns)
  502. {
  503. for (; ns; ns = __next_ns(root, ns)) {
  504. if (!list_empty(&ns->base.profiles))
  505. return list_first_entry(&ns->base.profiles,
  506. struct aa_profile, base.list);
  507. }
  508. return NULL;
  509. }
  510. /**
  511. * __next_profile - step to the next profile in a profile tree
  512. * @profile: current profile in tree (NOT NULL)
  513. *
  514. * Perform a depth first traversal on the profile tree in a namespace
  515. *
  516. * Returns: next profile or NULL if done
  517. * Requires: profile->ns.lock to be held
  518. */
  519. static struct aa_profile *__next_profile(struct aa_profile *p)
  520. {
  521. struct aa_profile *parent;
  522. struct aa_ns *ns = p->ns;
  523. /* is next profile a child */
  524. if (!list_empty(&p->base.profiles))
  525. return list_first_entry(&p->base.profiles, typeof(*p),
  526. base.list);
  527. /* is next profile a sibling, parent sibling, gp, sibling, .. */
  528. parent = rcu_dereference_protected(p->parent,
  529. mutex_is_locked(&p->ns->lock));
  530. while (parent) {
  531. p = list_next_entry(p, base.list);
  532. if (!list_entry_is_head(p, &parent->base.profiles, base.list))
  533. return p;
  534. p = parent;
  535. parent = rcu_dereference_protected(parent->parent,
  536. mutex_is_locked(&parent->ns->lock));
  537. }
  538. /* is next another profile in the namespace */
  539. p = list_next_entry(p, base.list);
  540. if (!list_entry_is_head(p, &ns->base.profiles, base.list))
  541. return p;
  542. return NULL;
  543. }
  544. /**
  545. * next_profile - step to the next profile in where ever it may be
  546. * @root: root namespace (NOT NULL)
  547. * @profile: current profile (NOT NULL)
  548. *
  549. * Returns: next profile or NULL if there isn't one
  550. */
  551. static struct aa_profile *next_profile(struct aa_ns *root,
  552. struct aa_profile *profile)
  553. {
  554. struct aa_profile *next = __next_profile(profile);
  555. if (next)
  556. return next;
  557. /* finished all profiles in namespace move to next namespace */
  558. return __first_profile(root, __next_ns(root, profile->ns));
  559. }
  560. /**
  561. * p_start - start a depth first traversal of profile tree
  562. * @f: seq_file to fill
  563. * @pos: current position
  564. *
  565. * Returns: first profile under current namespace or NULL if none found
  566. *
  567. * acquires first ns->lock
  568. */
  569. static void *p_start(struct seq_file *f, loff_t *pos)
  570. {
  571. struct aa_profile *profile = NULL;
  572. struct aa_ns *root = aa_current_profile()->ns;
  573. loff_t l = *pos;
  574. f->private = aa_get_ns(root);
  575. /* find the first profile */
  576. mutex_lock(&root->lock);
  577. profile = __first_profile(root, root);
  578. /* skip to position */
  579. for (; profile && l > 0; l--)
  580. profile = next_profile(root, profile);
  581. return profile;
  582. }
  583. /**
  584. * p_next - read the next profile entry
  585. * @f: seq_file to fill
  586. * @p: profile previously returned
  587. * @pos: current position
  588. *
  589. * Returns: next profile after @p or NULL if none
  590. *
  591. * may acquire/release locks in namespace tree as necessary
  592. */
  593. static void *p_next(struct seq_file *f, void *p, loff_t *pos)
  594. {
  595. struct aa_profile *profile = p;
  596. struct aa_ns *ns = f->private;
  597. (*pos)++;
  598. return next_profile(ns, profile);
  599. }
  600. /**
  601. * p_stop - stop depth first traversal
  602. * @f: seq_file we are filling
  603. * @p: the last profile writen
  604. *
  605. * Release all locking done by p_start/p_next on namespace tree
  606. */
  607. static void p_stop(struct seq_file *f, void *p)
  608. {
  609. struct aa_profile *profile = p;
  610. struct aa_ns *root = f->private, *ns;
  611. if (profile) {
  612. for (ns = profile->ns; ns && ns != root; ns = ns->parent)
  613. mutex_unlock(&ns->lock);
  614. }
  615. mutex_unlock(&root->lock);
  616. aa_put_ns(root);
  617. }
  618. /**
  619. * seq_show_profile - show a profile entry
  620. * @f: seq_file to file
  621. * @p: current position (profile) (NOT NULL)
  622. *
  623. * Returns: error on failure
  624. */
  625. static int seq_show_profile(struct seq_file *f, void *p)
  626. {
  627. struct aa_profile *profile = (struct aa_profile *)p;
  628. struct aa_ns *root = f->private;
  629. if (profile->ns != root)
  630. seq_printf(f, ":%s://", aa_ns_name(root, profile->ns));
  631. seq_printf(f, "%s (%s)\n", profile->base.hname,
  632. aa_profile_mode_names[profile->mode]);
  633. return 0;
  634. }
  635. static const struct seq_operations aa_fs_profiles_op = {
  636. .start = p_start,
  637. .next = p_next,
  638. .stop = p_stop,
  639. .show = seq_show_profile,
  640. };
  641. static int profiles_open(struct inode *inode, struct file *file)
  642. {
  643. return seq_open(file, &aa_fs_profiles_op);
  644. }
  645. static int profiles_release(struct inode *inode, struct file *file)
  646. {
  647. return seq_release(inode, file);
  648. }
  649. static const struct file_operations aa_fs_profiles_fops = {
  650. .open = profiles_open,
  651. .read = seq_read,
  652. .llseek = seq_lseek,
  653. .release = profiles_release,
  654. };
  655. /** Base file system setup **/
  656. static struct aa_fs_entry aa_fs_entry_file[] = {
  657. AA_FS_FILE_STRING("mask", "create read write exec append mmap_exec " \
  658. "link lock"),
  659. { }
  660. };
  661. static struct aa_fs_entry aa_fs_entry_domain[] = {
  662. AA_FS_FILE_BOOLEAN("change_hat", 1),
  663. AA_FS_FILE_BOOLEAN("change_hatv", 1),
  664. AA_FS_FILE_BOOLEAN("change_onexec", 1),
  665. AA_FS_FILE_BOOLEAN("change_profile", 1),
  666. { }
  667. };
  668. static struct aa_fs_entry aa_fs_entry_policy[] = {
  669. AA_FS_FILE_BOOLEAN("set_load", 1),
  670. {}
  671. };
  672. static struct aa_fs_entry aa_fs_entry_features[] = {
  673. AA_FS_DIR("policy", aa_fs_entry_policy),
  674. AA_FS_DIR("domain", aa_fs_entry_domain),
  675. AA_FS_DIR("file", aa_fs_entry_file),
  676. AA_FS_FILE_U64("capability", VFS_CAP_FLAGS_MASK),
  677. AA_FS_DIR("rlimit", aa_fs_entry_rlimit),
  678. AA_FS_DIR("caps", aa_fs_entry_caps),
  679. { }
  680. };
  681. static struct aa_fs_entry aa_fs_entry_apparmor[] = {
  682. AA_FS_FILE_FOPS(".load", 0640, &aa_fs_profile_load),
  683. AA_FS_FILE_FOPS(".replace", 0640, &aa_fs_profile_replace),
  684. AA_FS_FILE_FOPS(".remove", 0640, &aa_fs_profile_remove),
  685. AA_FS_FILE_FOPS("profiles", 0640, &aa_fs_profiles_fops),
  686. AA_FS_DIR("features", aa_fs_entry_features),
  687. { }
  688. };
  689. static struct aa_fs_entry aa_fs_entry =
  690. AA_FS_DIR("apparmor", aa_fs_entry_apparmor);
  691. /**
  692. * aafs_create_file - create a file entry in the apparmor securityfs
  693. * @fs_file: aa_fs_entry to build an entry for (NOT NULL)
  694. * @parent: the parent dentry in the securityfs
  695. *
  696. * Use aafs_remove_file to remove entries created with this fn.
  697. */
  698. static int __init aafs_create_file(struct aa_fs_entry *fs_file,
  699. struct dentry *parent)
  700. {
  701. int error = 0;
  702. fs_file->dentry = securityfs_create_file(fs_file->name,
  703. S_IFREG | fs_file->mode,
  704. parent, fs_file,
  705. fs_file->file_ops);
  706. if (IS_ERR(fs_file->dentry)) {
  707. error = PTR_ERR(fs_file->dentry);
  708. fs_file->dentry = NULL;
  709. }
  710. return error;
  711. }
  712. static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir);
  713. /**
  714. * aafs_create_dir - recursively create a directory entry in the securityfs
  715. * @fs_dir: aa_fs_entry (and all child entries) to build (NOT NULL)
  716. * @parent: the parent dentry in the securityfs
  717. *
  718. * Use aafs_remove_dir to remove entries created with this fn.
  719. */
  720. static int __init aafs_create_dir(struct aa_fs_entry *fs_dir,
  721. struct dentry *parent)
  722. {
  723. struct aa_fs_entry *fs_file;
  724. struct dentry *dir;
  725. int error;
  726. dir = securityfs_create_dir(fs_dir->name, parent);
  727. if (IS_ERR(dir))
  728. return PTR_ERR(dir);
  729. fs_dir->dentry = dir;
  730. for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
  731. if (fs_file->v_type == AA_FS_TYPE_DIR)
  732. error = aafs_create_dir(fs_file, fs_dir->dentry);
  733. else
  734. error = aafs_create_file(fs_file, fs_dir->dentry);
  735. if (error)
  736. goto failed;
  737. }
  738. return 0;
  739. failed:
  740. aafs_remove_dir(fs_dir);
  741. return error;
  742. }
  743. /**
  744. * aafs_remove_file - drop a single file entry in the apparmor securityfs
  745. * @fs_file: aa_fs_entry to detach from the securityfs (NOT NULL)
  746. */
  747. static void __init aafs_remove_file(struct aa_fs_entry *fs_file)
  748. {
  749. if (!fs_file->dentry)
  750. return;
  751. securityfs_remove(fs_file->dentry);
  752. fs_file->dentry = NULL;
  753. }
  754. /**
  755. * aafs_remove_dir - recursively drop a directory entry from the securityfs
  756. * @fs_dir: aa_fs_entry (and all child entries) to detach (NOT NULL)
  757. */
  758. static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir)
  759. {
  760. struct aa_fs_entry *fs_file;
  761. for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
  762. if (fs_file->v_type == AA_FS_TYPE_DIR)
  763. aafs_remove_dir(fs_file);
  764. else
  765. aafs_remove_file(fs_file);
  766. }
  767. aafs_remove_file(fs_dir);
  768. }
  769. /**
  770. * aa_destroy_aafs - cleanup and free aafs
  771. *
  772. * releases dentries allocated by aa_create_aafs
  773. */
  774. void __init aa_destroy_aafs(void)
  775. {
  776. aafs_remove_dir(&aa_fs_entry);
  777. }
  778. /**
  779. * aa_create_aafs - create the apparmor security filesystem
  780. *
  781. * dentries created here are released by aa_destroy_aafs
  782. *
  783. * Returns: error on failure
  784. */
  785. static int __init aa_create_aafs(void)
  786. {
  787. int error;
  788. if (!apparmor_initialized)
  789. return 0;
  790. if (aa_fs_entry.dentry) {
  791. AA_ERROR("%s: AppArmor securityfs already exists\n", __func__);
  792. return -EEXIST;
  793. }
  794. /* Populate fs tree. */
  795. error = aafs_create_dir(&aa_fs_entry, NULL);
  796. if (error)
  797. goto error;
  798. error = __aa_fs_ns_mkdir(root_ns, aa_fs_entry.dentry, "policy");
  799. if (error)
  800. goto error;
  801. /* TODO: add support for apparmorfs_null and apparmorfs_mnt */
  802. /* Report that AppArmor fs is enabled */
  803. aa_info_message("AppArmor Filesystem Enabled");
  804. return 0;
  805. error:
  806. aa_destroy_aafs();
  807. AA_ERROR("Error creating AppArmor securityfs\n");
  808. return error;
  809. }
  810. fs_initcall(aa_create_aafs);