apparmorfs.c 63 KB

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