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