apparmorfs.c 35 KB

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