apparmorfs.c 63 KB

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