apparmorfs.c 38 KB

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