apparmorfs.c 61 KB

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