apparmorfs.c 23 KB

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