apparmorfs.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AppArmor security module
  4. *
  5. * This file contains AppArmor /sys/kernel/security/apparmor interface functions
  6. *
  7. * Copyright (C) 1998-2008 Novell/SUSE
  8. * Copyright 2009-2010 Canonical Ltd.
  9. */
  10. #include <linux/ctype.h>
  11. #include <linux/security.h>
  12. #include <linux/vmalloc.h>
  13. #include <linux/init.h>
  14. #include <linux/seq_file.h>
  15. #include <linux/uaccess.h>
  16. #include <linux/mount.h>
  17. #include <linux/namei.h>
  18. #include <linux/capability.h>
  19. #include <linux/rcupdate.h>
  20. #include <linux/fs.h>
  21. #include <linux/fs_context.h>
  22. #include <linux/poll.h>
  23. #include <linux/zlib.h>
  24. #include <uapi/linux/major.h>
  25. #include <uapi/linux/magic.h>
  26. #include "include/apparmor.h"
  27. #include "include/apparmorfs.h"
  28. #include "include/audit.h"
  29. #include "include/cred.h"
  30. #include "include/crypto.h"
  31. #include "include/ipc.h"
  32. #include "include/label.h"
  33. #include "include/policy.h"
  34. #include "include/policy_ns.h"
  35. #include "include/resource.h"
  36. #include "include/policy_unpack.h"
  37. #include "include/task.h"
  38. /*
  39. * The apparmor filesystem interface used for policy load and introspection
  40. * The interface is split into two main components based on their function
  41. * a securityfs component:
  42. * used for static files that are always available, and which allows
  43. * userspace to specificy the location of the security filesystem.
  44. *
  45. * fns and data are prefixed with
  46. * aa_sfs_
  47. *
  48. * an apparmorfs component:
  49. * used loaded policy content and introspection. It is not part of a
  50. * regular mounted filesystem and is available only through the magic
  51. * policy symlink in the root of the securityfs apparmor/ directory.
  52. * Tasks queries will be magically redirected to the correct portion
  53. * of the policy tree based on their confinement.
  54. *
  55. * fns and data are prefixed with
  56. * aafs_
  57. *
  58. * The aa_fs_ prefix is used to indicate the fn is used by both the
  59. * securityfs and apparmorfs filesystems.
  60. */
  61. /*
  62. * support fns
  63. */
  64. struct rawdata_f_data {
  65. struct aa_loaddata *loaddata;
  66. };
  67. #ifdef CONFIG_SECURITY_APPARMOR_EXPORT_BINARY
  68. #define RAWDATA_F_DATA_BUF(p) (char *)(p + 1)
  69. static void rawdata_f_data_free(struct rawdata_f_data *private)
  70. {
  71. if (!private)
  72. return;
  73. aa_put_loaddata(private->loaddata);
  74. kvfree(private);
  75. }
  76. static struct rawdata_f_data *rawdata_f_data_alloc(size_t size)
  77. {
  78. struct rawdata_f_data *ret;
  79. if (size > SIZE_MAX - sizeof(*ret))
  80. return ERR_PTR(-EINVAL);
  81. ret = kvzalloc(sizeof(*ret) + size, GFP_KERNEL);
  82. if (!ret)
  83. return ERR_PTR(-ENOMEM);
  84. return ret;
  85. }
  86. #endif
  87. /**
  88. * mangle_name - mangle a profile name to std profile layout form
  89. * @name: profile name to mangle (NOT NULL)
  90. * @target: buffer to store mangled name, same length as @name (MAYBE NULL)
  91. *
  92. * Returns: length of mangled name
  93. */
  94. static int mangle_name(const char *name, char *target)
  95. {
  96. char *t = target;
  97. while (*name == '/' || *name == '.')
  98. name++;
  99. if (target) {
  100. for (; *name; name++) {
  101. if (*name == '/')
  102. *(t)++ = '.';
  103. else if (isspace(*name))
  104. *(t)++ = '_';
  105. else if (isalnum(*name) || strchr("._-", *name))
  106. *(t)++ = *name;
  107. }
  108. *t = 0;
  109. } else {
  110. int len = 0;
  111. for (; *name; name++) {
  112. if (isalnum(*name) || isspace(*name) ||
  113. strchr("/._-", *name))
  114. len++;
  115. }
  116. return len;
  117. }
  118. return t - target;
  119. }
  120. /*
  121. * aafs - core fns and data for the policy tree
  122. */
  123. #define AAFS_NAME "apparmorfs"
  124. static struct vfsmount *aafs_mnt;
  125. static int aafs_count;
  126. static int aafs_show_path(struct seq_file *seq, struct dentry *dentry)
  127. {
  128. seq_printf(seq, "%s:[%lu]", AAFS_NAME, d_inode(dentry)->i_ino);
  129. return 0;
  130. }
  131. static void aafs_free_inode(struct inode *inode)
  132. {
  133. if (S_ISLNK(inode->i_mode))
  134. kfree(inode->i_link);
  135. free_inode_nonrcu(inode);
  136. }
  137. static const struct super_operations aafs_super_ops = {
  138. .statfs = simple_statfs,
  139. .free_inode = aafs_free_inode,
  140. .show_path = aafs_show_path,
  141. };
  142. static int apparmorfs_fill_super(struct super_block *sb, struct fs_context *fc)
  143. {
  144. static struct tree_descr files[] = { {""} };
  145. int error;
  146. error = simple_fill_super(sb, AAFS_MAGIC, files);
  147. if (error)
  148. return error;
  149. sb->s_op = &aafs_super_ops;
  150. return 0;
  151. }
  152. static int apparmorfs_get_tree(struct fs_context *fc)
  153. {
  154. return get_tree_single(fc, apparmorfs_fill_super);
  155. }
  156. static const struct fs_context_operations apparmorfs_context_ops = {
  157. .get_tree = apparmorfs_get_tree,
  158. };
  159. static int apparmorfs_init_fs_context(struct fs_context *fc)
  160. {
  161. fc->ops = &apparmorfs_context_ops;
  162. return 0;
  163. }
  164. static struct file_system_type aafs_ops = {
  165. .owner = THIS_MODULE,
  166. .name = AAFS_NAME,
  167. .init_fs_context = apparmorfs_init_fs_context,
  168. .kill_sb = kill_anon_super,
  169. };
  170. /**
  171. * __aafs_setup_d_inode - basic inode setup for apparmorfs
  172. * @dir: parent directory for the dentry
  173. * @dentry: dentry we are seting the inode up for
  174. * @mode: permissions the file should have
  175. * @data: data to store on inode.i_private, available in open()
  176. * @link: if symlink, symlink target string
  177. * @fops: struct file_operations that should be used
  178. * @iops: struct of inode_operations that should be used
  179. */
  180. static int __aafs_setup_d_inode(struct inode *dir, struct dentry *dentry,
  181. umode_t mode, void *data, char *link,
  182. const struct file_operations *fops,
  183. const struct inode_operations *iops)
  184. {
  185. struct inode *inode = new_inode(dir->i_sb);
  186. AA_BUG(!dir);
  187. AA_BUG(!dentry);
  188. if (!inode)
  189. return -ENOMEM;
  190. inode->i_ino = get_next_ino();
  191. inode->i_mode = mode;
  192. inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
  193. inode->i_private = data;
  194. if (S_ISDIR(mode)) {
  195. inode->i_op = iops ? iops : &simple_dir_inode_operations;
  196. inode->i_fop = &simple_dir_operations;
  197. inc_nlink(inode);
  198. inc_nlink(dir);
  199. } else if (S_ISLNK(mode)) {
  200. inode->i_op = iops ? iops : &simple_symlink_inode_operations;
  201. inode->i_link = link;
  202. } else {
  203. inode->i_fop = fops;
  204. }
  205. d_instantiate(dentry, inode);
  206. dget(dentry);
  207. return 0;
  208. }
  209. /**
  210. * aafs_create - create a dentry in the apparmorfs filesystem
  211. *
  212. * @name: name of dentry to create
  213. * @mode: permissions the file should have
  214. * @parent: parent directory for this dentry
  215. * @data: data to store on inode.i_private, available in open()
  216. * @link: if symlink, symlink target string
  217. * @fops: struct file_operations that should be used for
  218. * @iops: struct of inode_operations that should be used
  219. *
  220. * This is the basic "create a xxx" function for apparmorfs.
  221. *
  222. * Returns a pointer to a dentry if it succeeds, that must be free with
  223. * aafs_remove(). Will return ERR_PTR on failure.
  224. */
  225. static struct dentry *aafs_create(const char *name, umode_t mode,
  226. struct dentry *parent, void *data, void *link,
  227. const struct file_operations *fops,
  228. const struct inode_operations *iops)
  229. {
  230. struct dentry *dentry;
  231. struct inode *dir;
  232. int error;
  233. AA_BUG(!name);
  234. AA_BUG(!parent);
  235. if (!(mode & S_IFMT))
  236. mode = (mode & S_IALLUGO) | S_IFREG;
  237. error = simple_pin_fs(&aafs_ops, &aafs_mnt, &aafs_count);
  238. if (error)
  239. return ERR_PTR(error);
  240. dir = d_inode(parent);
  241. inode_lock(dir);
  242. dentry = lookup_one_len(name, parent, strlen(name));
  243. if (IS_ERR(dentry)) {
  244. error = PTR_ERR(dentry);
  245. goto fail_lock;
  246. }
  247. if (d_really_is_positive(dentry)) {
  248. error = -EEXIST;
  249. goto fail_dentry;
  250. }
  251. error = __aafs_setup_d_inode(dir, dentry, mode, data, link, fops, iops);
  252. if (error)
  253. goto fail_dentry;
  254. inode_unlock(dir);
  255. return dentry;
  256. fail_dentry:
  257. dput(dentry);
  258. fail_lock:
  259. inode_unlock(dir);
  260. simple_release_fs(&aafs_mnt, &aafs_count);
  261. return ERR_PTR(error);
  262. }
  263. /**
  264. * aafs_create_file - create a file in the apparmorfs filesystem
  265. *
  266. * @name: name of dentry to create
  267. * @mode: permissions the file should have
  268. * @parent: parent directory for this dentry
  269. * @data: data to store on inode.i_private, available in open()
  270. * @fops: struct file_operations that should be used for
  271. *
  272. * see aafs_create
  273. */
  274. static struct dentry *aafs_create_file(const char *name, umode_t mode,
  275. struct dentry *parent, void *data,
  276. const struct file_operations *fops)
  277. {
  278. return aafs_create(name, mode, parent, data, NULL, fops, NULL);
  279. }
  280. /**
  281. * aafs_create_dir - create a directory in the apparmorfs filesystem
  282. *
  283. * @name: name of dentry to create
  284. * @parent: parent directory for this dentry
  285. *
  286. * see aafs_create
  287. */
  288. static struct dentry *aafs_create_dir(const char *name, struct dentry *parent)
  289. {
  290. return aafs_create(name, S_IFDIR | 0755, parent, NULL, NULL, NULL,
  291. NULL);
  292. }
  293. /**
  294. * aafs_remove - removes a file or directory from the apparmorfs filesystem
  295. *
  296. * @dentry: dentry of the file/directory/symlink to removed.
  297. */
  298. static void aafs_remove(struct dentry *dentry)
  299. {
  300. struct inode *dir;
  301. if (!dentry || IS_ERR(dentry))
  302. return;
  303. dir = d_inode(dentry->d_parent);
  304. inode_lock(dir);
  305. if (simple_positive(dentry)) {
  306. if (d_is_dir(dentry))
  307. simple_rmdir(dir, dentry);
  308. else
  309. simple_unlink(dir, dentry);
  310. d_delete(dentry);
  311. dput(dentry);
  312. }
  313. inode_unlock(dir);
  314. simple_release_fs(&aafs_mnt, &aafs_count);
  315. }
  316. /*
  317. * aa_fs - policy load/replace/remove
  318. */
  319. /**
  320. * aa_simple_write_to_buffer - common routine for getting policy from user
  321. * @userbuf: user buffer to copy data from (NOT NULL)
  322. * @alloc_size: size of user buffer (REQUIRES: @alloc_size >= @copy_size)
  323. * @copy_size: size of data to copy from user buffer
  324. * @pos: position write is at in the file (NOT NULL)
  325. *
  326. * Returns: kernel buffer containing copy of user buffer data or an
  327. * ERR_PTR on failure.
  328. */
  329. static struct aa_loaddata *aa_simple_write_to_buffer(const char __user *userbuf,
  330. size_t alloc_size,
  331. size_t copy_size,
  332. loff_t *pos)
  333. {
  334. struct aa_loaddata *data;
  335. AA_BUG(copy_size > alloc_size);
  336. if (*pos != 0)
  337. /* only writes from pos 0, that is complete writes */
  338. return ERR_PTR(-ESPIPE);
  339. /* freed by caller to simple_write_to_buffer */
  340. data = aa_loaddata_alloc(alloc_size);
  341. if (IS_ERR(data))
  342. return data;
  343. data->size = copy_size;
  344. if (copy_from_user(data->data, userbuf, copy_size)) {
  345. aa_put_loaddata(data);
  346. return ERR_PTR(-EFAULT);
  347. }
  348. return data;
  349. }
  350. static ssize_t policy_update(u32 mask, const char __user *buf, size_t size,
  351. loff_t *pos, struct aa_ns *ns)
  352. {
  353. struct aa_loaddata *data;
  354. struct aa_label *label;
  355. ssize_t error;
  356. label = begin_current_label_crit_section();
  357. /* high level check about policy management - fine grained in
  358. * below after unpack
  359. */
  360. error = aa_may_manage_policy(label, ns, mask);
  361. if (error)
  362. goto end_section;
  363. data = aa_simple_write_to_buffer(buf, size, size, pos);
  364. error = PTR_ERR(data);
  365. if (!IS_ERR(data)) {
  366. error = aa_replace_profiles(ns, label, mask, data);
  367. aa_put_loaddata(data);
  368. }
  369. end_section:
  370. end_current_label_crit_section(label);
  371. return error;
  372. }
  373. /* .load file hook fn to load policy */
  374. static ssize_t profile_load(struct file *f, const char __user *buf, size_t size,
  375. loff_t *pos)
  376. {
  377. struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
  378. int error = policy_update(AA_MAY_LOAD_POLICY, buf, size, pos, ns);
  379. aa_put_ns(ns);
  380. return error;
  381. }
  382. static const struct file_operations aa_fs_profile_load = {
  383. .write = profile_load,
  384. .llseek = default_llseek,
  385. };
  386. /* .replace file hook fn to load and/or replace policy */
  387. static ssize_t profile_replace(struct file *f, const char __user *buf,
  388. size_t size, loff_t *pos)
  389. {
  390. struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
  391. int error = policy_update(AA_MAY_LOAD_POLICY | AA_MAY_REPLACE_POLICY,
  392. buf, size, pos, ns);
  393. aa_put_ns(ns);
  394. return error;
  395. }
  396. static const struct file_operations aa_fs_profile_replace = {
  397. .write = profile_replace,
  398. .llseek = default_llseek,
  399. };
  400. /* .remove file hook fn to remove loaded policy */
  401. static ssize_t profile_remove(struct file *f, const char __user *buf,
  402. size_t size, loff_t *pos)
  403. {
  404. struct aa_loaddata *data;
  405. struct aa_label *label;
  406. ssize_t error;
  407. struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
  408. label = begin_current_label_crit_section();
  409. /* high level check about policy management - fine grained in
  410. * below after unpack
  411. */
  412. error = aa_may_manage_policy(label, ns, AA_MAY_REMOVE_POLICY);
  413. if (error)
  414. goto out;
  415. /*
  416. * aa_remove_profile needs a null terminated string so 1 extra
  417. * byte is allocated and the copied data is null terminated.
  418. */
  419. data = aa_simple_write_to_buffer(buf, size + 1, size, pos);
  420. error = PTR_ERR(data);
  421. if (!IS_ERR(data)) {
  422. data->data[size] = 0;
  423. error = aa_remove_profiles(ns, label, data->data, size);
  424. aa_put_loaddata(data);
  425. }
  426. out:
  427. end_current_label_crit_section(label);
  428. aa_put_ns(ns);
  429. return error;
  430. }
  431. static const struct file_operations aa_fs_profile_remove = {
  432. .write = profile_remove,
  433. .llseek = default_llseek,
  434. };
  435. struct aa_revision {
  436. struct aa_ns *ns;
  437. long last_read;
  438. };
  439. /* revision file hook fn for policy loads */
  440. static int ns_revision_release(struct inode *inode, struct file *file)
  441. {
  442. struct aa_revision *rev = file->private_data;
  443. if (rev) {
  444. aa_put_ns(rev->ns);
  445. kfree(rev);
  446. }
  447. return 0;
  448. }
  449. static ssize_t ns_revision_read(struct file *file, char __user *buf,
  450. size_t size, loff_t *ppos)
  451. {
  452. struct aa_revision *rev = file->private_data;
  453. char buffer[32];
  454. long last_read;
  455. int avail;
  456. mutex_lock_nested(&rev->ns->lock, rev->ns->level);
  457. last_read = rev->last_read;
  458. if (last_read == rev->ns->revision) {
  459. mutex_unlock(&rev->ns->lock);
  460. if (file->f_flags & O_NONBLOCK)
  461. return -EAGAIN;
  462. if (wait_event_interruptible(rev->ns->wait,
  463. last_read !=
  464. READ_ONCE(rev->ns->revision)))
  465. return -ERESTARTSYS;
  466. mutex_lock_nested(&rev->ns->lock, rev->ns->level);
  467. }
  468. avail = sprintf(buffer, "%ld\n", rev->ns->revision);
  469. if (*ppos + size > avail) {
  470. rev->last_read = rev->ns->revision;
  471. *ppos = 0;
  472. }
  473. mutex_unlock(&rev->ns->lock);
  474. return simple_read_from_buffer(buf, size, ppos, buffer, avail);
  475. }
  476. static int ns_revision_open(struct inode *inode, struct file *file)
  477. {
  478. struct aa_revision *rev = kzalloc(sizeof(*rev), GFP_KERNEL);
  479. if (!rev)
  480. return -ENOMEM;
  481. rev->ns = aa_get_ns(inode->i_private);
  482. if (!rev->ns)
  483. rev->ns = aa_get_current_ns();
  484. file->private_data = rev;
  485. return 0;
  486. }
  487. static __poll_t ns_revision_poll(struct file *file, poll_table *pt)
  488. {
  489. struct aa_revision *rev = file->private_data;
  490. __poll_t mask = 0;
  491. if (rev) {
  492. mutex_lock_nested(&rev->ns->lock, rev->ns->level);
  493. poll_wait(file, &rev->ns->wait, pt);
  494. if (rev->last_read < rev->ns->revision)
  495. mask |= EPOLLIN | EPOLLRDNORM;
  496. mutex_unlock(&rev->ns->lock);
  497. }
  498. return mask;
  499. }
  500. void __aa_bump_ns_revision(struct aa_ns *ns)
  501. {
  502. WRITE_ONCE(ns->revision, READ_ONCE(ns->revision) + 1);
  503. wake_up_interruptible(&ns->wait);
  504. }
  505. static const struct file_operations aa_fs_ns_revision_fops = {
  506. .owner = THIS_MODULE,
  507. .open = ns_revision_open,
  508. .poll = ns_revision_poll,
  509. .read = ns_revision_read,
  510. .llseek = generic_file_llseek,
  511. .release = ns_revision_release,
  512. };
  513. static void profile_query_cb(struct aa_profile *profile, struct aa_perms *perms,
  514. const char *match_str, size_t match_len)
  515. {
  516. struct aa_perms tmp = { };
  517. struct aa_dfa *dfa;
  518. unsigned int state = 0;
  519. if (profile_unconfined(profile))
  520. return;
  521. if (profile->file.dfa && *match_str == AA_CLASS_FILE) {
  522. dfa = profile->file.dfa;
  523. state = aa_dfa_match_len(dfa, profile->file.start,
  524. match_str + 1, match_len - 1);
  525. if (state) {
  526. struct path_cond cond = { };
  527. tmp = aa_compute_fperms(dfa, state, &cond);
  528. }
  529. } else if (profile->policy.dfa) {
  530. if (!PROFILE_MEDIATES(profile, *match_str))
  531. return; /* no change to current perms */
  532. dfa = profile->policy.dfa;
  533. state = aa_dfa_match_len(dfa, profile->policy.start[0],
  534. match_str, match_len);
  535. if (state)
  536. aa_compute_perms(dfa, state, &tmp);
  537. }
  538. aa_apply_modes_to_perms(profile, &tmp);
  539. aa_perms_accum_raw(perms, &tmp);
  540. }
  541. /**
  542. * query_data - queries a policy and writes its data to buf
  543. * @buf: the resulting data is stored here (NOT NULL)
  544. * @buf_len: size of buf
  545. * @query: query string used to retrieve data
  546. * @query_len: size of query including second NUL byte
  547. *
  548. * The buffers pointed to by buf and query may overlap. The query buffer is
  549. * parsed before buf is written to.
  550. *
  551. * The query should look like "<LABEL>\0<KEY>\0", where <LABEL> is the name of
  552. * the security confinement context and <KEY> is the name of the data to
  553. * retrieve. <LABEL> and <KEY> must not be NUL-terminated.
  554. *
  555. * Don't expect the contents of buf to be preserved on failure.
  556. *
  557. * Returns: number of characters written to buf or -errno on failure
  558. */
  559. static ssize_t query_data(char *buf, size_t buf_len,
  560. char *query, size_t query_len)
  561. {
  562. char *out;
  563. const char *key;
  564. struct label_it i;
  565. struct aa_label *label, *curr;
  566. struct aa_profile *profile;
  567. struct aa_data *data;
  568. u32 bytes, blocks;
  569. __le32 outle32;
  570. if (!query_len)
  571. return -EINVAL; /* need a query */
  572. key = query + strnlen(query, query_len) + 1;
  573. if (key + 1 >= query + query_len)
  574. return -EINVAL; /* not enough space for a non-empty key */
  575. if (key + strnlen(key, query + query_len - key) >= query + query_len)
  576. return -EINVAL; /* must end with NUL */
  577. if (buf_len < sizeof(bytes) + sizeof(blocks))
  578. return -EINVAL; /* not enough space */
  579. curr = begin_current_label_crit_section();
  580. label = aa_label_parse(curr, query, GFP_KERNEL, false, false);
  581. end_current_label_crit_section(curr);
  582. if (IS_ERR(label))
  583. return PTR_ERR(label);
  584. /* We are going to leave space for two numbers. The first is the total
  585. * number of bytes we are writing after the first number. This is so
  586. * users can read the full output without reallocation.
  587. *
  588. * The second number is the number of data blocks we're writing. An
  589. * application might be confined by multiple policies having data in
  590. * the same key.
  591. */
  592. memset(buf, 0, sizeof(bytes) + sizeof(blocks));
  593. out = buf + sizeof(bytes) + sizeof(blocks);
  594. blocks = 0;
  595. label_for_each_confined(i, label, profile) {
  596. if (!profile->data)
  597. continue;
  598. data = rhashtable_lookup_fast(profile->data, &key,
  599. profile->data->p);
  600. if (data) {
  601. if (out + sizeof(outle32) + data->size > buf +
  602. buf_len) {
  603. aa_put_label(label);
  604. return -EINVAL; /* not enough space */
  605. }
  606. outle32 = __cpu_to_le32(data->size);
  607. memcpy(out, &outle32, sizeof(outle32));
  608. out += sizeof(outle32);
  609. memcpy(out, data->data, data->size);
  610. out += data->size;
  611. blocks++;
  612. }
  613. }
  614. aa_put_label(label);
  615. outle32 = __cpu_to_le32(out - buf - sizeof(bytes));
  616. memcpy(buf, &outle32, sizeof(outle32));
  617. outle32 = __cpu_to_le32(blocks);
  618. memcpy(buf + sizeof(bytes), &outle32, sizeof(outle32));
  619. return out - buf;
  620. }
  621. /**
  622. * query_label - queries a label and writes permissions to buf
  623. * @buf: the resulting permissions string is stored here (NOT NULL)
  624. * @buf_len: size of buf
  625. * @query: binary query string to match against the dfa
  626. * @query_len: size of query
  627. * @view_only: only compute for querier's view
  628. *
  629. * The buffers pointed to by buf and query may overlap. The query buffer is
  630. * parsed before buf is written to.
  631. *
  632. * The query should look like "LABEL_NAME\0DFA_STRING" where LABEL_NAME is
  633. * the name of the label, in the current namespace, that is to be queried and
  634. * DFA_STRING is a binary string to match against the label(s)'s DFA.
  635. *
  636. * LABEL_NAME must be NUL terminated. DFA_STRING may contain NUL characters
  637. * but must *not* be NUL terminated.
  638. *
  639. * Returns: number of characters written to buf or -errno on failure
  640. */
  641. static ssize_t query_label(char *buf, size_t buf_len,
  642. char *query, size_t query_len, bool view_only)
  643. {
  644. struct aa_profile *profile;
  645. struct aa_label *label, *curr;
  646. char *label_name, *match_str;
  647. size_t label_name_len, match_len;
  648. struct aa_perms perms;
  649. struct label_it i;
  650. if (!query_len)
  651. return -EINVAL;
  652. label_name = query;
  653. label_name_len = strnlen(query, query_len);
  654. if (!label_name_len || label_name_len == query_len)
  655. return -EINVAL;
  656. /**
  657. * The extra byte is to account for the null byte between the
  658. * profile name and dfa string. profile_name_len is greater
  659. * than zero and less than query_len, so a byte can be safely
  660. * added or subtracted.
  661. */
  662. match_str = label_name + label_name_len + 1;
  663. match_len = query_len - label_name_len - 1;
  664. curr = begin_current_label_crit_section();
  665. label = aa_label_parse(curr, label_name, GFP_KERNEL, false, false);
  666. end_current_label_crit_section(curr);
  667. if (IS_ERR(label))
  668. return PTR_ERR(label);
  669. perms = allperms;
  670. if (view_only) {
  671. label_for_each_in_ns(i, labels_ns(label), label, profile) {
  672. profile_query_cb(profile, &perms, match_str, match_len);
  673. }
  674. } else {
  675. label_for_each(i, label, profile) {
  676. profile_query_cb(profile, &perms, match_str, match_len);
  677. }
  678. }
  679. aa_put_label(label);
  680. return scnprintf(buf, buf_len,
  681. "allow 0x%08x\ndeny 0x%08x\naudit 0x%08x\nquiet 0x%08x\n",
  682. perms.allow, perms.deny, perms.audit, perms.quiet);
  683. }
  684. /*
  685. * Transaction based IO.
  686. * The file expects a write which triggers the transaction, and then
  687. * possibly a read(s) which collects the result - which is stored in a
  688. * file-local buffer. Once a new write is performed, a new set of results
  689. * are stored in the file-local buffer.
  690. */
  691. struct multi_transaction {
  692. struct kref count;
  693. ssize_t size;
  694. char data[];
  695. };
  696. #define MULTI_TRANSACTION_LIMIT (PAGE_SIZE - sizeof(struct multi_transaction))
  697. static void multi_transaction_kref(struct kref *kref)
  698. {
  699. struct multi_transaction *t;
  700. t = container_of(kref, struct multi_transaction, count);
  701. free_page((unsigned long) t);
  702. }
  703. static struct multi_transaction *
  704. get_multi_transaction(struct multi_transaction *t)
  705. {
  706. if (t)
  707. kref_get(&(t->count));
  708. return t;
  709. }
  710. static void put_multi_transaction(struct multi_transaction *t)
  711. {
  712. if (t)
  713. kref_put(&(t->count), multi_transaction_kref);
  714. }
  715. /* does not increment @new's count */
  716. static void multi_transaction_set(struct file *file,
  717. struct multi_transaction *new, size_t n)
  718. {
  719. struct multi_transaction *old;
  720. AA_BUG(n > MULTI_TRANSACTION_LIMIT);
  721. new->size = n;
  722. spin_lock(&file->f_lock);
  723. old = (struct multi_transaction *) file->private_data;
  724. file->private_data = new;
  725. spin_unlock(&file->f_lock);
  726. put_multi_transaction(old);
  727. }
  728. static struct multi_transaction *multi_transaction_new(struct file *file,
  729. const char __user *buf,
  730. size_t size)
  731. {
  732. struct multi_transaction *t;
  733. if (size > MULTI_TRANSACTION_LIMIT - 1)
  734. return ERR_PTR(-EFBIG);
  735. t = (struct multi_transaction *)get_zeroed_page(GFP_KERNEL);
  736. if (!t)
  737. return ERR_PTR(-ENOMEM);
  738. kref_init(&t->count);
  739. if (copy_from_user(t->data, buf, size)) {
  740. put_multi_transaction(t);
  741. return ERR_PTR(-EFAULT);
  742. }
  743. return t;
  744. }
  745. static ssize_t multi_transaction_read(struct file *file, char __user *buf,
  746. size_t size, loff_t *pos)
  747. {
  748. struct multi_transaction *t;
  749. ssize_t ret;
  750. spin_lock(&file->f_lock);
  751. t = get_multi_transaction(file->private_data);
  752. spin_unlock(&file->f_lock);
  753. if (!t)
  754. return 0;
  755. ret = simple_read_from_buffer(buf, size, pos, t->data, t->size);
  756. put_multi_transaction(t);
  757. return ret;
  758. }
  759. static int multi_transaction_release(struct inode *inode, struct file *file)
  760. {
  761. put_multi_transaction(file->private_data);
  762. return 0;
  763. }
  764. #define QUERY_CMD_LABEL "label\0"
  765. #define QUERY_CMD_LABEL_LEN 6
  766. #define QUERY_CMD_PROFILE "profile\0"
  767. #define QUERY_CMD_PROFILE_LEN 8
  768. #define QUERY_CMD_LABELALL "labelall\0"
  769. #define QUERY_CMD_LABELALL_LEN 9
  770. #define QUERY_CMD_DATA "data\0"
  771. #define QUERY_CMD_DATA_LEN 5
  772. /**
  773. * aa_write_access - generic permissions and data query
  774. * @file: pointer to open apparmorfs/access file
  775. * @ubuf: user buffer containing the complete query string (NOT NULL)
  776. * @count: size of ubuf
  777. * @ppos: position in the file (MUST BE ZERO)
  778. *
  779. * Allows for one permissions or data query per open(), write(), and read()
  780. * sequence. The only queries currently supported are label-based queries for
  781. * permissions or data.
  782. *
  783. * For permissions queries, ubuf must begin with "label\0", followed by the
  784. * profile query specific format described in the query_label() function
  785. * documentation.
  786. *
  787. * For data queries, ubuf must have the form "data\0<LABEL>\0<KEY>\0", where
  788. * <LABEL> is the name of the security confinement context and <KEY> is the
  789. * name of the data to retrieve.
  790. *
  791. * Returns: number of bytes written or -errno on failure
  792. */
  793. static ssize_t aa_write_access(struct file *file, const char __user *ubuf,
  794. size_t count, loff_t *ppos)
  795. {
  796. struct multi_transaction *t;
  797. ssize_t len;
  798. if (*ppos)
  799. return -ESPIPE;
  800. t = multi_transaction_new(file, ubuf, count);
  801. if (IS_ERR(t))
  802. return PTR_ERR(t);
  803. if (count > QUERY_CMD_PROFILE_LEN &&
  804. !memcmp(t->data, QUERY_CMD_PROFILE, QUERY_CMD_PROFILE_LEN)) {
  805. len = query_label(t->data, MULTI_TRANSACTION_LIMIT,
  806. t->data + QUERY_CMD_PROFILE_LEN,
  807. count - QUERY_CMD_PROFILE_LEN, true);
  808. } else if (count > QUERY_CMD_LABEL_LEN &&
  809. !memcmp(t->data, QUERY_CMD_LABEL, QUERY_CMD_LABEL_LEN)) {
  810. len = query_label(t->data, MULTI_TRANSACTION_LIMIT,
  811. t->data + QUERY_CMD_LABEL_LEN,
  812. count - QUERY_CMD_LABEL_LEN, true);
  813. } else if (count > QUERY_CMD_LABELALL_LEN &&
  814. !memcmp(t->data, QUERY_CMD_LABELALL,
  815. QUERY_CMD_LABELALL_LEN)) {
  816. len = query_label(t->data, MULTI_TRANSACTION_LIMIT,
  817. t->data + QUERY_CMD_LABELALL_LEN,
  818. count - QUERY_CMD_LABELALL_LEN, false);
  819. } else if (count > QUERY_CMD_DATA_LEN &&
  820. !memcmp(t->data, QUERY_CMD_DATA, QUERY_CMD_DATA_LEN)) {
  821. len = query_data(t->data, MULTI_TRANSACTION_LIMIT,
  822. t->data + QUERY_CMD_DATA_LEN,
  823. count - QUERY_CMD_DATA_LEN);
  824. } else
  825. len = -EINVAL;
  826. if (len < 0) {
  827. put_multi_transaction(t);
  828. return len;
  829. }
  830. multi_transaction_set(file, t, len);
  831. return count;
  832. }
  833. static const struct file_operations aa_sfs_access = {
  834. .write = aa_write_access,
  835. .read = multi_transaction_read,
  836. .release = multi_transaction_release,
  837. .llseek = generic_file_llseek,
  838. };
  839. static int aa_sfs_seq_show(struct seq_file *seq, void *v)
  840. {
  841. struct aa_sfs_entry *fs_file = seq->private;
  842. if (!fs_file)
  843. return 0;
  844. switch (fs_file->v_type) {
  845. case AA_SFS_TYPE_BOOLEAN:
  846. seq_printf(seq, "%s\n", fs_file->v.boolean ? "yes" : "no");
  847. break;
  848. case AA_SFS_TYPE_STRING:
  849. seq_printf(seq, "%s\n", fs_file->v.string);
  850. break;
  851. case AA_SFS_TYPE_U64:
  852. seq_printf(seq, "%#08lx\n", fs_file->v.u64);
  853. break;
  854. default:
  855. /* Ignore unpritable entry types. */
  856. break;
  857. }
  858. return 0;
  859. }
  860. static int aa_sfs_seq_open(struct inode *inode, struct file *file)
  861. {
  862. return single_open(file, aa_sfs_seq_show, inode->i_private);
  863. }
  864. const struct file_operations aa_sfs_seq_file_ops = {
  865. .owner = THIS_MODULE,
  866. .open = aa_sfs_seq_open,
  867. .read = seq_read,
  868. .llseek = seq_lseek,
  869. .release = single_release,
  870. };
  871. /*
  872. * profile based file operations
  873. * policy/profiles/XXXX/profiles/ *
  874. */
  875. #define SEQ_PROFILE_FOPS(NAME) \
  876. static int seq_profile_ ##NAME ##_open(struct inode *inode, struct file *file)\
  877. { \
  878. return seq_profile_open(inode, file, seq_profile_ ##NAME ##_show); \
  879. } \
  880. \
  881. static const struct file_operations seq_profile_ ##NAME ##_fops = { \
  882. .owner = THIS_MODULE, \
  883. .open = seq_profile_ ##NAME ##_open, \
  884. .read = seq_read, \
  885. .llseek = seq_lseek, \
  886. .release = seq_profile_release, \
  887. } \
  888. static int seq_profile_open(struct inode *inode, struct file *file,
  889. int (*show)(struct seq_file *, void *))
  890. {
  891. struct aa_proxy *proxy = aa_get_proxy(inode->i_private);
  892. int error = single_open(file, show, proxy);
  893. if (error) {
  894. file->private_data = NULL;
  895. aa_put_proxy(proxy);
  896. }
  897. return error;
  898. }
  899. static int seq_profile_release(struct inode *inode, struct file *file)
  900. {
  901. struct seq_file *seq = (struct seq_file *) file->private_data;
  902. if (seq)
  903. aa_put_proxy(seq->private);
  904. return single_release(inode, file);
  905. }
  906. static int seq_profile_name_show(struct seq_file *seq, void *v)
  907. {
  908. struct aa_proxy *proxy = seq->private;
  909. struct aa_label *label = aa_get_label_rcu(&proxy->label);
  910. struct aa_profile *profile = labels_profile(label);
  911. seq_printf(seq, "%s\n", profile->base.name);
  912. aa_put_label(label);
  913. return 0;
  914. }
  915. static int seq_profile_mode_show(struct seq_file *seq, void *v)
  916. {
  917. struct aa_proxy *proxy = seq->private;
  918. struct aa_label *label = aa_get_label_rcu(&proxy->label);
  919. struct aa_profile *profile = labels_profile(label);
  920. seq_printf(seq, "%s\n", aa_profile_mode_names[profile->mode]);
  921. aa_put_label(label);
  922. return 0;
  923. }
  924. static int seq_profile_attach_show(struct seq_file *seq, void *v)
  925. {
  926. struct aa_proxy *proxy = seq->private;
  927. struct aa_label *label = aa_get_label_rcu(&proxy->label);
  928. struct aa_profile *profile = labels_profile(label);
  929. if (profile->attach)
  930. seq_printf(seq, "%s\n", profile->attach);
  931. else if (profile->xmatch)
  932. seq_puts(seq, "<unknown>\n");
  933. else
  934. seq_printf(seq, "%s\n", profile->base.name);
  935. aa_put_label(label);
  936. return 0;
  937. }
  938. static int seq_profile_hash_show(struct seq_file *seq, void *v)
  939. {
  940. struct aa_proxy *proxy = seq->private;
  941. struct aa_label *label = aa_get_label_rcu(&proxy->label);
  942. struct aa_profile *profile = labels_profile(label);
  943. unsigned int i, size = aa_hash_size();
  944. if (profile->hash) {
  945. for (i = 0; i < size; i++)
  946. seq_printf(seq, "%.2x", profile->hash[i]);
  947. seq_putc(seq, '\n');
  948. }
  949. aa_put_label(label);
  950. return 0;
  951. }
  952. SEQ_PROFILE_FOPS(name);
  953. SEQ_PROFILE_FOPS(mode);
  954. SEQ_PROFILE_FOPS(attach);
  955. SEQ_PROFILE_FOPS(hash);
  956. /*
  957. * namespace based files
  958. * several root files and
  959. * policy/ *
  960. */
  961. #define SEQ_NS_FOPS(NAME) \
  962. static int seq_ns_ ##NAME ##_open(struct inode *inode, struct file *file) \
  963. { \
  964. return single_open(file, seq_ns_ ##NAME ##_show, inode->i_private); \
  965. } \
  966. \
  967. static const struct file_operations seq_ns_ ##NAME ##_fops = { \
  968. .owner = THIS_MODULE, \
  969. .open = seq_ns_ ##NAME ##_open, \
  970. .read = seq_read, \
  971. .llseek = seq_lseek, \
  972. .release = single_release, \
  973. } \
  974. static int seq_ns_stacked_show(struct seq_file *seq, void *v)
  975. {
  976. struct aa_label *label;
  977. label = begin_current_label_crit_section();
  978. seq_printf(seq, "%s\n", label->size > 1 ? "yes" : "no");
  979. end_current_label_crit_section(label);
  980. return 0;
  981. }
  982. static int seq_ns_nsstacked_show(struct seq_file *seq, void *v)
  983. {
  984. struct aa_label *label;
  985. struct aa_profile *profile;
  986. struct label_it it;
  987. int count = 1;
  988. label = begin_current_label_crit_section();
  989. if (label->size > 1) {
  990. label_for_each(it, label, profile)
  991. if (profile->ns != labels_ns(label)) {
  992. count++;
  993. break;
  994. }
  995. }
  996. seq_printf(seq, "%s\n", count > 1 ? "yes" : "no");
  997. end_current_label_crit_section(label);
  998. return 0;
  999. }
  1000. static int seq_ns_level_show(struct seq_file *seq, void *v)
  1001. {
  1002. struct aa_label *label;
  1003. label = begin_current_label_crit_section();
  1004. seq_printf(seq, "%d\n", labels_ns(label)->level);
  1005. end_current_label_crit_section(label);
  1006. return 0;
  1007. }
  1008. static int seq_ns_name_show(struct seq_file *seq, void *v)
  1009. {
  1010. struct aa_label *label = begin_current_label_crit_section();
  1011. seq_printf(seq, "%s\n", labels_ns(label)->base.name);
  1012. end_current_label_crit_section(label);
  1013. return 0;
  1014. }
  1015. SEQ_NS_FOPS(stacked);
  1016. SEQ_NS_FOPS(nsstacked);
  1017. SEQ_NS_FOPS(level);
  1018. SEQ_NS_FOPS(name);
  1019. /* policy/raw_data/ * file ops */
  1020. #ifdef CONFIG_SECURITY_APPARMOR_EXPORT_BINARY
  1021. #define SEQ_RAWDATA_FOPS(NAME) \
  1022. static int seq_rawdata_ ##NAME ##_open(struct inode *inode, struct file *file)\
  1023. { \
  1024. return seq_rawdata_open(inode, file, seq_rawdata_ ##NAME ##_show); \
  1025. } \
  1026. \
  1027. static const struct file_operations seq_rawdata_ ##NAME ##_fops = { \
  1028. .owner = THIS_MODULE, \
  1029. .open = seq_rawdata_ ##NAME ##_open, \
  1030. .read = seq_read, \
  1031. .llseek = seq_lseek, \
  1032. .release = seq_rawdata_release, \
  1033. } \
  1034. static int seq_rawdata_open(struct inode *inode, struct file *file,
  1035. int (*show)(struct seq_file *, void *))
  1036. {
  1037. struct aa_loaddata *data = __aa_get_loaddata(inode->i_private);
  1038. int error;
  1039. if (!data)
  1040. /* lost race this ent is being reaped */
  1041. return -ENOENT;
  1042. error = single_open(file, show, data);
  1043. if (error) {
  1044. AA_BUG(file->private_data &&
  1045. ((struct seq_file *)file->private_data)->private);
  1046. aa_put_loaddata(data);
  1047. }
  1048. return error;
  1049. }
  1050. static int seq_rawdata_release(struct inode *inode, struct file *file)
  1051. {
  1052. struct seq_file *seq = (struct seq_file *) file->private_data;
  1053. if (seq)
  1054. aa_put_loaddata(seq->private);
  1055. return single_release(inode, file);
  1056. }
  1057. static int seq_rawdata_abi_show(struct seq_file *seq, void *v)
  1058. {
  1059. struct aa_loaddata *data = seq->private;
  1060. seq_printf(seq, "v%d\n", data->abi);
  1061. return 0;
  1062. }
  1063. static int seq_rawdata_revision_show(struct seq_file *seq, void *v)
  1064. {
  1065. struct aa_loaddata *data = seq->private;
  1066. seq_printf(seq, "%ld\n", data->revision);
  1067. return 0;
  1068. }
  1069. static int seq_rawdata_hash_show(struct seq_file *seq, void *v)
  1070. {
  1071. struct aa_loaddata *data = seq->private;
  1072. unsigned int i, size = aa_hash_size();
  1073. if (data->hash) {
  1074. for (i = 0; i < size; i++)
  1075. seq_printf(seq, "%.2x", data->hash[i]);
  1076. seq_putc(seq, '\n');
  1077. }
  1078. return 0;
  1079. }
  1080. static int seq_rawdata_compressed_size_show(struct seq_file *seq, void *v)
  1081. {
  1082. struct aa_loaddata *data = seq->private;
  1083. seq_printf(seq, "%zu\n", data->compressed_size);
  1084. return 0;
  1085. }
  1086. SEQ_RAWDATA_FOPS(abi);
  1087. SEQ_RAWDATA_FOPS(revision);
  1088. SEQ_RAWDATA_FOPS(hash);
  1089. SEQ_RAWDATA_FOPS(compressed_size);
  1090. static int deflate_decompress(char *src, size_t slen, char *dst, size_t dlen)
  1091. {
  1092. #ifdef CONFIG_SECURITY_APPARMOR_EXPORT_BINARY
  1093. if (aa_g_rawdata_compression_level != 0) {
  1094. int error = 0;
  1095. struct z_stream_s strm;
  1096. memset(&strm, 0, sizeof(strm));
  1097. strm.workspace = kvzalloc(zlib_inflate_workspacesize(), GFP_KERNEL);
  1098. if (!strm.workspace)
  1099. return -ENOMEM;
  1100. strm.next_in = src;
  1101. strm.avail_in = slen;
  1102. error = zlib_inflateInit(&strm);
  1103. if (error != Z_OK) {
  1104. error = -ENOMEM;
  1105. goto fail_inflate_init;
  1106. }
  1107. strm.next_out = dst;
  1108. strm.avail_out = dlen;
  1109. error = zlib_inflate(&strm, Z_FINISH);
  1110. if (error != Z_STREAM_END)
  1111. error = -EINVAL;
  1112. else
  1113. error = 0;
  1114. zlib_inflateEnd(&strm);
  1115. fail_inflate_init:
  1116. kvfree(strm.workspace);
  1117. return error;
  1118. }
  1119. #endif
  1120. if (dlen < slen)
  1121. return -EINVAL;
  1122. memcpy(dst, src, slen);
  1123. return 0;
  1124. }
  1125. static ssize_t rawdata_read(struct file *file, char __user *buf, size_t size,
  1126. loff_t *ppos)
  1127. {
  1128. struct rawdata_f_data *private = file->private_data;
  1129. return simple_read_from_buffer(buf, size, ppos,
  1130. RAWDATA_F_DATA_BUF(private),
  1131. private->loaddata->size);
  1132. }
  1133. static int rawdata_release(struct inode *inode, struct file *file)
  1134. {
  1135. rawdata_f_data_free(file->private_data);
  1136. return 0;
  1137. }
  1138. static int rawdata_open(struct inode *inode, struct file *file)
  1139. {
  1140. int error;
  1141. struct aa_loaddata *loaddata;
  1142. struct rawdata_f_data *private;
  1143. if (!aa_current_policy_view_capable(NULL))
  1144. return -EACCES;
  1145. loaddata = __aa_get_loaddata(inode->i_private);
  1146. if (!loaddata)
  1147. /* lost race: this entry is being reaped */
  1148. return -ENOENT;
  1149. private = rawdata_f_data_alloc(loaddata->size);
  1150. if (IS_ERR(private)) {
  1151. error = PTR_ERR(private);
  1152. goto fail_private_alloc;
  1153. }
  1154. private->loaddata = loaddata;
  1155. error = deflate_decompress(loaddata->data, loaddata->compressed_size,
  1156. RAWDATA_F_DATA_BUF(private),
  1157. loaddata->size);
  1158. if (error)
  1159. goto fail_decompress;
  1160. file->private_data = private;
  1161. return 0;
  1162. fail_decompress:
  1163. rawdata_f_data_free(private);
  1164. return error;
  1165. fail_private_alloc:
  1166. aa_put_loaddata(loaddata);
  1167. return error;
  1168. }
  1169. static const struct file_operations rawdata_fops = {
  1170. .open = rawdata_open,
  1171. .read = rawdata_read,
  1172. .llseek = generic_file_llseek,
  1173. .release = rawdata_release,
  1174. };
  1175. static void remove_rawdata_dents(struct aa_loaddata *rawdata)
  1176. {
  1177. int i;
  1178. for (i = 0; i < AAFS_LOADDATA_NDENTS; i++) {
  1179. if (!IS_ERR_OR_NULL(rawdata->dents[i])) {
  1180. /* no refcounts on i_private */
  1181. aafs_remove(rawdata->dents[i]);
  1182. rawdata->dents[i] = NULL;
  1183. }
  1184. }
  1185. }
  1186. void __aa_fs_remove_rawdata(struct aa_loaddata *rawdata)
  1187. {
  1188. AA_BUG(rawdata->ns && !mutex_is_locked(&rawdata->ns->lock));
  1189. if (rawdata->ns) {
  1190. remove_rawdata_dents(rawdata);
  1191. list_del_init(&rawdata->list);
  1192. aa_put_ns(rawdata->ns);
  1193. rawdata->ns = NULL;
  1194. }
  1195. }
  1196. int __aa_fs_create_rawdata(struct aa_ns *ns, struct aa_loaddata *rawdata)
  1197. {
  1198. struct dentry *dent, *dir;
  1199. AA_BUG(!ns);
  1200. AA_BUG(!rawdata);
  1201. AA_BUG(!mutex_is_locked(&ns->lock));
  1202. AA_BUG(!ns_subdata_dir(ns));
  1203. /*
  1204. * just use ns revision dir was originally created at. This is
  1205. * under ns->lock and if load is successful revision will be
  1206. * bumped and is guaranteed to be unique
  1207. */
  1208. rawdata->name = kasprintf(GFP_KERNEL, "%ld", ns->revision);
  1209. if (!rawdata->name)
  1210. return -ENOMEM;
  1211. dir = aafs_create_dir(rawdata->name, ns_subdata_dir(ns));
  1212. if (IS_ERR(dir))
  1213. /* ->name freed when rawdata freed */
  1214. return PTR_ERR(dir);
  1215. rawdata->dents[AAFS_LOADDATA_DIR] = dir;
  1216. dent = aafs_create_file("abi", S_IFREG | 0444, dir, rawdata,
  1217. &seq_rawdata_abi_fops);
  1218. if (IS_ERR(dent))
  1219. goto fail;
  1220. rawdata->dents[AAFS_LOADDATA_ABI] = dent;
  1221. dent = aafs_create_file("revision", S_IFREG | 0444, dir, rawdata,
  1222. &seq_rawdata_revision_fops);
  1223. if (IS_ERR(dent))
  1224. goto fail;
  1225. rawdata->dents[AAFS_LOADDATA_REVISION] = dent;
  1226. if (aa_g_hash_policy) {
  1227. dent = aafs_create_file("sha1", S_IFREG | 0444, dir,
  1228. rawdata, &seq_rawdata_hash_fops);
  1229. if (IS_ERR(dent))
  1230. goto fail;
  1231. rawdata->dents[AAFS_LOADDATA_HASH] = dent;
  1232. }
  1233. dent = aafs_create_file("compressed_size", S_IFREG | 0444, dir,
  1234. rawdata,
  1235. &seq_rawdata_compressed_size_fops);
  1236. if (IS_ERR(dent))
  1237. goto fail;
  1238. rawdata->dents[AAFS_LOADDATA_COMPRESSED_SIZE] = dent;
  1239. dent = aafs_create_file("raw_data", S_IFREG | 0444,
  1240. dir, rawdata, &rawdata_fops);
  1241. if (IS_ERR(dent))
  1242. goto fail;
  1243. rawdata->dents[AAFS_LOADDATA_DATA] = dent;
  1244. d_inode(dent)->i_size = rawdata->size;
  1245. rawdata->ns = aa_get_ns(ns);
  1246. list_add(&rawdata->list, &ns->rawdata_list);
  1247. /* no refcount on inode rawdata */
  1248. return 0;
  1249. fail:
  1250. remove_rawdata_dents(rawdata);
  1251. return PTR_ERR(dent);
  1252. }
  1253. #endif /* CONFIG_SECURITY_APPARMOR_EXPORT_BINARY */
  1254. /** fns to setup dynamic per profile/namespace files **/
  1255. /*
  1256. *
  1257. * Requires: @profile->ns->lock held
  1258. */
  1259. void __aafs_profile_rmdir(struct aa_profile *profile)
  1260. {
  1261. struct aa_profile *child;
  1262. int i;
  1263. if (!profile)
  1264. return;
  1265. list_for_each_entry(child, &profile->base.profiles, base.list)
  1266. __aafs_profile_rmdir(child);
  1267. for (i = AAFS_PROF_SIZEOF - 1; i >= 0; --i) {
  1268. struct aa_proxy *proxy;
  1269. if (!profile->dents[i])
  1270. continue;
  1271. proxy = d_inode(profile->dents[i])->i_private;
  1272. aafs_remove(profile->dents[i]);
  1273. aa_put_proxy(proxy);
  1274. profile->dents[i] = NULL;
  1275. }
  1276. }
  1277. /*
  1278. *
  1279. * Requires: @old->ns->lock held
  1280. */
  1281. void __aafs_profile_migrate_dents(struct aa_profile *old,
  1282. struct aa_profile *new)
  1283. {
  1284. int i;
  1285. AA_BUG(!old);
  1286. AA_BUG(!new);
  1287. AA_BUG(!mutex_is_locked(&profiles_ns(old)->lock));
  1288. for (i = 0; i < AAFS_PROF_SIZEOF; i++) {
  1289. new->dents[i] = old->dents[i];
  1290. if (new->dents[i])
  1291. new->dents[i]->d_inode->i_mtime = current_time(new->dents[i]->d_inode);
  1292. old->dents[i] = NULL;
  1293. }
  1294. }
  1295. static struct dentry *create_profile_file(struct dentry *dir, const char *name,
  1296. struct aa_profile *profile,
  1297. const struct file_operations *fops)
  1298. {
  1299. struct aa_proxy *proxy = aa_get_proxy(profile->label.proxy);
  1300. struct dentry *dent;
  1301. dent = aafs_create_file(name, S_IFREG | 0444, dir, proxy, fops);
  1302. if (IS_ERR(dent))
  1303. aa_put_proxy(proxy);
  1304. return dent;
  1305. }
  1306. #ifdef CONFIG_SECURITY_APPARMOR_EXPORT_BINARY
  1307. static int profile_depth(struct aa_profile *profile)
  1308. {
  1309. int depth = 0;
  1310. rcu_read_lock();
  1311. for (depth = 0; profile; profile = rcu_access_pointer(profile->parent))
  1312. depth++;
  1313. rcu_read_unlock();
  1314. return depth;
  1315. }
  1316. static char *gen_symlink_name(int depth, const char *dirname, const char *fname)
  1317. {
  1318. char *buffer, *s;
  1319. int error;
  1320. int size = depth * 6 + strlen(dirname) + strlen(fname) + 11;
  1321. s = buffer = kmalloc(size, GFP_KERNEL);
  1322. if (!buffer)
  1323. return ERR_PTR(-ENOMEM);
  1324. for (; depth > 0; depth--) {
  1325. strcpy(s, "../../");
  1326. s += 6;
  1327. size -= 6;
  1328. }
  1329. error = snprintf(s, size, "raw_data/%s/%s", dirname, fname);
  1330. if (error >= size || error < 0) {
  1331. kfree(buffer);
  1332. return ERR_PTR(-ENAMETOOLONG);
  1333. }
  1334. return buffer;
  1335. }
  1336. static void rawdata_link_cb(void *arg)
  1337. {
  1338. kfree(arg);
  1339. }
  1340. static const char *rawdata_get_link_base(struct dentry *dentry,
  1341. struct inode *inode,
  1342. struct delayed_call *done,
  1343. const char *name)
  1344. {
  1345. struct aa_proxy *proxy = inode->i_private;
  1346. struct aa_label *label;
  1347. struct aa_profile *profile;
  1348. char *target;
  1349. int depth;
  1350. if (!dentry)
  1351. return ERR_PTR(-ECHILD);
  1352. label = aa_get_label_rcu(&proxy->label);
  1353. profile = labels_profile(label);
  1354. depth = profile_depth(profile);
  1355. target = gen_symlink_name(depth, profile->rawdata->name, name);
  1356. aa_put_label(label);
  1357. if (IS_ERR(target))
  1358. return target;
  1359. set_delayed_call(done, rawdata_link_cb, target);
  1360. return target;
  1361. }
  1362. static const char *rawdata_get_link_sha1(struct dentry *dentry,
  1363. struct inode *inode,
  1364. struct delayed_call *done)
  1365. {
  1366. return rawdata_get_link_base(dentry, inode, done, "sha1");
  1367. }
  1368. static const char *rawdata_get_link_abi(struct dentry *dentry,
  1369. struct inode *inode,
  1370. struct delayed_call *done)
  1371. {
  1372. return rawdata_get_link_base(dentry, inode, done, "abi");
  1373. }
  1374. static const char *rawdata_get_link_data(struct dentry *dentry,
  1375. struct inode *inode,
  1376. struct delayed_call *done)
  1377. {
  1378. return rawdata_get_link_base(dentry, inode, done, "raw_data");
  1379. }
  1380. static const struct inode_operations rawdata_link_sha1_iops = {
  1381. .get_link = rawdata_get_link_sha1,
  1382. };
  1383. static const struct inode_operations rawdata_link_abi_iops = {
  1384. .get_link = rawdata_get_link_abi,
  1385. };
  1386. static const struct inode_operations rawdata_link_data_iops = {
  1387. .get_link = rawdata_get_link_data,
  1388. };
  1389. #endif /* CONFIG_SECURITY_APPARMOR_EXPORT_BINARY */
  1390. /*
  1391. * Requires: @profile->ns->lock held
  1392. */
  1393. int __aafs_profile_mkdir(struct aa_profile *profile, struct dentry *parent)
  1394. {
  1395. struct aa_profile *child;
  1396. struct dentry *dent = NULL, *dir;
  1397. int error;
  1398. AA_BUG(!profile);
  1399. AA_BUG(!mutex_is_locked(&profiles_ns(profile)->lock));
  1400. if (!parent) {
  1401. struct aa_profile *p;
  1402. p = aa_deref_parent(profile);
  1403. dent = prof_dir(p);
  1404. /* adding to parent that previously didn't have children */
  1405. dent = aafs_create_dir("profiles", dent);
  1406. if (IS_ERR(dent))
  1407. goto fail;
  1408. prof_child_dir(p) = parent = dent;
  1409. }
  1410. if (!profile->dirname) {
  1411. int len, id_len;
  1412. len = mangle_name(profile->base.name, NULL);
  1413. id_len = snprintf(NULL, 0, ".%ld", profile->ns->uniq_id);
  1414. profile->dirname = kmalloc(len + id_len + 1, GFP_KERNEL);
  1415. if (!profile->dirname) {
  1416. error = -ENOMEM;
  1417. goto fail2;
  1418. }
  1419. mangle_name(profile->base.name, profile->dirname);
  1420. sprintf(profile->dirname + len, ".%ld", profile->ns->uniq_id++);
  1421. }
  1422. dent = aafs_create_dir(profile->dirname, parent);
  1423. if (IS_ERR(dent))
  1424. goto fail;
  1425. prof_dir(profile) = dir = dent;
  1426. dent = create_profile_file(dir, "name", profile,
  1427. &seq_profile_name_fops);
  1428. if (IS_ERR(dent))
  1429. goto fail;
  1430. profile->dents[AAFS_PROF_NAME] = dent;
  1431. dent = create_profile_file(dir, "mode", profile,
  1432. &seq_profile_mode_fops);
  1433. if (IS_ERR(dent))
  1434. goto fail;
  1435. profile->dents[AAFS_PROF_MODE] = dent;
  1436. dent = create_profile_file(dir, "attach", profile,
  1437. &seq_profile_attach_fops);
  1438. if (IS_ERR(dent))
  1439. goto fail;
  1440. profile->dents[AAFS_PROF_ATTACH] = dent;
  1441. if (profile->hash) {
  1442. dent = create_profile_file(dir, "sha1", profile,
  1443. &seq_profile_hash_fops);
  1444. if (IS_ERR(dent))
  1445. goto fail;
  1446. profile->dents[AAFS_PROF_HASH] = dent;
  1447. }
  1448. #ifdef CONFIG_SECURITY_APPARMOR_EXPORT_BINARY
  1449. if (profile->rawdata) {
  1450. if (aa_g_hash_policy) {
  1451. dent = aafs_create("raw_sha1", S_IFLNK | 0444, dir,
  1452. profile->label.proxy, NULL, NULL,
  1453. &rawdata_link_sha1_iops);
  1454. if (IS_ERR(dent))
  1455. goto fail;
  1456. aa_get_proxy(profile->label.proxy);
  1457. profile->dents[AAFS_PROF_RAW_HASH] = dent;
  1458. }
  1459. dent = aafs_create("raw_abi", S_IFLNK | 0444, dir,
  1460. profile->label.proxy, NULL, NULL,
  1461. &rawdata_link_abi_iops);
  1462. if (IS_ERR(dent))
  1463. goto fail;
  1464. aa_get_proxy(profile->label.proxy);
  1465. profile->dents[AAFS_PROF_RAW_ABI] = dent;
  1466. dent = aafs_create("raw_data", S_IFLNK | 0444, dir,
  1467. profile->label.proxy, NULL, NULL,
  1468. &rawdata_link_data_iops);
  1469. if (IS_ERR(dent))
  1470. goto fail;
  1471. aa_get_proxy(profile->label.proxy);
  1472. profile->dents[AAFS_PROF_RAW_DATA] = dent;
  1473. }
  1474. #endif /*CONFIG_SECURITY_APPARMOR_EXPORT_BINARY */
  1475. list_for_each_entry(child, &profile->base.profiles, base.list) {
  1476. error = __aafs_profile_mkdir(child, prof_child_dir(profile));
  1477. if (error)
  1478. goto fail2;
  1479. }
  1480. return 0;
  1481. fail:
  1482. error = PTR_ERR(dent);
  1483. fail2:
  1484. __aafs_profile_rmdir(profile);
  1485. return error;
  1486. }
  1487. static int ns_mkdir_op(struct user_namespace *mnt_userns, struct inode *dir,
  1488. struct dentry *dentry, umode_t mode)
  1489. {
  1490. struct aa_ns *ns, *parent;
  1491. /* TODO: improve permission check */
  1492. struct aa_label *label;
  1493. int error;
  1494. label = begin_current_label_crit_section();
  1495. error = aa_may_manage_policy(label, NULL, AA_MAY_LOAD_POLICY);
  1496. end_current_label_crit_section(label);
  1497. if (error)
  1498. return error;
  1499. parent = aa_get_ns(dir->i_private);
  1500. AA_BUG(d_inode(ns_subns_dir(parent)) != dir);
  1501. /* we have to unlock and then relock to get locking order right
  1502. * for pin_fs
  1503. */
  1504. inode_unlock(dir);
  1505. error = simple_pin_fs(&aafs_ops, &aafs_mnt, &aafs_count);
  1506. mutex_lock_nested(&parent->lock, parent->level);
  1507. inode_lock_nested(dir, I_MUTEX_PARENT);
  1508. if (error)
  1509. goto out;
  1510. error = __aafs_setup_d_inode(dir, dentry, mode | S_IFDIR, NULL,
  1511. NULL, NULL, NULL);
  1512. if (error)
  1513. goto out_pin;
  1514. ns = __aa_find_or_create_ns(parent, READ_ONCE(dentry->d_name.name),
  1515. dentry);
  1516. if (IS_ERR(ns)) {
  1517. error = PTR_ERR(ns);
  1518. ns = NULL;
  1519. }
  1520. aa_put_ns(ns); /* list ref remains */
  1521. out_pin:
  1522. if (error)
  1523. simple_release_fs(&aafs_mnt, &aafs_count);
  1524. out:
  1525. mutex_unlock(&parent->lock);
  1526. aa_put_ns(parent);
  1527. return error;
  1528. }
  1529. static int ns_rmdir_op(struct inode *dir, struct dentry *dentry)
  1530. {
  1531. struct aa_ns *ns, *parent;
  1532. /* TODO: improve permission check */
  1533. struct aa_label *label;
  1534. int error;
  1535. label = begin_current_label_crit_section();
  1536. error = aa_may_manage_policy(label, NULL, AA_MAY_LOAD_POLICY);
  1537. end_current_label_crit_section(label);
  1538. if (error)
  1539. return error;
  1540. parent = aa_get_ns(dir->i_private);
  1541. /* rmdir calls the generic securityfs functions to remove files
  1542. * from the apparmor dir. It is up to the apparmor ns locking
  1543. * to avoid races.
  1544. */
  1545. inode_unlock(dir);
  1546. inode_unlock(dentry->d_inode);
  1547. mutex_lock_nested(&parent->lock, parent->level);
  1548. ns = aa_get_ns(__aa_findn_ns(&parent->sub_ns, dentry->d_name.name,
  1549. dentry->d_name.len));
  1550. if (!ns) {
  1551. error = -ENOENT;
  1552. goto out;
  1553. }
  1554. AA_BUG(ns_dir(ns) != dentry);
  1555. __aa_remove_ns(ns);
  1556. aa_put_ns(ns);
  1557. out:
  1558. mutex_unlock(&parent->lock);
  1559. inode_lock_nested(dir, I_MUTEX_PARENT);
  1560. inode_lock(dentry->d_inode);
  1561. aa_put_ns(parent);
  1562. return error;
  1563. }
  1564. static const struct inode_operations ns_dir_inode_operations = {
  1565. .lookup = simple_lookup,
  1566. .mkdir = ns_mkdir_op,
  1567. .rmdir = ns_rmdir_op,
  1568. };
  1569. static void __aa_fs_list_remove_rawdata(struct aa_ns *ns)
  1570. {
  1571. struct aa_loaddata *ent, *tmp;
  1572. AA_BUG(!mutex_is_locked(&ns->lock));
  1573. list_for_each_entry_safe(ent, tmp, &ns->rawdata_list, list)
  1574. __aa_fs_remove_rawdata(ent);
  1575. }
  1576. /*
  1577. *
  1578. * Requires: @ns->lock held
  1579. */
  1580. void __aafs_ns_rmdir(struct aa_ns *ns)
  1581. {
  1582. struct aa_ns *sub;
  1583. struct aa_profile *child;
  1584. int i;
  1585. if (!ns)
  1586. return;
  1587. AA_BUG(!mutex_is_locked(&ns->lock));
  1588. list_for_each_entry(child, &ns->base.profiles, base.list)
  1589. __aafs_profile_rmdir(child);
  1590. list_for_each_entry(sub, &ns->sub_ns, base.list) {
  1591. mutex_lock_nested(&sub->lock, sub->level);
  1592. __aafs_ns_rmdir(sub);
  1593. mutex_unlock(&sub->lock);
  1594. }
  1595. __aa_fs_list_remove_rawdata(ns);
  1596. if (ns_subns_dir(ns)) {
  1597. sub = d_inode(ns_subns_dir(ns))->i_private;
  1598. aa_put_ns(sub);
  1599. }
  1600. if (ns_subload(ns)) {
  1601. sub = d_inode(ns_subload(ns))->i_private;
  1602. aa_put_ns(sub);
  1603. }
  1604. if (ns_subreplace(ns)) {
  1605. sub = d_inode(ns_subreplace(ns))->i_private;
  1606. aa_put_ns(sub);
  1607. }
  1608. if (ns_subremove(ns)) {
  1609. sub = d_inode(ns_subremove(ns))->i_private;
  1610. aa_put_ns(sub);
  1611. }
  1612. if (ns_subrevision(ns)) {
  1613. sub = d_inode(ns_subrevision(ns))->i_private;
  1614. aa_put_ns(sub);
  1615. }
  1616. for (i = AAFS_NS_SIZEOF - 1; i >= 0; --i) {
  1617. aafs_remove(ns->dents[i]);
  1618. ns->dents[i] = NULL;
  1619. }
  1620. }
  1621. /* assumes cleanup in caller */
  1622. static int __aafs_ns_mkdir_entries(struct aa_ns *ns, struct dentry *dir)
  1623. {
  1624. struct dentry *dent;
  1625. AA_BUG(!ns);
  1626. AA_BUG(!dir);
  1627. dent = aafs_create_dir("profiles", dir);
  1628. if (IS_ERR(dent))
  1629. return PTR_ERR(dent);
  1630. ns_subprofs_dir(ns) = dent;
  1631. dent = aafs_create_dir("raw_data", dir);
  1632. if (IS_ERR(dent))
  1633. return PTR_ERR(dent);
  1634. ns_subdata_dir(ns) = dent;
  1635. dent = aafs_create_file("revision", 0444, dir, ns,
  1636. &aa_fs_ns_revision_fops);
  1637. if (IS_ERR(dent))
  1638. return PTR_ERR(dent);
  1639. aa_get_ns(ns);
  1640. ns_subrevision(ns) = dent;
  1641. dent = aafs_create_file(".load", 0640, dir, ns,
  1642. &aa_fs_profile_load);
  1643. if (IS_ERR(dent))
  1644. return PTR_ERR(dent);
  1645. aa_get_ns(ns);
  1646. ns_subload(ns) = dent;
  1647. dent = aafs_create_file(".replace", 0640, dir, ns,
  1648. &aa_fs_profile_replace);
  1649. if (IS_ERR(dent))
  1650. return PTR_ERR(dent);
  1651. aa_get_ns(ns);
  1652. ns_subreplace(ns) = dent;
  1653. dent = aafs_create_file(".remove", 0640, dir, ns,
  1654. &aa_fs_profile_remove);
  1655. if (IS_ERR(dent))
  1656. return PTR_ERR(dent);
  1657. aa_get_ns(ns);
  1658. ns_subremove(ns) = dent;
  1659. /* use create_dentry so we can supply private data */
  1660. dent = aafs_create("namespaces", S_IFDIR | 0755, dir, ns, NULL, NULL,
  1661. &ns_dir_inode_operations);
  1662. if (IS_ERR(dent))
  1663. return PTR_ERR(dent);
  1664. aa_get_ns(ns);
  1665. ns_subns_dir(ns) = dent;
  1666. return 0;
  1667. }
  1668. /*
  1669. * Requires: @ns->lock held
  1670. */
  1671. int __aafs_ns_mkdir(struct aa_ns *ns, struct dentry *parent, const char *name,
  1672. struct dentry *dent)
  1673. {
  1674. struct aa_ns *sub;
  1675. struct aa_profile *child;
  1676. struct dentry *dir;
  1677. int error;
  1678. AA_BUG(!ns);
  1679. AA_BUG(!parent);
  1680. AA_BUG(!mutex_is_locked(&ns->lock));
  1681. if (!name)
  1682. name = ns->base.name;
  1683. if (!dent) {
  1684. /* create ns dir if it doesn't already exist */
  1685. dent = aafs_create_dir(name, parent);
  1686. if (IS_ERR(dent))
  1687. goto fail;
  1688. } else
  1689. dget(dent);
  1690. ns_dir(ns) = dir = dent;
  1691. error = __aafs_ns_mkdir_entries(ns, dir);
  1692. if (error)
  1693. goto fail2;
  1694. /* profiles */
  1695. list_for_each_entry(child, &ns->base.profiles, base.list) {
  1696. error = __aafs_profile_mkdir(child, ns_subprofs_dir(ns));
  1697. if (error)
  1698. goto fail2;
  1699. }
  1700. /* subnamespaces */
  1701. list_for_each_entry(sub, &ns->sub_ns, base.list) {
  1702. mutex_lock_nested(&sub->lock, sub->level);
  1703. error = __aafs_ns_mkdir(sub, ns_subns_dir(ns), NULL, NULL);
  1704. mutex_unlock(&sub->lock);
  1705. if (error)
  1706. goto fail2;
  1707. }
  1708. return 0;
  1709. fail:
  1710. error = PTR_ERR(dent);
  1711. fail2:
  1712. __aafs_ns_rmdir(ns);
  1713. return error;
  1714. }
  1715. /**
  1716. * __next_ns - find the next namespace to list
  1717. * @root: root namespace to stop search at (NOT NULL)
  1718. * @ns: current ns position (NOT NULL)
  1719. *
  1720. * Find the next namespace from @ns under @root and handle all locking needed
  1721. * while switching current namespace.
  1722. *
  1723. * Returns: next namespace or NULL if at last namespace under @root
  1724. * Requires: ns->parent->lock to be held
  1725. * NOTE: will not unlock root->lock
  1726. */
  1727. static struct aa_ns *__next_ns(struct aa_ns *root, struct aa_ns *ns)
  1728. {
  1729. struct aa_ns *parent, *next;
  1730. AA_BUG(!root);
  1731. AA_BUG(!ns);
  1732. AA_BUG(ns != root && !mutex_is_locked(&ns->parent->lock));
  1733. /* is next namespace a child */
  1734. if (!list_empty(&ns->sub_ns)) {
  1735. next = list_first_entry(&ns->sub_ns, typeof(*ns), base.list);
  1736. mutex_lock_nested(&next->lock, next->level);
  1737. return next;
  1738. }
  1739. /* check if the next ns is a sibling, parent, gp, .. */
  1740. parent = ns->parent;
  1741. while (ns != root) {
  1742. mutex_unlock(&ns->lock);
  1743. next = list_next_entry(ns, base.list);
  1744. if (!list_entry_is_head(next, &parent->sub_ns, base.list)) {
  1745. mutex_lock_nested(&next->lock, next->level);
  1746. return next;
  1747. }
  1748. ns = parent;
  1749. parent = parent->parent;
  1750. }
  1751. return NULL;
  1752. }
  1753. /**
  1754. * __first_profile - find the first profile in a namespace
  1755. * @root: namespace that is root of profiles being displayed (NOT NULL)
  1756. * @ns: namespace to start in (NOT NULL)
  1757. *
  1758. * Returns: unrefcounted profile or NULL if no profile
  1759. * Requires: profile->ns.lock to be held
  1760. */
  1761. static struct aa_profile *__first_profile(struct aa_ns *root,
  1762. struct aa_ns *ns)
  1763. {
  1764. AA_BUG(!root);
  1765. AA_BUG(ns && !mutex_is_locked(&ns->lock));
  1766. for (; ns; ns = __next_ns(root, ns)) {
  1767. if (!list_empty(&ns->base.profiles))
  1768. return list_first_entry(&ns->base.profiles,
  1769. struct aa_profile, base.list);
  1770. }
  1771. return NULL;
  1772. }
  1773. /**
  1774. * __next_profile - step to the next profile in a profile tree
  1775. * @p: current profile in tree (NOT NULL)
  1776. *
  1777. * Perform a depth first traversal on the profile tree in a namespace
  1778. *
  1779. * Returns: next profile or NULL if done
  1780. * Requires: profile->ns.lock to be held
  1781. */
  1782. static struct aa_profile *__next_profile(struct aa_profile *p)
  1783. {
  1784. struct aa_profile *parent;
  1785. struct aa_ns *ns = p->ns;
  1786. AA_BUG(!mutex_is_locked(&profiles_ns(p)->lock));
  1787. /* is next profile a child */
  1788. if (!list_empty(&p->base.profiles))
  1789. return list_first_entry(&p->base.profiles, typeof(*p),
  1790. base.list);
  1791. /* is next profile a sibling, parent sibling, gp, sibling, .. */
  1792. parent = rcu_dereference_protected(p->parent,
  1793. mutex_is_locked(&p->ns->lock));
  1794. while (parent) {
  1795. p = list_next_entry(p, base.list);
  1796. if (!list_entry_is_head(p, &parent->base.profiles, base.list))
  1797. return p;
  1798. p = parent;
  1799. parent = rcu_dereference_protected(parent->parent,
  1800. mutex_is_locked(&parent->ns->lock));
  1801. }
  1802. /* is next another profile in the namespace */
  1803. p = list_next_entry(p, base.list);
  1804. if (!list_entry_is_head(p, &ns->base.profiles, base.list))
  1805. return p;
  1806. return NULL;
  1807. }
  1808. /**
  1809. * next_profile - step to the next profile in where ever it may be
  1810. * @root: root namespace (NOT NULL)
  1811. * @profile: current profile (NOT NULL)
  1812. *
  1813. * Returns: next profile or NULL if there isn't one
  1814. */
  1815. static struct aa_profile *next_profile(struct aa_ns *root,
  1816. struct aa_profile *profile)
  1817. {
  1818. struct aa_profile *next = __next_profile(profile);
  1819. if (next)
  1820. return next;
  1821. /* finished all profiles in namespace move to next namespace */
  1822. return __first_profile(root, __next_ns(root, profile->ns));
  1823. }
  1824. /**
  1825. * p_start - start a depth first traversal of profile tree
  1826. * @f: seq_file to fill
  1827. * @pos: current position
  1828. *
  1829. * Returns: first profile under current namespace or NULL if none found
  1830. *
  1831. * acquires first ns->lock
  1832. */
  1833. static void *p_start(struct seq_file *f, loff_t *pos)
  1834. {
  1835. struct aa_profile *profile = NULL;
  1836. struct aa_ns *root = aa_get_current_ns();
  1837. loff_t l = *pos;
  1838. f->private = root;
  1839. /* find the first profile */
  1840. mutex_lock_nested(&root->lock, root->level);
  1841. profile = __first_profile(root, root);
  1842. /* skip to position */
  1843. for (; profile && l > 0; l--)
  1844. profile = next_profile(root, profile);
  1845. return profile;
  1846. }
  1847. /**
  1848. * p_next - read the next profile entry
  1849. * @f: seq_file to fill
  1850. * @p: profile previously returned
  1851. * @pos: current position
  1852. *
  1853. * Returns: next profile after @p or NULL if none
  1854. *
  1855. * may acquire/release locks in namespace tree as necessary
  1856. */
  1857. static void *p_next(struct seq_file *f, void *p, loff_t *pos)
  1858. {
  1859. struct aa_profile *profile = p;
  1860. struct aa_ns *ns = f->private;
  1861. (*pos)++;
  1862. return next_profile(ns, profile);
  1863. }
  1864. /**
  1865. * p_stop - stop depth first traversal
  1866. * @f: seq_file we are filling
  1867. * @p: the last profile writen
  1868. *
  1869. * Release all locking done by p_start/p_next on namespace tree
  1870. */
  1871. static void p_stop(struct seq_file *f, void *p)
  1872. {
  1873. struct aa_profile *profile = p;
  1874. struct aa_ns *root = f->private, *ns;
  1875. if (profile) {
  1876. for (ns = profile->ns; ns && ns != root; ns = ns->parent)
  1877. mutex_unlock(&ns->lock);
  1878. }
  1879. mutex_unlock(&root->lock);
  1880. aa_put_ns(root);
  1881. }
  1882. /**
  1883. * seq_show_profile - show a profile entry
  1884. * @f: seq_file to file
  1885. * @p: current position (profile) (NOT NULL)
  1886. *
  1887. * Returns: error on failure
  1888. */
  1889. static int seq_show_profile(struct seq_file *f, void *p)
  1890. {
  1891. struct aa_profile *profile = (struct aa_profile *)p;
  1892. struct aa_ns *root = f->private;
  1893. aa_label_seq_xprint(f, root, &profile->label,
  1894. FLAG_SHOW_MODE | FLAG_VIEW_SUBNS, GFP_KERNEL);
  1895. seq_putc(f, '\n');
  1896. return 0;
  1897. }
  1898. static const struct seq_operations aa_sfs_profiles_op = {
  1899. .start = p_start,
  1900. .next = p_next,
  1901. .stop = p_stop,
  1902. .show = seq_show_profile,
  1903. };
  1904. static int profiles_open(struct inode *inode, struct file *file)
  1905. {
  1906. if (!aa_current_policy_view_capable(NULL))
  1907. return -EACCES;
  1908. return seq_open(file, &aa_sfs_profiles_op);
  1909. }
  1910. static int profiles_release(struct inode *inode, struct file *file)
  1911. {
  1912. return seq_release(inode, file);
  1913. }
  1914. static const struct file_operations aa_sfs_profiles_fops = {
  1915. .open = profiles_open,
  1916. .read = seq_read,
  1917. .llseek = seq_lseek,
  1918. .release = profiles_release,
  1919. };
  1920. /** Base file system setup **/
  1921. static struct aa_sfs_entry aa_sfs_entry_file[] = {
  1922. AA_SFS_FILE_STRING("mask",
  1923. "create read write exec append mmap_exec link lock"),
  1924. { }
  1925. };
  1926. static struct aa_sfs_entry aa_sfs_entry_ptrace[] = {
  1927. AA_SFS_FILE_STRING("mask", "read trace"),
  1928. { }
  1929. };
  1930. static struct aa_sfs_entry aa_sfs_entry_signal[] = {
  1931. AA_SFS_FILE_STRING("mask", AA_SFS_SIG_MASK),
  1932. { }
  1933. };
  1934. static struct aa_sfs_entry aa_sfs_entry_attach[] = {
  1935. AA_SFS_FILE_BOOLEAN("xattr", 1),
  1936. { }
  1937. };
  1938. static struct aa_sfs_entry aa_sfs_entry_domain[] = {
  1939. AA_SFS_FILE_BOOLEAN("change_hat", 1),
  1940. AA_SFS_FILE_BOOLEAN("change_hatv", 1),
  1941. AA_SFS_FILE_BOOLEAN("change_onexec", 1),
  1942. AA_SFS_FILE_BOOLEAN("change_profile", 1),
  1943. AA_SFS_FILE_BOOLEAN("stack", 1),
  1944. AA_SFS_FILE_BOOLEAN("fix_binfmt_elf_mmap", 1),
  1945. AA_SFS_FILE_BOOLEAN("post_nnp_subset", 1),
  1946. AA_SFS_FILE_BOOLEAN("computed_longest_left", 1),
  1947. AA_SFS_DIR("attach_conditions", aa_sfs_entry_attach),
  1948. AA_SFS_FILE_STRING("version", "1.2"),
  1949. { }
  1950. };
  1951. static struct aa_sfs_entry aa_sfs_entry_versions[] = {
  1952. AA_SFS_FILE_BOOLEAN("v5", 1),
  1953. AA_SFS_FILE_BOOLEAN("v6", 1),
  1954. AA_SFS_FILE_BOOLEAN("v7", 1),
  1955. AA_SFS_FILE_BOOLEAN("v8", 1),
  1956. AA_SFS_FILE_BOOLEAN("v9", 1),
  1957. { }
  1958. };
  1959. static struct aa_sfs_entry aa_sfs_entry_policy[] = {
  1960. AA_SFS_DIR("versions", aa_sfs_entry_versions),
  1961. AA_SFS_FILE_BOOLEAN("set_load", 1),
  1962. /* number of out of band transitions supported */
  1963. AA_SFS_FILE_U64("outofband", MAX_OOB_SUPPORTED),
  1964. { }
  1965. };
  1966. static struct aa_sfs_entry aa_sfs_entry_mount[] = {
  1967. AA_SFS_FILE_STRING("mask", "mount umount pivot_root"),
  1968. { }
  1969. };
  1970. static struct aa_sfs_entry aa_sfs_entry_ns[] = {
  1971. AA_SFS_FILE_BOOLEAN("profile", 1),
  1972. AA_SFS_FILE_BOOLEAN("pivot_root", 0),
  1973. { }
  1974. };
  1975. static struct aa_sfs_entry aa_sfs_entry_query_label[] = {
  1976. AA_SFS_FILE_STRING("perms", "allow deny audit quiet"),
  1977. AA_SFS_FILE_BOOLEAN("data", 1),
  1978. AA_SFS_FILE_BOOLEAN("multi_transaction", 1),
  1979. { }
  1980. };
  1981. static struct aa_sfs_entry aa_sfs_entry_query[] = {
  1982. AA_SFS_DIR("label", aa_sfs_entry_query_label),
  1983. { }
  1984. };
  1985. static struct aa_sfs_entry aa_sfs_entry_features[] = {
  1986. AA_SFS_DIR("policy", aa_sfs_entry_policy),
  1987. AA_SFS_DIR("domain", aa_sfs_entry_domain),
  1988. AA_SFS_DIR("file", aa_sfs_entry_file),
  1989. AA_SFS_DIR("network_v8", aa_sfs_entry_network),
  1990. AA_SFS_DIR("mount", aa_sfs_entry_mount),
  1991. AA_SFS_DIR("namespaces", aa_sfs_entry_ns),
  1992. AA_SFS_FILE_U64("capability", VFS_CAP_FLAGS_MASK),
  1993. AA_SFS_DIR("rlimit", aa_sfs_entry_rlimit),
  1994. AA_SFS_DIR("caps", aa_sfs_entry_caps),
  1995. AA_SFS_DIR("ptrace", aa_sfs_entry_ptrace),
  1996. AA_SFS_DIR("signal", aa_sfs_entry_signal),
  1997. AA_SFS_DIR("query", aa_sfs_entry_query),
  1998. { }
  1999. };
  2000. static struct aa_sfs_entry aa_sfs_entry_apparmor[] = {
  2001. AA_SFS_FILE_FOPS(".access", 0666, &aa_sfs_access),
  2002. AA_SFS_FILE_FOPS(".stacked", 0444, &seq_ns_stacked_fops),
  2003. AA_SFS_FILE_FOPS(".ns_stacked", 0444, &seq_ns_nsstacked_fops),
  2004. AA_SFS_FILE_FOPS(".ns_level", 0444, &seq_ns_level_fops),
  2005. AA_SFS_FILE_FOPS(".ns_name", 0444, &seq_ns_name_fops),
  2006. AA_SFS_FILE_FOPS("profiles", 0444, &aa_sfs_profiles_fops),
  2007. AA_SFS_DIR("features", aa_sfs_entry_features),
  2008. { }
  2009. };
  2010. static struct aa_sfs_entry aa_sfs_entry =
  2011. AA_SFS_DIR("apparmor", aa_sfs_entry_apparmor);
  2012. /**
  2013. * entry_create_file - create a file entry in the apparmor securityfs
  2014. * @fs_file: aa_sfs_entry to build an entry for (NOT NULL)
  2015. * @parent: the parent dentry in the securityfs
  2016. *
  2017. * Use entry_remove_file to remove entries created with this fn.
  2018. */
  2019. static int __init entry_create_file(struct aa_sfs_entry *fs_file,
  2020. struct dentry *parent)
  2021. {
  2022. int error = 0;
  2023. fs_file->dentry = securityfs_create_file(fs_file->name,
  2024. S_IFREG | fs_file->mode,
  2025. parent, fs_file,
  2026. fs_file->file_ops);
  2027. if (IS_ERR(fs_file->dentry)) {
  2028. error = PTR_ERR(fs_file->dentry);
  2029. fs_file->dentry = NULL;
  2030. }
  2031. return error;
  2032. }
  2033. static void __init entry_remove_dir(struct aa_sfs_entry *fs_dir);
  2034. /**
  2035. * entry_create_dir - recursively create a directory entry in the securityfs
  2036. * @fs_dir: aa_sfs_entry (and all child entries) to build (NOT NULL)
  2037. * @parent: the parent dentry in the securityfs
  2038. *
  2039. * Use entry_remove_dir to remove entries created with this fn.
  2040. */
  2041. static int __init entry_create_dir(struct aa_sfs_entry *fs_dir,
  2042. struct dentry *parent)
  2043. {
  2044. struct aa_sfs_entry *fs_file;
  2045. struct dentry *dir;
  2046. int error;
  2047. dir = securityfs_create_dir(fs_dir->name, parent);
  2048. if (IS_ERR(dir))
  2049. return PTR_ERR(dir);
  2050. fs_dir->dentry = dir;
  2051. for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
  2052. if (fs_file->v_type == AA_SFS_TYPE_DIR)
  2053. error = entry_create_dir(fs_file, fs_dir->dentry);
  2054. else
  2055. error = entry_create_file(fs_file, fs_dir->dentry);
  2056. if (error)
  2057. goto failed;
  2058. }
  2059. return 0;
  2060. failed:
  2061. entry_remove_dir(fs_dir);
  2062. return error;
  2063. }
  2064. /**
  2065. * entry_remove_file - drop a single file entry in the apparmor securityfs
  2066. * @fs_file: aa_sfs_entry to detach from the securityfs (NOT NULL)
  2067. */
  2068. static void __init entry_remove_file(struct aa_sfs_entry *fs_file)
  2069. {
  2070. if (!fs_file->dentry)
  2071. return;
  2072. securityfs_remove(fs_file->dentry);
  2073. fs_file->dentry = NULL;
  2074. }
  2075. /**
  2076. * entry_remove_dir - recursively drop a directory entry from the securityfs
  2077. * @fs_dir: aa_sfs_entry (and all child entries) to detach (NOT NULL)
  2078. */
  2079. static void __init entry_remove_dir(struct aa_sfs_entry *fs_dir)
  2080. {
  2081. struct aa_sfs_entry *fs_file;
  2082. for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
  2083. if (fs_file->v_type == AA_SFS_TYPE_DIR)
  2084. entry_remove_dir(fs_file);
  2085. else
  2086. entry_remove_file(fs_file);
  2087. }
  2088. entry_remove_file(fs_dir);
  2089. }
  2090. /**
  2091. * aa_destroy_aafs - cleanup and free aafs
  2092. *
  2093. * releases dentries allocated by aa_create_aafs
  2094. */
  2095. void __init aa_destroy_aafs(void)
  2096. {
  2097. entry_remove_dir(&aa_sfs_entry);
  2098. }
  2099. #define NULL_FILE_NAME ".null"
  2100. struct path aa_null;
  2101. static int aa_mk_null_file(struct dentry *parent)
  2102. {
  2103. struct vfsmount *mount = NULL;
  2104. struct dentry *dentry;
  2105. struct inode *inode;
  2106. int count = 0;
  2107. int error = simple_pin_fs(parent->d_sb->s_type, &mount, &count);
  2108. if (error)
  2109. return error;
  2110. inode_lock(d_inode(parent));
  2111. dentry = lookup_one_len(NULL_FILE_NAME, parent, strlen(NULL_FILE_NAME));
  2112. if (IS_ERR(dentry)) {
  2113. error = PTR_ERR(dentry);
  2114. goto out;
  2115. }
  2116. inode = new_inode(parent->d_inode->i_sb);
  2117. if (!inode) {
  2118. error = -ENOMEM;
  2119. goto out1;
  2120. }
  2121. inode->i_ino = get_next_ino();
  2122. inode->i_mode = S_IFCHR | S_IRUGO | S_IWUGO;
  2123. inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
  2124. init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO,
  2125. MKDEV(MEM_MAJOR, 3));
  2126. d_instantiate(dentry, inode);
  2127. aa_null.dentry = dget(dentry);
  2128. aa_null.mnt = mntget(mount);
  2129. error = 0;
  2130. out1:
  2131. dput(dentry);
  2132. out:
  2133. inode_unlock(d_inode(parent));
  2134. simple_release_fs(&mount, &count);
  2135. return error;
  2136. }
  2137. static const char *policy_get_link(struct dentry *dentry,
  2138. struct inode *inode,
  2139. struct delayed_call *done)
  2140. {
  2141. struct aa_ns *ns;
  2142. struct path path;
  2143. int error;
  2144. if (!dentry)
  2145. return ERR_PTR(-ECHILD);
  2146. ns = aa_get_current_ns();
  2147. path.mnt = mntget(aafs_mnt);
  2148. path.dentry = dget(ns_dir(ns));
  2149. error = nd_jump_link(&path);
  2150. aa_put_ns(ns);
  2151. return ERR_PTR(error);
  2152. }
  2153. static int policy_readlink(struct dentry *dentry, char __user *buffer,
  2154. int buflen)
  2155. {
  2156. char name[32];
  2157. int res;
  2158. res = snprintf(name, sizeof(name), "%s:[%lu]", AAFS_NAME,
  2159. d_inode(dentry)->i_ino);
  2160. if (res > 0 && res < sizeof(name))
  2161. res = readlink_copy(buffer, buflen, name);
  2162. else
  2163. res = -ENOENT;
  2164. return res;
  2165. }
  2166. static const struct inode_operations policy_link_iops = {
  2167. .readlink = policy_readlink,
  2168. .get_link = policy_get_link,
  2169. };
  2170. /**
  2171. * aa_create_aafs - create the apparmor security filesystem
  2172. *
  2173. * dentries created here are released by aa_destroy_aafs
  2174. *
  2175. * Returns: error on failure
  2176. */
  2177. static int __init aa_create_aafs(void)
  2178. {
  2179. struct dentry *dent;
  2180. int error;
  2181. if (!apparmor_initialized)
  2182. return 0;
  2183. if (aa_sfs_entry.dentry) {
  2184. AA_ERROR("%s: AppArmor securityfs already exists\n", __func__);
  2185. return -EEXIST;
  2186. }
  2187. /* setup apparmorfs used to virtualize policy/ */
  2188. aafs_mnt = kern_mount(&aafs_ops);
  2189. if (IS_ERR(aafs_mnt))
  2190. panic("can't set apparmorfs up\n");
  2191. aafs_mnt->mnt_sb->s_flags &= ~SB_NOUSER;
  2192. /* Populate fs tree. */
  2193. error = entry_create_dir(&aa_sfs_entry, NULL);
  2194. if (error)
  2195. goto error;
  2196. dent = securityfs_create_file(".load", 0666, aa_sfs_entry.dentry,
  2197. NULL, &aa_fs_profile_load);
  2198. if (IS_ERR(dent))
  2199. goto dent_error;
  2200. ns_subload(root_ns) = dent;
  2201. dent = securityfs_create_file(".replace", 0666, aa_sfs_entry.dentry,
  2202. NULL, &aa_fs_profile_replace);
  2203. if (IS_ERR(dent))
  2204. goto dent_error;
  2205. ns_subreplace(root_ns) = dent;
  2206. dent = securityfs_create_file(".remove", 0666, aa_sfs_entry.dentry,
  2207. NULL, &aa_fs_profile_remove);
  2208. if (IS_ERR(dent))
  2209. goto dent_error;
  2210. ns_subremove(root_ns) = dent;
  2211. dent = securityfs_create_file("revision", 0444, aa_sfs_entry.dentry,
  2212. NULL, &aa_fs_ns_revision_fops);
  2213. if (IS_ERR(dent))
  2214. goto dent_error;
  2215. ns_subrevision(root_ns) = dent;
  2216. /* policy tree referenced by magic policy symlink */
  2217. mutex_lock_nested(&root_ns->lock, root_ns->level);
  2218. error = __aafs_ns_mkdir(root_ns, aafs_mnt->mnt_root, ".policy",
  2219. aafs_mnt->mnt_root);
  2220. mutex_unlock(&root_ns->lock);
  2221. if (error)
  2222. goto error;
  2223. /* magic symlink similar to nsfs redirects based on task policy */
  2224. dent = securityfs_create_symlink("policy", aa_sfs_entry.dentry,
  2225. NULL, &policy_link_iops);
  2226. if (IS_ERR(dent))
  2227. goto dent_error;
  2228. error = aa_mk_null_file(aa_sfs_entry.dentry);
  2229. if (error)
  2230. goto error;
  2231. /* TODO: add default profile to apparmorfs */
  2232. /* Report that AppArmor fs is enabled */
  2233. aa_info_message("AppArmor Filesystem Enabled");
  2234. return 0;
  2235. dent_error:
  2236. error = PTR_ERR(dent);
  2237. error:
  2238. aa_destroy_aafs();
  2239. AA_ERROR("Error creating AppArmor securityfs\n");
  2240. return error;
  2241. }
  2242. fs_initcall(aa_create_aafs);