apparmorfs.c 63 KB

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