apparmorfs.c 22 KB

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