apparmorfs.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  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. aa_put_profile(profile);
  281. return 0;
  282. }
  283. static int aa_fs_seq_hash_open(struct inode *inode, struct file *file)
  284. {
  285. return single_open(file, aa_fs_seq_hash_show, inode->i_private);
  286. }
  287. static const struct file_operations aa_fs_seq_hash_fops = {
  288. .owner = THIS_MODULE,
  289. .open = aa_fs_seq_hash_open,
  290. .read = seq_read,
  291. .llseek = seq_lseek,
  292. .release = single_release,
  293. };
  294. /** fns to setup dynamic per profile/namespace files **/
  295. void __aa_fs_profile_rmdir(struct aa_profile *profile)
  296. {
  297. struct aa_profile *child;
  298. int i;
  299. if (!profile)
  300. return;
  301. list_for_each_entry(child, &profile->base.profiles, base.list)
  302. __aa_fs_profile_rmdir(child);
  303. for (i = AAFS_PROF_SIZEOF - 1; i >= 0; --i) {
  304. struct aa_replacedby *r;
  305. if (!profile->dents[i])
  306. continue;
  307. r = d_inode(profile->dents[i])->i_private;
  308. securityfs_remove(profile->dents[i]);
  309. aa_put_replacedby(r);
  310. profile->dents[i] = NULL;
  311. }
  312. }
  313. void __aa_fs_profile_migrate_dents(struct aa_profile *old,
  314. struct aa_profile *new)
  315. {
  316. int i;
  317. for (i = 0; i < AAFS_PROF_SIZEOF; i++) {
  318. new->dents[i] = old->dents[i];
  319. old->dents[i] = NULL;
  320. }
  321. }
  322. static struct dentry *create_profile_file(struct dentry *dir, const char *name,
  323. struct aa_profile *profile,
  324. const struct file_operations *fops)
  325. {
  326. struct aa_replacedby *r = aa_get_replacedby(profile->replacedby);
  327. struct dentry *dent;
  328. dent = securityfs_create_file(name, S_IFREG | 0444, dir, r, fops);
  329. if (IS_ERR(dent))
  330. aa_put_replacedby(r);
  331. return dent;
  332. }
  333. /* requires lock be held */
  334. int __aa_fs_profile_mkdir(struct aa_profile *profile, struct dentry *parent)
  335. {
  336. struct aa_profile *child;
  337. struct dentry *dent = NULL, *dir;
  338. int error;
  339. if (!parent) {
  340. struct aa_profile *p;
  341. p = aa_deref_parent(profile);
  342. dent = prof_dir(p);
  343. /* adding to parent that previously didn't have children */
  344. dent = securityfs_create_dir("profiles", dent);
  345. if (IS_ERR(dent))
  346. goto fail;
  347. prof_child_dir(p) = parent = dent;
  348. }
  349. if (!profile->dirname) {
  350. int len, id_len;
  351. len = mangle_name(profile->base.name, NULL);
  352. id_len = snprintf(NULL, 0, ".%ld", profile->ns->uniq_id);
  353. profile->dirname = kmalloc(len + id_len + 1, GFP_KERNEL);
  354. if (!profile->dirname)
  355. goto fail;
  356. mangle_name(profile->base.name, profile->dirname);
  357. sprintf(profile->dirname + len, ".%ld", profile->ns->uniq_id++);
  358. }
  359. dent = securityfs_create_dir(profile->dirname, parent);
  360. if (IS_ERR(dent))
  361. goto fail;
  362. prof_dir(profile) = dir = dent;
  363. dent = create_profile_file(dir, "name", profile, &aa_fs_profname_fops);
  364. if (IS_ERR(dent))
  365. goto fail;
  366. profile->dents[AAFS_PROF_NAME] = dent;
  367. dent = create_profile_file(dir, "mode", profile, &aa_fs_profmode_fops);
  368. if (IS_ERR(dent))
  369. goto fail;
  370. profile->dents[AAFS_PROF_MODE] = dent;
  371. dent = create_profile_file(dir, "attach", profile,
  372. &aa_fs_profattach_fops);
  373. if (IS_ERR(dent))
  374. goto fail;
  375. profile->dents[AAFS_PROF_ATTACH] = dent;
  376. if (profile->hash) {
  377. dent = create_profile_file(dir, "sha1", profile,
  378. &aa_fs_seq_hash_fops);
  379. if (IS_ERR(dent))
  380. goto fail;
  381. profile->dents[AAFS_PROF_HASH] = dent;
  382. }
  383. list_for_each_entry(child, &profile->base.profiles, base.list) {
  384. error = __aa_fs_profile_mkdir(child, prof_child_dir(profile));
  385. if (error)
  386. goto fail2;
  387. }
  388. return 0;
  389. fail:
  390. error = PTR_ERR(dent);
  391. fail2:
  392. __aa_fs_profile_rmdir(profile);
  393. return error;
  394. }
  395. void __aa_fs_namespace_rmdir(struct aa_namespace *ns)
  396. {
  397. struct aa_namespace *sub;
  398. struct aa_profile *child;
  399. int i;
  400. if (!ns)
  401. return;
  402. list_for_each_entry(child, &ns->base.profiles, base.list)
  403. __aa_fs_profile_rmdir(child);
  404. list_for_each_entry(sub, &ns->sub_ns, base.list) {
  405. mutex_lock(&sub->lock);
  406. __aa_fs_namespace_rmdir(sub);
  407. mutex_unlock(&sub->lock);
  408. }
  409. for (i = AAFS_NS_SIZEOF - 1; i >= 0; --i) {
  410. securityfs_remove(ns->dents[i]);
  411. ns->dents[i] = NULL;
  412. }
  413. }
  414. int __aa_fs_namespace_mkdir(struct aa_namespace *ns, struct dentry *parent,
  415. const char *name)
  416. {
  417. struct aa_namespace *sub;
  418. struct aa_profile *child;
  419. struct dentry *dent, *dir;
  420. int error;
  421. if (!name)
  422. name = ns->base.name;
  423. dent = securityfs_create_dir(name, parent);
  424. if (IS_ERR(dent))
  425. goto fail;
  426. ns_dir(ns) = dir = dent;
  427. dent = securityfs_create_dir("profiles", dir);
  428. if (IS_ERR(dent))
  429. goto fail;
  430. ns_subprofs_dir(ns) = dent;
  431. dent = securityfs_create_dir("namespaces", dir);
  432. if (IS_ERR(dent))
  433. goto fail;
  434. ns_subns_dir(ns) = dent;
  435. list_for_each_entry(child, &ns->base.profiles, base.list) {
  436. error = __aa_fs_profile_mkdir(child, ns_subprofs_dir(ns));
  437. if (error)
  438. goto fail2;
  439. }
  440. list_for_each_entry(sub, &ns->sub_ns, base.list) {
  441. mutex_lock(&sub->lock);
  442. error = __aa_fs_namespace_mkdir(sub, ns_subns_dir(ns), NULL);
  443. mutex_unlock(&sub->lock);
  444. if (error)
  445. goto fail2;
  446. }
  447. return 0;
  448. fail:
  449. error = PTR_ERR(dent);
  450. fail2:
  451. __aa_fs_namespace_rmdir(ns);
  452. return error;
  453. }
  454. #define list_entry_next(pos, member) \
  455. list_entry(pos->member.next, typeof(*pos), member)
  456. #define list_entry_is_head(pos, head, member) (&pos->member == (head))
  457. /**
  458. * __next_namespace - 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_namespace *__next_namespace(struct aa_namespace *root,
  470. struct aa_namespace *ns)
  471. {
  472. struct aa_namespace *parent, *next;
  473. /* is next namespace a child */
  474. if (!list_empty(&ns->sub_ns)) {
  475. next = list_first_entry(&ns->sub_ns, typeof(*ns), base.list);
  476. mutex_lock(&next->lock);
  477. return next;
  478. }
  479. /* check if the next ns is a sibling, parent, gp, .. */
  480. parent = ns->parent;
  481. while (ns != root) {
  482. mutex_unlock(&ns->lock);
  483. next = list_entry_next(ns, base.list);
  484. if (!list_entry_is_head(next, &parent->sub_ns, base.list)) {
  485. mutex_lock(&next->lock);
  486. return next;
  487. }
  488. ns = parent;
  489. parent = parent->parent;
  490. }
  491. return NULL;
  492. }
  493. /**
  494. * __first_profile - find the first profile in a namespace
  495. * @root: namespace that is root of profiles being displayed (NOT NULL)
  496. * @ns: namespace to start in (NOT NULL)
  497. *
  498. * Returns: unrefcounted profile or NULL if no profile
  499. * Requires: profile->ns.lock to be held
  500. */
  501. static struct aa_profile *__first_profile(struct aa_namespace *root,
  502. struct aa_namespace *ns)
  503. {
  504. for (; ns; ns = __next_namespace(root, ns)) {
  505. if (!list_empty(&ns->base.profiles))
  506. return list_first_entry(&ns->base.profiles,
  507. struct aa_profile, base.list);
  508. }
  509. return NULL;
  510. }
  511. /**
  512. * __next_profile - step to the next profile in a profile tree
  513. * @profile: current profile in tree (NOT NULL)
  514. *
  515. * Perform a depth first traversal on the profile tree in a namespace
  516. *
  517. * Returns: next profile or NULL if done
  518. * Requires: profile->ns.lock to be held
  519. */
  520. static struct aa_profile *__next_profile(struct aa_profile *p)
  521. {
  522. struct aa_profile *parent;
  523. struct aa_namespace *ns = p->ns;
  524. /* is next profile a child */
  525. if (!list_empty(&p->base.profiles))
  526. return list_first_entry(&p->base.profiles, typeof(*p),
  527. base.list);
  528. /* is next profile a sibling, parent sibling, gp, sibling, .. */
  529. parent = rcu_dereference_protected(p->parent,
  530. mutex_is_locked(&p->ns->lock));
  531. while (parent) {
  532. p = list_entry_next(p, base.list);
  533. if (!list_entry_is_head(p, &parent->base.profiles, base.list))
  534. return p;
  535. p = parent;
  536. parent = rcu_dereference_protected(parent->parent,
  537. mutex_is_locked(&parent->ns->lock));
  538. }
  539. /* is next another profile in the namespace */
  540. p = list_entry_next(p, base.list);
  541. if (!list_entry_is_head(p, &ns->base.profiles, base.list))
  542. return p;
  543. return NULL;
  544. }
  545. /**
  546. * next_profile - step to the next profile in where ever it may be
  547. * @root: root namespace (NOT NULL)
  548. * @profile: current profile (NOT NULL)
  549. *
  550. * Returns: next profile or NULL if there isn't one
  551. */
  552. static struct aa_profile *next_profile(struct aa_namespace *root,
  553. struct aa_profile *profile)
  554. {
  555. struct aa_profile *next = __next_profile(profile);
  556. if (next)
  557. return next;
  558. /* finished all profiles in namespace move to next namespace */
  559. return __first_profile(root, __next_namespace(root, profile->ns));
  560. }
  561. /**
  562. * p_start - start a depth first traversal of profile tree
  563. * @f: seq_file to fill
  564. * @pos: current position
  565. *
  566. * Returns: first profile under current namespace or NULL if none found
  567. *
  568. * acquires first ns->lock
  569. */
  570. static void *p_start(struct seq_file *f, loff_t *pos)
  571. {
  572. struct aa_profile *profile = NULL;
  573. struct aa_namespace *root = aa_current_profile()->ns;
  574. loff_t l = *pos;
  575. f->private = aa_get_namespace(root);
  576. /* find the first profile */
  577. mutex_lock(&root->lock);
  578. profile = __first_profile(root, root);
  579. /* skip to position */
  580. for (; profile && l > 0; l--)
  581. profile = next_profile(root, profile);
  582. return profile;
  583. }
  584. /**
  585. * p_next - read the next profile entry
  586. * @f: seq_file to fill
  587. * @p: profile previously returned
  588. * @pos: current position
  589. *
  590. * Returns: next profile after @p or NULL if none
  591. *
  592. * may acquire/release locks in namespace tree as necessary
  593. */
  594. static void *p_next(struct seq_file *f, void *p, loff_t *pos)
  595. {
  596. struct aa_profile *profile = p;
  597. struct aa_namespace *ns = f->private;
  598. (*pos)++;
  599. return next_profile(ns, profile);
  600. }
  601. /**
  602. * p_stop - stop depth first traversal
  603. * @f: seq_file we are filling
  604. * @p: the last profile writen
  605. *
  606. * Release all locking done by p_start/p_next on namespace tree
  607. */
  608. static void p_stop(struct seq_file *f, void *p)
  609. {
  610. struct aa_profile *profile = p;
  611. struct aa_namespace *root = f->private, *ns;
  612. if (profile) {
  613. for (ns = profile->ns; ns && ns != root; ns = ns->parent)
  614. mutex_unlock(&ns->lock);
  615. }
  616. mutex_unlock(&root->lock);
  617. aa_put_namespace(root);
  618. }
  619. /**
  620. * seq_show_profile - show a profile entry
  621. * @f: seq_file to file
  622. * @p: current position (profile) (NOT NULL)
  623. *
  624. * Returns: error on failure
  625. */
  626. static int seq_show_profile(struct seq_file *f, void *p)
  627. {
  628. struct aa_profile *profile = (struct aa_profile *)p;
  629. struct aa_namespace *root = f->private;
  630. if (profile->ns != root)
  631. seq_printf(f, ":%s://", aa_ns_name(root, profile->ns));
  632. seq_printf(f, "%s (%s)\n", profile->base.hname,
  633. aa_profile_mode_names[profile->mode]);
  634. return 0;
  635. }
  636. static const struct seq_operations aa_fs_profiles_op = {
  637. .start = p_start,
  638. .next = p_next,
  639. .stop = p_stop,
  640. .show = seq_show_profile,
  641. };
  642. static int profiles_open(struct inode *inode, struct file *file)
  643. {
  644. return seq_open(file, &aa_fs_profiles_op);
  645. }
  646. static int profiles_release(struct inode *inode, struct file *file)
  647. {
  648. return seq_release(inode, file);
  649. }
  650. static const struct file_operations aa_fs_profiles_fops = {
  651. .open = profiles_open,
  652. .read = seq_read,
  653. .llseek = seq_lseek,
  654. .release = profiles_release,
  655. };
  656. /** Base file system setup **/
  657. static struct aa_fs_entry aa_fs_entry_file[] = {
  658. AA_FS_FILE_STRING("mask", "create read write exec append mmap_exec " \
  659. "link lock"),
  660. { }
  661. };
  662. static struct aa_fs_entry aa_fs_entry_domain[] = {
  663. AA_FS_FILE_BOOLEAN("change_hat", 1),
  664. AA_FS_FILE_BOOLEAN("change_hatv", 1),
  665. AA_FS_FILE_BOOLEAN("change_onexec", 1),
  666. AA_FS_FILE_BOOLEAN("change_profile", 1),
  667. { }
  668. };
  669. static struct aa_fs_entry aa_fs_entry_policy[] = {
  670. AA_FS_FILE_BOOLEAN("set_load", 1),
  671. {}
  672. };
  673. static struct aa_fs_entry aa_fs_entry_features[] = {
  674. AA_FS_DIR("policy", aa_fs_entry_policy),
  675. AA_FS_DIR("domain", aa_fs_entry_domain),
  676. AA_FS_DIR("file", aa_fs_entry_file),
  677. AA_FS_FILE_U64("capability", VFS_CAP_FLAGS_MASK),
  678. AA_FS_DIR("rlimit", aa_fs_entry_rlimit),
  679. AA_FS_DIR("caps", aa_fs_entry_caps),
  680. { }
  681. };
  682. static struct aa_fs_entry aa_fs_entry_apparmor[] = {
  683. AA_FS_FILE_FOPS(".load", 0640, &aa_fs_profile_load),
  684. AA_FS_FILE_FOPS(".replace", 0640, &aa_fs_profile_replace),
  685. AA_FS_FILE_FOPS(".remove", 0640, &aa_fs_profile_remove),
  686. AA_FS_FILE_FOPS("profiles", 0640, &aa_fs_profiles_fops),
  687. AA_FS_DIR("features", aa_fs_entry_features),
  688. { }
  689. };
  690. static struct aa_fs_entry aa_fs_entry =
  691. AA_FS_DIR("apparmor", aa_fs_entry_apparmor);
  692. /**
  693. * aafs_create_file - create a file entry in the apparmor securityfs
  694. * @fs_file: aa_fs_entry to build an entry for (NOT NULL)
  695. * @parent: the parent dentry in the securityfs
  696. *
  697. * Use aafs_remove_file to remove entries created with this fn.
  698. */
  699. static int __init aafs_create_file(struct aa_fs_entry *fs_file,
  700. struct dentry *parent)
  701. {
  702. int error = 0;
  703. fs_file->dentry = securityfs_create_file(fs_file->name,
  704. S_IFREG | fs_file->mode,
  705. parent, fs_file,
  706. fs_file->file_ops);
  707. if (IS_ERR(fs_file->dentry)) {
  708. error = PTR_ERR(fs_file->dentry);
  709. fs_file->dentry = NULL;
  710. }
  711. return error;
  712. }
  713. static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir);
  714. /**
  715. * aafs_create_dir - recursively create a directory entry in the securityfs
  716. * @fs_dir: aa_fs_entry (and all child entries) to build (NOT NULL)
  717. * @parent: the parent dentry in the securityfs
  718. *
  719. * Use aafs_remove_dir to remove entries created with this fn.
  720. */
  721. static int __init aafs_create_dir(struct aa_fs_entry *fs_dir,
  722. struct dentry *parent)
  723. {
  724. struct aa_fs_entry *fs_file;
  725. struct dentry *dir;
  726. int error;
  727. dir = securityfs_create_dir(fs_dir->name, parent);
  728. if (IS_ERR(dir))
  729. return PTR_ERR(dir);
  730. fs_dir->dentry = dir;
  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. error = aafs_create_dir(fs_file, fs_dir->dentry);
  734. else
  735. error = aafs_create_file(fs_file, fs_dir->dentry);
  736. if (error)
  737. goto failed;
  738. }
  739. return 0;
  740. failed:
  741. aafs_remove_dir(fs_dir);
  742. return error;
  743. }
  744. /**
  745. * aafs_remove_file - drop a single file entry in the apparmor securityfs
  746. * @fs_file: aa_fs_entry to detach from the securityfs (NOT NULL)
  747. */
  748. static void __init aafs_remove_file(struct aa_fs_entry *fs_file)
  749. {
  750. if (!fs_file->dentry)
  751. return;
  752. securityfs_remove(fs_file->dentry);
  753. fs_file->dentry = NULL;
  754. }
  755. /**
  756. * aafs_remove_dir - recursively drop a directory entry from the securityfs
  757. * @fs_dir: aa_fs_entry (and all child entries) to detach (NOT NULL)
  758. */
  759. static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir)
  760. {
  761. struct aa_fs_entry *fs_file;
  762. for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
  763. if (fs_file->v_type == AA_FS_TYPE_DIR)
  764. aafs_remove_dir(fs_file);
  765. else
  766. aafs_remove_file(fs_file);
  767. }
  768. aafs_remove_file(fs_dir);
  769. }
  770. /**
  771. * aa_destroy_aafs - cleanup and free aafs
  772. *
  773. * releases dentries allocated by aa_create_aafs
  774. */
  775. void __init aa_destroy_aafs(void)
  776. {
  777. aafs_remove_dir(&aa_fs_entry);
  778. }
  779. /**
  780. * aa_create_aafs - create the apparmor security filesystem
  781. *
  782. * dentries created here are released by aa_destroy_aafs
  783. *
  784. * Returns: error on failure
  785. */
  786. static int __init aa_create_aafs(void)
  787. {
  788. int error;
  789. if (!apparmor_initialized)
  790. return 0;
  791. if (aa_fs_entry.dentry) {
  792. AA_ERROR("%s: AppArmor securityfs already exists\n", __func__);
  793. return -EEXIST;
  794. }
  795. /* Populate fs tree. */
  796. error = aafs_create_dir(&aa_fs_entry, NULL);
  797. if (error)
  798. goto error;
  799. error = __aa_fs_namespace_mkdir(root_ns, aa_fs_entry.dentry,
  800. "policy");
  801. if (error)
  802. goto error;
  803. /* TODO: add support for apparmorfs_null and apparmorfs_mnt */
  804. /* Report that AppArmor fs is enabled */
  805. aa_info_message("AppArmor Filesystem Enabled");
  806. return 0;
  807. error:
  808. aa_destroy_aafs();
  809. AA_ERROR("Error creating AppArmor securityfs\n");
  810. return error;
  811. }
  812. fs_initcall(aa_create_aafs);