apparmorfs.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AppArmor security module
  4. *
  5. * This file contains AppArmor /sys/kernel/security/apparmor interface functions
  6. *
  7. * Copyright (C) 1998-2008 Novell/SUSE
  8. * Copyright 2009-2010 Canonical Ltd.
  9. */
  10. #include <linux/ctype.h>
  11. #include <linux/security.h>
  12. #include <linux/vmalloc.h>
  13. #include <linux/init.h>
  14. #include <linux/seq_file.h>
  15. #include <linux/uaccess.h>
  16. #include <linux/mount.h>
  17. #include <linux/namei.h>
  18. #include <linux/capability.h>
  19. #include <linux/rcupdate.h>
  20. #include <linux/fs.h>
  21. #include <linux/fs_context.h>
  22. #include <linux/poll.h>
  23. #include <linux/zstd.h>
  24. #include <uapi/linux/major.h>
  25. #include <uapi/linux/magic.h>
  26. #include "include/apparmor.h"
  27. #include "include/apparmorfs.h"
  28. #include "include/audit.h"
  29. #include "include/cred.h"
  30. #include "include/crypto.h"
  31. #include "include/ipc.h"
  32. #include "include/label.h"
  33. #include "include/policy.h"
  34. #include "include/policy_ns.h"
  35. #include "include/resource.h"
  36. #include "include/policy_unpack.h"
  37. #include "include/task.h"
  38. /*
  39. * The apparmor filesystem interface used for policy load and introspection
  40. * The interface is split into two main components based on their function
  41. * a securityfs component:
  42. * used for static files that are always available, and which allows
  43. * userspace to specificy the location of the security filesystem.
  44. *
  45. * fns and data are prefixed with
  46. * aa_sfs_
  47. *
  48. * an apparmorfs component:
  49. * used loaded policy content and introspection. It is not part of a
  50. * regular mounted filesystem and is available only through the magic
  51. * policy symlink in the root of the securityfs apparmor/ directory.
  52. * Tasks queries will be magically redirected to the correct portion
  53. * of the policy tree based on their confinement.
  54. *
  55. * fns and data are prefixed with
  56. * aafs_
  57. *
  58. * The aa_fs_ prefix is used to indicate the fn is used by both the
  59. * securityfs and apparmorfs filesystems.
  60. */
  61. /*
  62. * support fns
  63. */
  64. struct rawdata_f_data {
  65. struct aa_loaddata *loaddata;
  66. };
  67. #ifdef CONFIG_SECURITY_APPARMOR_EXPORT_BINARY
  68. #define RAWDATA_F_DATA_BUF(p) (char *)(p + 1)
  69. static void rawdata_f_data_free(struct rawdata_f_data *private)
  70. {
  71. if (!private)
  72. return;
  73. aa_put_loaddata(private->loaddata);
  74. kvfree(private);
  75. }
  76. static struct rawdata_f_data *rawdata_f_data_alloc(size_t size)
  77. {
  78. struct rawdata_f_data *ret;
  79. if (size > SIZE_MAX - sizeof(*ret))
  80. return ERR_PTR(-EINVAL);
  81. ret = kvzalloc(sizeof(*ret) + size, GFP_KERNEL);
  82. if (!ret)
  83. return ERR_PTR(-ENOMEM);
  84. return ret;
  85. }
  86. #endif
  87. /**
  88. * mangle_name - mangle a profile name to std profile layout form
  89. * @name: profile name to mangle (NOT NULL)
  90. * @target: buffer to store mangled name, same length as @name (MAYBE NULL)
  91. *
  92. * Returns: length of mangled name
  93. */
  94. static int mangle_name(const char *name, char *target)
  95. {
  96. char *t = target;
  97. while (*name == '/' || *name == '.')
  98. name++;
  99. if (target) {
  100. for (; *name; name++) {
  101. if (*name == '/')
  102. *(t)++ = '.';
  103. else if (isspace(*name))
  104. *(t)++ = '_';
  105. else if (isalnum(*name) || strchr("._-", *name))
  106. *(t)++ = *name;
  107. }
  108. *t = 0;
  109. } else {
  110. int len = 0;
  111. for (; *name; name++) {
  112. if (isalnum(*name) || isspace(*name) ||
  113. strchr("/._-", *name))
  114. len++;
  115. }
  116. return len;
  117. }
  118. return t - target;
  119. }
  120. /*
  121. * aafs - core fns and data for the policy tree
  122. */
  123. #define AAFS_NAME "apparmorfs"
  124. static struct vfsmount *aafs_mnt;
  125. static int aafs_count;
  126. static int aafs_show_path(struct seq_file *seq, struct dentry *dentry)
  127. {
  128. seq_printf(seq, "%s:[%lu]", AAFS_NAME, d_inode(dentry)->i_ino);
  129. return 0;
  130. }
  131. static void aafs_free_inode(struct inode *inode)
  132. {
  133. if (S_ISLNK(inode->i_mode))
  134. kfree(inode->i_link);
  135. free_inode_nonrcu(inode);
  136. }
  137. static const struct super_operations aafs_super_ops = {
  138. .statfs = simple_statfs,
  139. .free_inode = aafs_free_inode,
  140. .show_path = aafs_show_path,
  141. };
  142. static int apparmorfs_fill_super(struct super_block *sb, struct fs_context *fc)
  143. {
  144. static struct tree_descr files[] = { {""} };
  145. int error;
  146. error = simple_fill_super(sb, AAFS_MAGIC, files);
  147. if (error)
  148. return error;
  149. sb->s_op = &aafs_super_ops;
  150. return 0;
  151. }
  152. static int apparmorfs_get_tree(struct fs_context *fc)
  153. {
  154. return get_tree_single(fc, apparmorfs_fill_super);
  155. }
  156. static const struct fs_context_operations apparmorfs_context_ops = {
  157. .get_tree = apparmorfs_get_tree,
  158. };
  159. static int apparmorfs_init_fs_context(struct fs_context *fc)
  160. {
  161. fc->ops = &apparmorfs_context_ops;
  162. return 0;
  163. }
  164. static struct file_system_type aafs_ops = {
  165. .owner = THIS_MODULE,
  166. .name = AAFS_NAME,
  167. .init_fs_context = apparmorfs_init_fs_context,
  168. .kill_sb = kill_anon_super,
  169. };
  170. /**
  171. * __aafs_setup_d_inode - basic inode setup for apparmorfs
  172. * @dir: parent directory for the dentry
  173. * @dentry: dentry we are seting the inode up for
  174. * @mode: permissions the file should have
  175. * @data: data to store on inode.i_private, available in open()
  176. * @link: if symlink, symlink target string
  177. * @fops: struct file_operations that should be used
  178. * @iops: struct of inode_operations that should be used
  179. */
  180. static int __aafs_setup_d_inode(struct inode *dir, struct dentry *dentry,
  181. umode_t mode, void *data, char *link,
  182. const struct file_operations *fops,
  183. const struct inode_operations *iops)
  184. {
  185. struct inode *inode = new_inode(dir->i_sb);
  186. AA_BUG(!dir);
  187. AA_BUG(!dentry);
  188. if (!inode)
  189. return -ENOMEM;
  190. inode->i_ino = get_next_ino();
  191. inode->i_mode = mode;
  192. simple_inode_init_ts(inode);
  193. inode->i_private = data;
  194. if (S_ISDIR(mode)) {
  195. inode->i_op = iops ? iops : &simple_dir_inode_operations;
  196. inode->i_fop = &simple_dir_operations;
  197. inc_nlink(inode);
  198. inc_nlink(dir);
  199. } else if (S_ISLNK(mode)) {
  200. inode->i_op = iops ? iops : &simple_symlink_inode_operations;
  201. inode->i_link = link;
  202. } else {
  203. inode->i_fop = fops;
  204. }
  205. d_instantiate(dentry, inode);
  206. dget(dentry);
  207. return 0;
  208. }
  209. /**
  210. * aafs_create - create a dentry in the apparmorfs filesystem
  211. *
  212. * @name: name of dentry to create
  213. * @mode: permissions the file should have
  214. * @parent: parent directory for this dentry
  215. * @data: data to store on inode.i_private, available in open()
  216. * @link: if symlink, symlink target string
  217. * @fops: struct file_operations that should be used for
  218. * @iops: struct of inode_operations that should be used
  219. *
  220. * This is the basic "create a xxx" function for apparmorfs.
  221. *
  222. * Returns a pointer to a dentry if it succeeds, that must be free with
  223. * aafs_remove(). Will return ERR_PTR on failure.
  224. */
  225. static struct dentry *aafs_create(const char *name, umode_t mode,
  226. struct dentry *parent, void *data, void *link,
  227. const struct file_operations *fops,
  228. const struct inode_operations *iops)
  229. {
  230. struct dentry *dentry;
  231. struct inode *dir;
  232. int error;
  233. AA_BUG(!name);
  234. AA_BUG(!parent);
  235. if (!(mode & S_IFMT))
  236. mode = (mode & S_IALLUGO) | S_IFREG;
  237. error = simple_pin_fs(&aafs_ops, &aafs_mnt, &aafs_count);
  238. if (error)
  239. return ERR_PTR(error);
  240. dir = d_inode(parent);
  241. inode_lock(dir);
  242. dentry = lookup_one_len(name, parent, strlen(name));
  243. if (IS_ERR(dentry)) {
  244. error = PTR_ERR(dentry);
  245. goto fail_lock;
  246. }
  247. if (d_really_is_positive(dentry)) {
  248. error = -EEXIST;
  249. goto fail_dentry;
  250. }
  251. error = __aafs_setup_d_inode(dir, dentry, mode, data, link, fops, iops);
  252. if (error)
  253. goto fail_dentry;
  254. inode_unlock(dir);
  255. return dentry;
  256. fail_dentry:
  257. dput(dentry);
  258. fail_lock:
  259. inode_unlock(dir);
  260. simple_release_fs(&aafs_mnt, &aafs_count);
  261. return ERR_PTR(error);
  262. }
  263. /**
  264. * aafs_create_file - create a file in the apparmorfs filesystem
  265. *
  266. * @name: name of dentry to create
  267. * @mode: permissions the file should have
  268. * @parent: parent directory for this dentry
  269. * @data: data to store on inode.i_private, available in open()
  270. * @fops: struct file_operations that should be used for
  271. *
  272. * see aafs_create
  273. */
  274. static struct dentry *aafs_create_file(const char *name, umode_t mode,
  275. struct dentry *parent, void *data,
  276. const struct file_operations *fops)
  277. {
  278. return aafs_create(name, mode, parent, data, NULL, fops, NULL);
  279. }
  280. /**
  281. * aafs_create_dir - create a directory in the apparmorfs filesystem
  282. *
  283. * @name: name of dentry to create
  284. * @parent: parent directory for this dentry
  285. *
  286. * see aafs_create
  287. */
  288. static struct dentry *aafs_create_dir(const char *name, struct dentry *parent)
  289. {
  290. return aafs_create(name, S_IFDIR | 0755, parent, NULL, NULL, NULL,
  291. NULL);
  292. }
  293. /**
  294. * aafs_remove - removes a file or directory from the apparmorfs filesystem
  295. *
  296. * @dentry: dentry of the file/directory/symlink to removed.
  297. */
  298. static void aafs_remove(struct dentry *dentry)
  299. {
  300. struct inode *dir;
  301. if (!dentry || IS_ERR(dentry))
  302. return;
  303. dir = d_inode(dentry->d_parent);
  304. inode_lock(dir);
  305. if (simple_positive(dentry)) {
  306. if (d_is_dir(dentry))
  307. simple_rmdir(dir, dentry);
  308. else
  309. simple_unlink(dir, dentry);
  310. d_delete(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. aa_put_loaddata(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(current_cred(), label, ns, mask);
  361. if (error)
  362. goto end_section;
  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_section:
  370. end_current_label_crit_section(label);
  371. return error;
  372. }
  373. /* .load file hook fn to load policy */
  374. static ssize_t profile_load(struct file *f, const char __user *buf, size_t size,
  375. loff_t *pos)
  376. {
  377. struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
  378. int error = policy_update(AA_MAY_LOAD_POLICY, buf, size, pos, ns);
  379. aa_put_ns(ns);
  380. return error;
  381. }
  382. static const struct file_operations aa_fs_profile_load = {
  383. .write = profile_load,
  384. .llseek = default_llseek,
  385. };
  386. /* .replace file hook fn to load and/or replace policy */
  387. static ssize_t profile_replace(struct file *f, const char __user *buf,
  388. size_t size, loff_t *pos)
  389. {
  390. struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
  391. int error = policy_update(AA_MAY_LOAD_POLICY | AA_MAY_REPLACE_POLICY,
  392. buf, size, pos, ns);
  393. aa_put_ns(ns);
  394. return error;
  395. }
  396. static const struct file_operations aa_fs_profile_replace = {
  397. .write = profile_replace,
  398. .llseek = default_llseek,
  399. };
  400. /* .remove file hook fn to remove loaded policy */
  401. static ssize_t profile_remove(struct file *f, const char __user *buf,
  402. size_t size, loff_t *pos)
  403. {
  404. struct aa_loaddata *data;
  405. struct aa_label *label;
  406. ssize_t error;
  407. struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
  408. label = begin_current_label_crit_section();
  409. /* high level check about policy management - fine grained in
  410. * below after unpack
  411. */
  412. error = aa_may_manage_policy(current_cred(), label, ns,
  413. AA_MAY_REMOVE_POLICY);
  414. if (error)
  415. goto out;
  416. /*
  417. * aa_remove_profile needs a null terminated string so 1 extra
  418. * byte is allocated and the copied data is null terminated.
  419. */
  420. data = aa_simple_write_to_buffer(buf, size + 1, size, pos);
  421. error = PTR_ERR(data);
  422. if (!IS_ERR(data)) {
  423. data->data[size] = 0;
  424. error = aa_remove_profiles(ns, label, data->data, size);
  425. aa_put_loaddata(data);
  426. }
  427. out:
  428. end_current_label_crit_section(label);
  429. aa_put_ns(ns);
  430. return error;
  431. }
  432. static const struct file_operations aa_fs_profile_remove = {
  433. .write = profile_remove,
  434. .llseek = default_llseek,
  435. };
  436. struct aa_revision {
  437. struct aa_ns *ns;
  438. long last_read;
  439. };
  440. /* revision file hook fn for policy loads */
  441. static int ns_revision_release(struct inode *inode, struct file *file)
  442. {
  443. struct aa_revision *rev = file->private_data;
  444. if (rev) {
  445. aa_put_ns(rev->ns);
  446. kfree(rev);
  447. }
  448. return 0;
  449. }
  450. static ssize_t ns_revision_read(struct file *file, char __user *buf,
  451. size_t size, loff_t *ppos)
  452. {
  453. struct aa_revision *rev = file->private_data;
  454. char buffer[32];
  455. long last_read;
  456. int avail;
  457. mutex_lock_nested(&rev->ns->lock, rev->ns->level);
  458. last_read = rev->last_read;
  459. if (last_read == rev->ns->revision) {
  460. mutex_unlock(&rev->ns->lock);
  461. if (file->f_flags & O_NONBLOCK)
  462. return -EAGAIN;
  463. if (wait_event_interruptible(rev->ns->wait,
  464. last_read !=
  465. READ_ONCE(rev->ns->revision)))
  466. return -ERESTARTSYS;
  467. mutex_lock_nested(&rev->ns->lock, rev->ns->level);
  468. }
  469. avail = sprintf(buffer, "%ld\n", rev->ns->revision);
  470. if (*ppos + size > avail) {
  471. rev->last_read = rev->ns->revision;
  472. *ppos = 0;
  473. }
  474. mutex_unlock(&rev->ns->lock);
  475. return simple_read_from_buffer(buf, size, ppos, buffer, avail);
  476. }
  477. static int ns_revision_open(struct inode *inode, struct file *file)
  478. {
  479. struct aa_revision *rev = kzalloc(sizeof(*rev), GFP_KERNEL);
  480. if (!rev)
  481. return -ENOMEM;
  482. rev->ns = aa_get_ns(inode->i_private);
  483. if (!rev->ns)
  484. rev->ns = aa_get_current_ns();
  485. file->private_data = rev;
  486. return 0;
  487. }
  488. static __poll_t ns_revision_poll(struct file *file, poll_table *pt)
  489. {
  490. struct aa_revision *rev = file->private_data;
  491. __poll_t mask = 0;
  492. if (rev) {
  493. mutex_lock_nested(&rev->ns->lock, rev->ns->level);
  494. poll_wait(file, &rev->ns->wait, pt);
  495. if (rev->last_read < rev->ns->revision)
  496. mask |= EPOLLIN | EPOLLRDNORM;
  497. mutex_unlock(&rev->ns->lock);
  498. }
  499. return mask;
  500. }
  501. void __aa_bump_ns_revision(struct aa_ns *ns)
  502. {
  503. WRITE_ONCE(ns->revision, READ_ONCE(ns->revision) + 1);
  504. wake_up_interruptible(&ns->wait);
  505. }
  506. static const struct file_operations aa_fs_ns_revision_fops = {
  507. .owner = THIS_MODULE,
  508. .open = ns_revision_open,
  509. .poll = ns_revision_poll,
  510. .read = ns_revision_read,
  511. .llseek = generic_file_llseek,
  512. .release = ns_revision_release,
  513. };
  514. static void profile_query_cb(struct aa_profile *profile, struct aa_perms *perms,
  515. const char *match_str, size_t match_len)
  516. {
  517. struct aa_ruleset *rules = list_first_entry(&profile->rules,
  518. typeof(*rules), list);
  519. struct aa_perms tmp = { };
  520. aa_state_t state = DFA_NOMATCH;
  521. if (profile_unconfined(profile))
  522. return;
  523. if (rules->file->dfa && *match_str == AA_CLASS_FILE) {
  524. state = aa_dfa_match_len(rules->file->dfa,
  525. rules->file->start[AA_CLASS_FILE],
  526. match_str + 1, match_len - 1);
  527. if (state) {
  528. struct path_cond cond = { };
  529. tmp = *(aa_lookup_fperms(rules->file, state, &cond));
  530. }
  531. } else if (rules->policy->dfa) {
  532. if (!RULE_MEDIATES(rules, *match_str))
  533. return; /* no change to current perms */
  534. state = aa_dfa_match_len(rules->policy->dfa,
  535. rules->policy->start[0],
  536. match_str, match_len);
  537. if (state)
  538. tmp = *aa_lookup_perms(rules->policy, state);
  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[];
  697. };
  698. #define MULTI_TRANSACTION_LIMIT (PAGE_SIZE - sizeof(struct multi_transaction))
  699. static void multi_transaction_kref(struct kref *kref)
  700. {
  701. struct multi_transaction *t;
  702. t = container_of(kref, struct multi_transaction, count);
  703. free_page((unsigned long) t);
  704. }
  705. static struct multi_transaction *
  706. get_multi_transaction(struct multi_transaction *t)
  707. {
  708. if (t)
  709. kref_get(&(t->count));
  710. return t;
  711. }
  712. static void put_multi_transaction(struct multi_transaction *t)
  713. {
  714. if (t)
  715. kref_put(&(t->count), multi_transaction_kref);
  716. }
  717. /* does not increment @new's count */
  718. static void multi_transaction_set(struct file *file,
  719. struct multi_transaction *new, size_t n)
  720. {
  721. struct multi_transaction *old;
  722. AA_BUG(n > MULTI_TRANSACTION_LIMIT);
  723. new->size = n;
  724. spin_lock(&file->f_lock);
  725. old = (struct multi_transaction *) file->private_data;
  726. file->private_data = new;
  727. spin_unlock(&file->f_lock);
  728. put_multi_transaction(old);
  729. }
  730. static struct multi_transaction *multi_transaction_new(struct file *file,
  731. const char __user *buf,
  732. size_t size)
  733. {
  734. struct multi_transaction *t;
  735. if (size > MULTI_TRANSACTION_LIMIT - 1)
  736. return ERR_PTR(-EFBIG);
  737. t = (struct multi_transaction *)get_zeroed_page(GFP_KERNEL);
  738. if (!t)
  739. return ERR_PTR(-ENOMEM);
  740. kref_init(&t->count);
  741. if (copy_from_user(t->data, buf, size)) {
  742. put_multi_transaction(t);
  743. return ERR_PTR(-EFAULT);
  744. }
  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(&file->f_lock);
  753. t = get_multi_transaction(file->private_data);
  754. spin_unlock(&file->f_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.xmatch_str)
  932. seq_printf(seq, "%s\n", profile->attach.xmatch_str);
  933. else if (profile->attach.xmatch->dfa)
  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. static int seq_ns_compress_min_show(struct seq_file *seq, void *v)
  1018. {
  1019. seq_printf(seq, "%d\n", AA_MIN_CLEVEL);
  1020. return 0;
  1021. }
  1022. static int seq_ns_compress_max_show(struct seq_file *seq, void *v)
  1023. {
  1024. seq_printf(seq, "%d\n", AA_MAX_CLEVEL);
  1025. return 0;
  1026. }
  1027. SEQ_NS_FOPS(stacked);
  1028. SEQ_NS_FOPS(nsstacked);
  1029. SEQ_NS_FOPS(level);
  1030. SEQ_NS_FOPS(name);
  1031. SEQ_NS_FOPS(compress_min);
  1032. SEQ_NS_FOPS(compress_max);
  1033. /* policy/raw_data/ * file ops */
  1034. #ifdef CONFIG_SECURITY_APPARMOR_EXPORT_BINARY
  1035. #define SEQ_RAWDATA_FOPS(NAME) \
  1036. static int seq_rawdata_ ##NAME ##_open(struct inode *inode, struct file *file)\
  1037. { \
  1038. return seq_rawdata_open(inode, file, seq_rawdata_ ##NAME ##_show); \
  1039. } \
  1040. \
  1041. static const struct file_operations seq_rawdata_ ##NAME ##_fops = { \
  1042. .owner = THIS_MODULE, \
  1043. .open = seq_rawdata_ ##NAME ##_open, \
  1044. .read = seq_read, \
  1045. .llseek = seq_lseek, \
  1046. .release = seq_rawdata_release, \
  1047. } \
  1048. static int seq_rawdata_open(struct inode *inode, struct file *file,
  1049. int (*show)(struct seq_file *, void *))
  1050. {
  1051. struct aa_loaddata *data = __aa_get_loaddata(inode->i_private);
  1052. int error;
  1053. if (!data)
  1054. /* lost race this ent is being reaped */
  1055. return -ENOENT;
  1056. error = single_open(file, show, data);
  1057. if (error) {
  1058. AA_BUG(file->private_data &&
  1059. ((struct seq_file *)file->private_data)->private);
  1060. aa_put_loaddata(data);
  1061. }
  1062. return error;
  1063. }
  1064. static int seq_rawdata_release(struct inode *inode, struct file *file)
  1065. {
  1066. struct seq_file *seq = (struct seq_file *) file->private_data;
  1067. if (seq)
  1068. aa_put_loaddata(seq->private);
  1069. return single_release(inode, file);
  1070. }
  1071. static int seq_rawdata_abi_show(struct seq_file *seq, void *v)
  1072. {
  1073. struct aa_loaddata *data = seq->private;
  1074. seq_printf(seq, "v%d\n", data->abi);
  1075. return 0;
  1076. }
  1077. static int seq_rawdata_revision_show(struct seq_file *seq, void *v)
  1078. {
  1079. struct aa_loaddata *data = seq->private;
  1080. seq_printf(seq, "%ld\n", data->revision);
  1081. return 0;
  1082. }
  1083. static int seq_rawdata_hash_show(struct seq_file *seq, void *v)
  1084. {
  1085. struct aa_loaddata *data = seq->private;
  1086. unsigned int i, size = aa_hash_size();
  1087. if (data->hash) {
  1088. for (i = 0; i < size; i++)
  1089. seq_printf(seq, "%.2x", data->hash[i]);
  1090. seq_putc(seq, '\n');
  1091. }
  1092. return 0;
  1093. }
  1094. static int seq_rawdata_compressed_size_show(struct seq_file *seq, void *v)
  1095. {
  1096. struct aa_loaddata *data = seq->private;
  1097. seq_printf(seq, "%zu\n", data->compressed_size);
  1098. return 0;
  1099. }
  1100. SEQ_RAWDATA_FOPS(abi);
  1101. SEQ_RAWDATA_FOPS(revision);
  1102. SEQ_RAWDATA_FOPS(hash);
  1103. SEQ_RAWDATA_FOPS(compressed_size);
  1104. static int decompress_zstd(char *src, size_t slen, char *dst, size_t dlen)
  1105. {
  1106. if (slen < dlen) {
  1107. const size_t wksp_len = zstd_dctx_workspace_bound();
  1108. zstd_dctx *ctx;
  1109. void *wksp;
  1110. size_t out_len;
  1111. int ret = 0;
  1112. wksp = kvzalloc(wksp_len, GFP_KERNEL);
  1113. if (!wksp) {
  1114. ret = -ENOMEM;
  1115. goto cleanup;
  1116. }
  1117. ctx = zstd_init_dctx(wksp, wksp_len);
  1118. if (ctx == NULL) {
  1119. ret = -ENOMEM;
  1120. goto cleanup;
  1121. }
  1122. out_len = zstd_decompress_dctx(ctx, dst, dlen, src, slen);
  1123. if (zstd_is_error(out_len)) {
  1124. ret = -EINVAL;
  1125. goto cleanup;
  1126. }
  1127. cleanup:
  1128. kvfree(wksp);
  1129. return ret;
  1130. }
  1131. if (dlen < slen)
  1132. return -EINVAL;
  1133. memcpy(dst, src, slen);
  1134. return 0;
  1135. }
  1136. static ssize_t rawdata_read(struct file *file, char __user *buf, size_t size,
  1137. loff_t *ppos)
  1138. {
  1139. struct rawdata_f_data *private = file->private_data;
  1140. return simple_read_from_buffer(buf, size, ppos,
  1141. RAWDATA_F_DATA_BUF(private),
  1142. private->loaddata->size);
  1143. }
  1144. static int rawdata_release(struct inode *inode, struct file *file)
  1145. {
  1146. rawdata_f_data_free(file->private_data);
  1147. return 0;
  1148. }
  1149. static int rawdata_open(struct inode *inode, struct file *file)
  1150. {
  1151. int error;
  1152. struct aa_loaddata *loaddata;
  1153. struct rawdata_f_data *private;
  1154. if (!aa_current_policy_view_capable(NULL))
  1155. return -EACCES;
  1156. loaddata = __aa_get_loaddata(inode->i_private);
  1157. if (!loaddata)
  1158. /* lost race: this entry is being reaped */
  1159. return -ENOENT;
  1160. private = rawdata_f_data_alloc(loaddata->size);
  1161. if (IS_ERR(private)) {
  1162. error = PTR_ERR(private);
  1163. goto fail_private_alloc;
  1164. }
  1165. private->loaddata = loaddata;
  1166. error = decompress_zstd(loaddata->data, loaddata->compressed_size,
  1167. RAWDATA_F_DATA_BUF(private),
  1168. loaddata->size);
  1169. if (error)
  1170. goto fail_decompress;
  1171. file->private_data = private;
  1172. return 0;
  1173. fail_decompress:
  1174. rawdata_f_data_free(private);
  1175. return error;
  1176. fail_private_alloc:
  1177. aa_put_loaddata(loaddata);
  1178. return error;
  1179. }
  1180. static const struct file_operations rawdata_fops = {
  1181. .open = rawdata_open,
  1182. .read = rawdata_read,
  1183. .llseek = generic_file_llseek,
  1184. .release = rawdata_release,
  1185. };
  1186. static void remove_rawdata_dents(struct aa_loaddata *rawdata)
  1187. {
  1188. int i;
  1189. for (i = 0; i < AAFS_LOADDATA_NDENTS; i++) {
  1190. if (!IS_ERR_OR_NULL(rawdata->dents[i])) {
  1191. /* no refcounts on i_private */
  1192. aafs_remove(rawdata->dents[i]);
  1193. rawdata->dents[i] = NULL;
  1194. }
  1195. }
  1196. }
  1197. void __aa_fs_remove_rawdata(struct aa_loaddata *rawdata)
  1198. {
  1199. AA_BUG(rawdata->ns && !mutex_is_locked(&rawdata->ns->lock));
  1200. if (rawdata->ns) {
  1201. remove_rawdata_dents(rawdata);
  1202. list_del_init(&rawdata->list);
  1203. aa_put_ns(rawdata->ns);
  1204. rawdata->ns = NULL;
  1205. }
  1206. }
  1207. int __aa_fs_create_rawdata(struct aa_ns *ns, struct aa_loaddata *rawdata)
  1208. {
  1209. struct dentry *dent, *dir;
  1210. AA_BUG(!ns);
  1211. AA_BUG(!rawdata);
  1212. AA_BUG(!mutex_is_locked(&ns->lock));
  1213. AA_BUG(!ns_subdata_dir(ns));
  1214. /*
  1215. * just use ns revision dir was originally created at. This is
  1216. * under ns->lock and if load is successful revision will be
  1217. * bumped and is guaranteed to be unique
  1218. */
  1219. rawdata->name = kasprintf(GFP_KERNEL, "%ld", ns->revision);
  1220. if (!rawdata->name)
  1221. return -ENOMEM;
  1222. dir = aafs_create_dir(rawdata->name, ns_subdata_dir(ns));
  1223. if (IS_ERR(dir))
  1224. /* ->name freed when rawdata freed */
  1225. return PTR_ERR(dir);
  1226. rawdata->dents[AAFS_LOADDATA_DIR] = dir;
  1227. dent = aafs_create_file("abi", S_IFREG | 0444, dir, rawdata,
  1228. &seq_rawdata_abi_fops);
  1229. if (IS_ERR(dent))
  1230. goto fail;
  1231. rawdata->dents[AAFS_LOADDATA_ABI] = dent;
  1232. dent = aafs_create_file("revision", S_IFREG | 0444, dir, rawdata,
  1233. &seq_rawdata_revision_fops);
  1234. if (IS_ERR(dent))
  1235. goto fail;
  1236. rawdata->dents[AAFS_LOADDATA_REVISION] = dent;
  1237. if (aa_g_hash_policy) {
  1238. dent = aafs_create_file("sha256", S_IFREG | 0444, dir,
  1239. rawdata, &seq_rawdata_hash_fops);
  1240. if (IS_ERR(dent))
  1241. goto fail;
  1242. rawdata->dents[AAFS_LOADDATA_HASH] = dent;
  1243. }
  1244. dent = aafs_create_file("compressed_size", S_IFREG | 0444, dir,
  1245. rawdata,
  1246. &seq_rawdata_compressed_size_fops);
  1247. if (IS_ERR(dent))
  1248. goto fail;
  1249. rawdata->dents[AAFS_LOADDATA_COMPRESSED_SIZE] = dent;
  1250. dent = aafs_create_file("raw_data", S_IFREG | 0444,
  1251. dir, rawdata, &rawdata_fops);
  1252. if (IS_ERR(dent))
  1253. goto fail;
  1254. rawdata->dents[AAFS_LOADDATA_DATA] = dent;
  1255. d_inode(dent)->i_size = rawdata->size;
  1256. rawdata->ns = aa_get_ns(ns);
  1257. list_add(&rawdata->list, &ns->rawdata_list);
  1258. /* no refcount on inode rawdata */
  1259. return 0;
  1260. fail:
  1261. remove_rawdata_dents(rawdata);
  1262. return PTR_ERR(dent);
  1263. }
  1264. #endif /* CONFIG_SECURITY_APPARMOR_EXPORT_BINARY */
  1265. /** fns to setup dynamic per profile/namespace files **/
  1266. /*
  1267. *
  1268. * Requires: @profile->ns->lock held
  1269. */
  1270. void __aafs_profile_rmdir(struct aa_profile *profile)
  1271. {
  1272. struct aa_profile *child;
  1273. int i;
  1274. if (!profile)
  1275. return;
  1276. list_for_each_entry(child, &profile->base.profiles, base.list)
  1277. __aafs_profile_rmdir(child);
  1278. for (i = AAFS_PROF_SIZEOF - 1; i >= 0; --i) {
  1279. struct aa_proxy *proxy;
  1280. if (!profile->dents[i])
  1281. continue;
  1282. proxy = d_inode(profile->dents[i])->i_private;
  1283. aafs_remove(profile->dents[i]);
  1284. aa_put_proxy(proxy);
  1285. profile->dents[i] = NULL;
  1286. }
  1287. }
  1288. /*
  1289. *
  1290. * Requires: @old->ns->lock held
  1291. */
  1292. void __aafs_profile_migrate_dents(struct aa_profile *old,
  1293. struct aa_profile *new)
  1294. {
  1295. int i;
  1296. AA_BUG(!old);
  1297. AA_BUG(!new);
  1298. AA_BUG(!mutex_is_locked(&profiles_ns(old)->lock));
  1299. for (i = 0; i < AAFS_PROF_SIZEOF; i++) {
  1300. new->dents[i] = old->dents[i];
  1301. if (new->dents[i]) {
  1302. struct inode *inode = d_inode(new->dents[i]);
  1303. inode_set_mtime_to_ts(inode,
  1304. inode_set_ctime_current(inode));
  1305. }
  1306. old->dents[i] = NULL;
  1307. }
  1308. }
  1309. static struct dentry *create_profile_file(struct dentry *dir, const char *name,
  1310. struct aa_profile *profile,
  1311. const struct file_operations *fops)
  1312. {
  1313. struct aa_proxy *proxy = aa_get_proxy(profile->label.proxy);
  1314. struct dentry *dent;
  1315. dent = aafs_create_file(name, S_IFREG | 0444, dir, proxy, fops);
  1316. if (IS_ERR(dent))
  1317. aa_put_proxy(proxy);
  1318. return dent;
  1319. }
  1320. #ifdef CONFIG_SECURITY_APPARMOR_EXPORT_BINARY
  1321. static int profile_depth(struct aa_profile *profile)
  1322. {
  1323. int depth = 0;
  1324. rcu_read_lock();
  1325. for (depth = 0; profile; profile = rcu_access_pointer(profile->parent))
  1326. depth++;
  1327. rcu_read_unlock();
  1328. return depth;
  1329. }
  1330. static char *gen_symlink_name(int depth, const char *dirname, const char *fname)
  1331. {
  1332. char *buffer, *s;
  1333. int error;
  1334. int size = depth * 6 + strlen(dirname) + strlen(fname) + 11;
  1335. s = buffer = kmalloc(size, GFP_KERNEL);
  1336. if (!buffer)
  1337. return ERR_PTR(-ENOMEM);
  1338. for (; depth > 0; depth--) {
  1339. strcpy(s, "../../");
  1340. s += 6;
  1341. size -= 6;
  1342. }
  1343. error = snprintf(s, size, "raw_data/%s/%s", dirname, fname);
  1344. if (error >= size || error < 0) {
  1345. kfree(buffer);
  1346. return ERR_PTR(-ENAMETOOLONG);
  1347. }
  1348. return buffer;
  1349. }
  1350. static const char *rawdata_get_link_base(struct dentry *dentry,
  1351. struct inode *inode,
  1352. struct delayed_call *done,
  1353. const char *name)
  1354. {
  1355. struct aa_proxy *proxy = inode->i_private;
  1356. struct aa_label *label;
  1357. struct aa_profile *profile;
  1358. char *target;
  1359. int depth;
  1360. if (!dentry)
  1361. return ERR_PTR(-ECHILD);
  1362. label = aa_get_label_rcu(&proxy->label);
  1363. profile = labels_profile(label);
  1364. depth = profile_depth(profile);
  1365. target = gen_symlink_name(depth, profile->rawdata->name, name);
  1366. aa_put_label(label);
  1367. if (IS_ERR(target))
  1368. return target;
  1369. set_delayed_call(done, kfree_link, target);
  1370. return target;
  1371. }
  1372. static const char *rawdata_get_link_sha256(struct dentry *dentry,
  1373. struct inode *inode,
  1374. struct delayed_call *done)
  1375. {
  1376. return rawdata_get_link_base(dentry, inode, done, "sha256");
  1377. }
  1378. static const char *rawdata_get_link_abi(struct dentry *dentry,
  1379. struct inode *inode,
  1380. struct delayed_call *done)
  1381. {
  1382. return rawdata_get_link_base(dentry, inode, done, "abi");
  1383. }
  1384. static const char *rawdata_get_link_data(struct dentry *dentry,
  1385. struct inode *inode,
  1386. struct delayed_call *done)
  1387. {
  1388. return rawdata_get_link_base(dentry, inode, done, "raw_data");
  1389. }
  1390. static const struct inode_operations rawdata_link_sha256_iops = {
  1391. .get_link = rawdata_get_link_sha256,
  1392. };
  1393. static const struct inode_operations rawdata_link_abi_iops = {
  1394. .get_link = rawdata_get_link_abi,
  1395. };
  1396. static const struct inode_operations rawdata_link_data_iops = {
  1397. .get_link = rawdata_get_link_data,
  1398. };
  1399. #endif /* CONFIG_SECURITY_APPARMOR_EXPORT_BINARY */
  1400. /*
  1401. * Requires: @profile->ns->lock held
  1402. */
  1403. int __aafs_profile_mkdir(struct aa_profile *profile, struct dentry *parent)
  1404. {
  1405. struct aa_profile *child;
  1406. struct dentry *dent = NULL, *dir;
  1407. int error;
  1408. AA_BUG(!profile);
  1409. AA_BUG(!mutex_is_locked(&profiles_ns(profile)->lock));
  1410. if (!parent) {
  1411. struct aa_profile *p;
  1412. p = aa_deref_parent(profile);
  1413. dent = prof_dir(p);
  1414. if (!dent) {
  1415. error = -ENOENT;
  1416. goto fail2;
  1417. }
  1418. /* adding to parent that previously didn't have children */
  1419. dent = aafs_create_dir("profiles", dent);
  1420. if (IS_ERR(dent))
  1421. goto fail;
  1422. prof_child_dir(p) = parent = dent;
  1423. }
  1424. if (!profile->dirname) {
  1425. int len, id_len;
  1426. len = mangle_name(profile->base.name, NULL);
  1427. id_len = snprintf(NULL, 0, ".%ld", profile->ns->uniq_id);
  1428. profile->dirname = kmalloc(len + id_len + 1, GFP_KERNEL);
  1429. if (!profile->dirname) {
  1430. error = -ENOMEM;
  1431. goto fail2;
  1432. }
  1433. mangle_name(profile->base.name, profile->dirname);
  1434. sprintf(profile->dirname + len, ".%ld", profile->ns->uniq_id++);
  1435. }
  1436. dent = aafs_create_dir(profile->dirname, parent);
  1437. if (IS_ERR(dent))
  1438. goto fail;
  1439. prof_dir(profile) = dir = dent;
  1440. dent = create_profile_file(dir, "name", profile,
  1441. &seq_profile_name_fops);
  1442. if (IS_ERR(dent))
  1443. goto fail;
  1444. profile->dents[AAFS_PROF_NAME] = dent;
  1445. dent = create_profile_file(dir, "mode", profile,
  1446. &seq_profile_mode_fops);
  1447. if (IS_ERR(dent))
  1448. goto fail;
  1449. profile->dents[AAFS_PROF_MODE] = dent;
  1450. dent = create_profile_file(dir, "attach", profile,
  1451. &seq_profile_attach_fops);
  1452. if (IS_ERR(dent))
  1453. goto fail;
  1454. profile->dents[AAFS_PROF_ATTACH] = dent;
  1455. if (profile->hash) {
  1456. dent = create_profile_file(dir, "sha256", profile,
  1457. &seq_profile_hash_fops);
  1458. if (IS_ERR(dent))
  1459. goto fail;
  1460. profile->dents[AAFS_PROF_HASH] = dent;
  1461. }
  1462. #ifdef CONFIG_SECURITY_APPARMOR_EXPORT_BINARY
  1463. if (profile->rawdata) {
  1464. if (aa_g_hash_policy) {
  1465. dent = aafs_create("raw_sha256", S_IFLNK | 0444, dir,
  1466. profile->label.proxy, NULL, NULL,
  1467. &rawdata_link_sha256_iops);
  1468. if (IS_ERR(dent))
  1469. goto fail;
  1470. aa_get_proxy(profile->label.proxy);
  1471. profile->dents[AAFS_PROF_RAW_HASH] = dent;
  1472. }
  1473. dent = aafs_create("raw_abi", S_IFLNK | 0444, dir,
  1474. profile->label.proxy, NULL, NULL,
  1475. &rawdata_link_abi_iops);
  1476. if (IS_ERR(dent))
  1477. goto fail;
  1478. aa_get_proxy(profile->label.proxy);
  1479. profile->dents[AAFS_PROF_RAW_ABI] = dent;
  1480. dent = aafs_create("raw_data", S_IFLNK | 0444, dir,
  1481. profile->label.proxy, NULL, NULL,
  1482. &rawdata_link_data_iops);
  1483. if (IS_ERR(dent))
  1484. goto fail;
  1485. aa_get_proxy(profile->label.proxy);
  1486. profile->dents[AAFS_PROF_RAW_DATA] = dent;
  1487. }
  1488. #endif /*CONFIG_SECURITY_APPARMOR_EXPORT_BINARY */
  1489. list_for_each_entry(child, &profile->base.profiles, base.list) {
  1490. error = __aafs_profile_mkdir(child, prof_child_dir(profile));
  1491. if (error)
  1492. goto fail2;
  1493. }
  1494. return 0;
  1495. fail:
  1496. error = PTR_ERR(dent);
  1497. fail2:
  1498. __aafs_profile_rmdir(profile);
  1499. return error;
  1500. }
  1501. static int ns_mkdir_op(struct mnt_idmap *idmap, struct inode *dir,
  1502. struct dentry *dentry, umode_t mode)
  1503. {
  1504. struct aa_ns *ns, *parent;
  1505. /* TODO: improve permission check */
  1506. struct aa_label *label;
  1507. int error;
  1508. label = begin_current_label_crit_section();
  1509. error = aa_may_manage_policy(current_cred(), label, NULL,
  1510. AA_MAY_LOAD_POLICY);
  1511. end_current_label_crit_section(label);
  1512. if (error)
  1513. return error;
  1514. parent = aa_get_ns(dir->i_private);
  1515. AA_BUG(d_inode(ns_subns_dir(parent)) != dir);
  1516. /* we have to unlock and then relock to get locking order right
  1517. * for pin_fs
  1518. */
  1519. inode_unlock(dir);
  1520. error = simple_pin_fs(&aafs_ops, &aafs_mnt, &aafs_count);
  1521. mutex_lock_nested(&parent->lock, parent->level);
  1522. inode_lock_nested(dir, I_MUTEX_PARENT);
  1523. if (error)
  1524. goto out;
  1525. error = __aafs_setup_d_inode(dir, dentry, mode | S_IFDIR, NULL,
  1526. NULL, NULL, NULL);
  1527. if (error)
  1528. goto out_pin;
  1529. ns = __aa_find_or_create_ns(parent, READ_ONCE(dentry->d_name.name),
  1530. dentry);
  1531. if (IS_ERR(ns)) {
  1532. error = PTR_ERR(ns);
  1533. ns = NULL;
  1534. }
  1535. aa_put_ns(ns); /* list ref remains */
  1536. out_pin:
  1537. if (error)
  1538. simple_release_fs(&aafs_mnt, &aafs_count);
  1539. out:
  1540. mutex_unlock(&parent->lock);
  1541. aa_put_ns(parent);
  1542. return error;
  1543. }
  1544. static int ns_rmdir_op(struct inode *dir, struct dentry *dentry)
  1545. {
  1546. struct aa_ns *ns, *parent;
  1547. /* TODO: improve permission check */
  1548. struct aa_label *label;
  1549. int error;
  1550. label = begin_current_label_crit_section();
  1551. error = aa_may_manage_policy(current_cred(), label, NULL,
  1552. AA_MAY_LOAD_POLICY);
  1553. end_current_label_crit_section(label);
  1554. if (error)
  1555. return error;
  1556. parent = aa_get_ns(dir->i_private);
  1557. /* rmdir calls the generic securityfs functions to remove files
  1558. * from the apparmor dir. It is up to the apparmor ns locking
  1559. * to avoid races.
  1560. */
  1561. inode_unlock(dir);
  1562. inode_unlock(dentry->d_inode);
  1563. mutex_lock_nested(&parent->lock, parent->level);
  1564. ns = aa_get_ns(__aa_findn_ns(&parent->sub_ns, dentry->d_name.name,
  1565. dentry->d_name.len));
  1566. if (!ns) {
  1567. error = -ENOENT;
  1568. goto out;
  1569. }
  1570. AA_BUG(ns_dir(ns) != dentry);
  1571. __aa_remove_ns(ns);
  1572. aa_put_ns(ns);
  1573. out:
  1574. mutex_unlock(&parent->lock);
  1575. inode_lock_nested(dir, I_MUTEX_PARENT);
  1576. inode_lock(dentry->d_inode);
  1577. aa_put_ns(parent);
  1578. return error;
  1579. }
  1580. static const struct inode_operations ns_dir_inode_operations = {
  1581. .lookup = simple_lookup,
  1582. .mkdir = ns_mkdir_op,
  1583. .rmdir = ns_rmdir_op,
  1584. };
  1585. static void __aa_fs_list_remove_rawdata(struct aa_ns *ns)
  1586. {
  1587. struct aa_loaddata *ent, *tmp;
  1588. AA_BUG(!mutex_is_locked(&ns->lock));
  1589. list_for_each_entry_safe(ent, tmp, &ns->rawdata_list, list)
  1590. __aa_fs_remove_rawdata(ent);
  1591. }
  1592. /*
  1593. *
  1594. * Requires: @ns->lock held
  1595. */
  1596. void __aafs_ns_rmdir(struct aa_ns *ns)
  1597. {
  1598. struct aa_ns *sub;
  1599. struct aa_profile *child;
  1600. int i;
  1601. if (!ns)
  1602. return;
  1603. AA_BUG(!mutex_is_locked(&ns->lock));
  1604. list_for_each_entry(child, &ns->base.profiles, base.list)
  1605. __aafs_profile_rmdir(child);
  1606. list_for_each_entry(sub, &ns->sub_ns, base.list) {
  1607. mutex_lock_nested(&sub->lock, sub->level);
  1608. __aafs_ns_rmdir(sub);
  1609. mutex_unlock(&sub->lock);
  1610. }
  1611. __aa_fs_list_remove_rawdata(ns);
  1612. if (ns_subns_dir(ns)) {
  1613. sub = d_inode(ns_subns_dir(ns))->i_private;
  1614. aa_put_ns(sub);
  1615. }
  1616. if (ns_subload(ns)) {
  1617. sub = d_inode(ns_subload(ns))->i_private;
  1618. aa_put_ns(sub);
  1619. }
  1620. if (ns_subreplace(ns)) {
  1621. sub = d_inode(ns_subreplace(ns))->i_private;
  1622. aa_put_ns(sub);
  1623. }
  1624. if (ns_subremove(ns)) {
  1625. sub = d_inode(ns_subremove(ns))->i_private;
  1626. aa_put_ns(sub);
  1627. }
  1628. if (ns_subrevision(ns)) {
  1629. sub = d_inode(ns_subrevision(ns))->i_private;
  1630. aa_put_ns(sub);
  1631. }
  1632. for (i = AAFS_NS_SIZEOF - 1; i >= 0; --i) {
  1633. aafs_remove(ns->dents[i]);
  1634. ns->dents[i] = NULL;
  1635. }
  1636. }
  1637. /* assumes cleanup in caller */
  1638. static int __aafs_ns_mkdir_entries(struct aa_ns *ns, struct dentry *dir)
  1639. {
  1640. struct dentry *dent;
  1641. AA_BUG(!ns);
  1642. AA_BUG(!dir);
  1643. dent = aafs_create_dir("profiles", dir);
  1644. if (IS_ERR(dent))
  1645. return PTR_ERR(dent);
  1646. ns_subprofs_dir(ns) = dent;
  1647. dent = aafs_create_dir("raw_data", dir);
  1648. if (IS_ERR(dent))
  1649. return PTR_ERR(dent);
  1650. ns_subdata_dir(ns) = dent;
  1651. dent = aafs_create_file("revision", 0444, dir, ns,
  1652. &aa_fs_ns_revision_fops);
  1653. if (IS_ERR(dent))
  1654. return PTR_ERR(dent);
  1655. aa_get_ns(ns);
  1656. ns_subrevision(ns) = dent;
  1657. dent = aafs_create_file(".load", 0640, dir, ns,
  1658. &aa_fs_profile_load);
  1659. if (IS_ERR(dent))
  1660. return PTR_ERR(dent);
  1661. aa_get_ns(ns);
  1662. ns_subload(ns) = dent;
  1663. dent = aafs_create_file(".replace", 0640, dir, ns,
  1664. &aa_fs_profile_replace);
  1665. if (IS_ERR(dent))
  1666. return PTR_ERR(dent);
  1667. aa_get_ns(ns);
  1668. ns_subreplace(ns) = dent;
  1669. dent = aafs_create_file(".remove", 0640, dir, ns,
  1670. &aa_fs_profile_remove);
  1671. if (IS_ERR(dent))
  1672. return PTR_ERR(dent);
  1673. aa_get_ns(ns);
  1674. ns_subremove(ns) = dent;
  1675. /* use create_dentry so we can supply private data */
  1676. dent = aafs_create("namespaces", S_IFDIR | 0755, dir, ns, NULL, NULL,
  1677. &ns_dir_inode_operations);
  1678. if (IS_ERR(dent))
  1679. return PTR_ERR(dent);
  1680. aa_get_ns(ns);
  1681. ns_subns_dir(ns) = dent;
  1682. return 0;
  1683. }
  1684. /*
  1685. * Requires: @ns->lock held
  1686. */
  1687. int __aafs_ns_mkdir(struct aa_ns *ns, struct dentry *parent, const char *name,
  1688. struct dentry *dent)
  1689. {
  1690. struct aa_ns *sub;
  1691. struct aa_profile *child;
  1692. struct dentry *dir;
  1693. int error;
  1694. AA_BUG(!ns);
  1695. AA_BUG(!parent);
  1696. AA_BUG(!mutex_is_locked(&ns->lock));
  1697. if (!name)
  1698. name = ns->base.name;
  1699. if (!dent) {
  1700. /* create ns dir if it doesn't already exist */
  1701. dent = aafs_create_dir(name, parent);
  1702. if (IS_ERR(dent))
  1703. goto fail;
  1704. } else
  1705. dget(dent);
  1706. ns_dir(ns) = dir = dent;
  1707. error = __aafs_ns_mkdir_entries(ns, dir);
  1708. if (error)
  1709. goto fail2;
  1710. /* profiles */
  1711. list_for_each_entry(child, &ns->base.profiles, base.list) {
  1712. error = __aafs_profile_mkdir(child, ns_subprofs_dir(ns));
  1713. if (error)
  1714. goto fail2;
  1715. }
  1716. /* subnamespaces */
  1717. list_for_each_entry(sub, &ns->sub_ns, base.list) {
  1718. mutex_lock_nested(&sub->lock, sub->level);
  1719. error = __aafs_ns_mkdir(sub, ns_subns_dir(ns), NULL, NULL);
  1720. mutex_unlock(&sub->lock);
  1721. if (error)
  1722. goto fail2;
  1723. }
  1724. return 0;
  1725. fail:
  1726. error = PTR_ERR(dent);
  1727. fail2:
  1728. __aafs_ns_rmdir(ns);
  1729. return error;
  1730. }
  1731. /**
  1732. * __next_ns - find the next namespace to list
  1733. * @root: root namespace to stop search at (NOT NULL)
  1734. * @ns: current ns position (NOT NULL)
  1735. *
  1736. * Find the next namespace from @ns under @root and handle all locking needed
  1737. * while switching current namespace.
  1738. *
  1739. * Returns: next namespace or NULL if at last namespace under @root
  1740. * Requires: ns->parent->lock to be held
  1741. * NOTE: will not unlock root->lock
  1742. */
  1743. static struct aa_ns *__next_ns(struct aa_ns *root, struct aa_ns *ns)
  1744. {
  1745. struct aa_ns *parent, *next;
  1746. AA_BUG(!root);
  1747. AA_BUG(!ns);
  1748. AA_BUG(ns != root && !mutex_is_locked(&ns->parent->lock));
  1749. /* is next namespace a child */
  1750. if (!list_empty(&ns->sub_ns)) {
  1751. next = list_first_entry(&ns->sub_ns, typeof(*ns), base.list);
  1752. mutex_lock_nested(&next->lock, next->level);
  1753. return next;
  1754. }
  1755. /* check if the next ns is a sibling, parent, gp, .. */
  1756. parent = ns->parent;
  1757. while (ns != root) {
  1758. mutex_unlock(&ns->lock);
  1759. next = list_next_entry(ns, base.list);
  1760. if (!list_entry_is_head(next, &parent->sub_ns, base.list)) {
  1761. mutex_lock_nested(&next->lock, next->level);
  1762. return next;
  1763. }
  1764. ns = parent;
  1765. parent = parent->parent;
  1766. }
  1767. return NULL;
  1768. }
  1769. /**
  1770. * __first_profile - find the first profile in a namespace
  1771. * @root: namespace that is root of profiles being displayed (NOT NULL)
  1772. * @ns: namespace to start in (NOT NULL)
  1773. *
  1774. * Returns: unrefcounted profile or NULL if no profile
  1775. * Requires: profile->ns.lock to be held
  1776. */
  1777. static struct aa_profile *__first_profile(struct aa_ns *root,
  1778. struct aa_ns *ns)
  1779. {
  1780. AA_BUG(!root);
  1781. AA_BUG(ns && !mutex_is_locked(&ns->lock));
  1782. for (; ns; ns = __next_ns(root, ns)) {
  1783. if (!list_empty(&ns->base.profiles))
  1784. return list_first_entry(&ns->base.profiles,
  1785. struct aa_profile, base.list);
  1786. }
  1787. return NULL;
  1788. }
  1789. /**
  1790. * __next_profile - step to the next profile in a profile tree
  1791. * @p: current profile in tree (NOT NULL)
  1792. *
  1793. * Perform a depth first traversal on the profile tree in a namespace
  1794. *
  1795. * Returns: next profile or NULL if done
  1796. * Requires: profile->ns.lock to be held
  1797. */
  1798. static struct aa_profile *__next_profile(struct aa_profile *p)
  1799. {
  1800. struct aa_profile *parent;
  1801. struct aa_ns *ns = p->ns;
  1802. AA_BUG(!mutex_is_locked(&profiles_ns(p)->lock));
  1803. /* is next profile a child */
  1804. if (!list_empty(&p->base.profiles))
  1805. return list_first_entry(&p->base.profiles, typeof(*p),
  1806. base.list);
  1807. /* is next profile a sibling, parent sibling, gp, sibling, .. */
  1808. parent = rcu_dereference_protected(p->parent,
  1809. mutex_is_locked(&p->ns->lock));
  1810. while (parent) {
  1811. p = list_next_entry(p, base.list);
  1812. if (!list_entry_is_head(p, &parent->base.profiles, base.list))
  1813. return p;
  1814. p = parent;
  1815. parent = rcu_dereference_protected(parent->parent,
  1816. mutex_is_locked(&parent->ns->lock));
  1817. }
  1818. /* is next another profile in the namespace */
  1819. p = list_next_entry(p, base.list);
  1820. if (!list_entry_is_head(p, &ns->base.profiles, base.list))
  1821. return p;
  1822. return NULL;
  1823. }
  1824. /**
  1825. * next_profile - step to the next profile in where ever it may be
  1826. * @root: root namespace (NOT NULL)
  1827. * @profile: current profile (NOT NULL)
  1828. *
  1829. * Returns: next profile or NULL if there isn't one
  1830. */
  1831. static struct aa_profile *next_profile(struct aa_ns *root,
  1832. struct aa_profile *profile)
  1833. {
  1834. struct aa_profile *next = __next_profile(profile);
  1835. if (next)
  1836. return next;
  1837. /* finished all profiles in namespace move to next namespace */
  1838. return __first_profile(root, __next_ns(root, profile->ns));
  1839. }
  1840. /**
  1841. * p_start - start a depth first traversal of profile tree
  1842. * @f: seq_file to fill
  1843. * @pos: current position
  1844. *
  1845. * Returns: first profile under current namespace or NULL if none found
  1846. *
  1847. * acquires first ns->lock
  1848. */
  1849. static void *p_start(struct seq_file *f, loff_t *pos)
  1850. {
  1851. struct aa_profile *profile = NULL;
  1852. struct aa_ns *root = aa_get_current_ns();
  1853. loff_t l = *pos;
  1854. f->private = root;
  1855. /* find the first profile */
  1856. mutex_lock_nested(&root->lock, root->level);
  1857. profile = __first_profile(root, root);
  1858. /* skip to position */
  1859. for (; profile && l > 0; l--)
  1860. profile = next_profile(root, profile);
  1861. return profile;
  1862. }
  1863. /**
  1864. * p_next - read the next profile entry
  1865. * @f: seq_file to fill
  1866. * @p: profile previously returned
  1867. * @pos: current position
  1868. *
  1869. * Returns: next profile after @p or NULL if none
  1870. *
  1871. * may acquire/release locks in namespace tree as necessary
  1872. */
  1873. static void *p_next(struct seq_file *f, void *p, loff_t *pos)
  1874. {
  1875. struct aa_profile *profile = p;
  1876. struct aa_ns *ns = f->private;
  1877. (*pos)++;
  1878. return next_profile(ns, profile);
  1879. }
  1880. /**
  1881. * p_stop - stop depth first traversal
  1882. * @f: seq_file we are filling
  1883. * @p: the last profile writen
  1884. *
  1885. * Release all locking done by p_start/p_next on namespace tree
  1886. */
  1887. static void p_stop(struct seq_file *f, void *p)
  1888. {
  1889. struct aa_profile *profile = p;
  1890. struct aa_ns *root = f->private, *ns;
  1891. if (profile) {
  1892. for (ns = profile->ns; ns && ns != root; ns = ns->parent)
  1893. mutex_unlock(&ns->lock);
  1894. }
  1895. mutex_unlock(&root->lock);
  1896. aa_put_ns(root);
  1897. }
  1898. /**
  1899. * seq_show_profile - show a profile entry
  1900. * @f: seq_file to file
  1901. * @p: current position (profile) (NOT NULL)
  1902. *
  1903. * Returns: error on failure
  1904. */
  1905. static int seq_show_profile(struct seq_file *f, void *p)
  1906. {
  1907. struct aa_profile *profile = (struct aa_profile *)p;
  1908. struct aa_ns *root = f->private;
  1909. aa_label_seq_xprint(f, root, &profile->label,
  1910. FLAG_SHOW_MODE | FLAG_VIEW_SUBNS, GFP_KERNEL);
  1911. seq_putc(f, '\n');
  1912. return 0;
  1913. }
  1914. static const struct seq_operations aa_sfs_profiles_op = {
  1915. .start = p_start,
  1916. .next = p_next,
  1917. .stop = p_stop,
  1918. .show = seq_show_profile,
  1919. };
  1920. static int profiles_open(struct inode *inode, struct file *file)
  1921. {
  1922. if (!aa_current_policy_view_capable(NULL))
  1923. return -EACCES;
  1924. return seq_open(file, &aa_sfs_profiles_op);
  1925. }
  1926. static int profiles_release(struct inode *inode, struct file *file)
  1927. {
  1928. return seq_release(inode, file);
  1929. }
  1930. static const struct file_operations aa_sfs_profiles_fops = {
  1931. .open = profiles_open,
  1932. .read = seq_read,
  1933. .llseek = seq_lseek,
  1934. .release = profiles_release,
  1935. };
  1936. /** Base file system setup **/
  1937. static struct aa_sfs_entry aa_sfs_entry_file[] = {
  1938. AA_SFS_FILE_STRING("mask",
  1939. "create read write exec append mmap_exec link lock"),
  1940. { }
  1941. };
  1942. static struct aa_sfs_entry aa_sfs_entry_ptrace[] = {
  1943. AA_SFS_FILE_STRING("mask", "read trace"),
  1944. { }
  1945. };
  1946. static struct aa_sfs_entry aa_sfs_entry_signal[] = {
  1947. AA_SFS_FILE_STRING("mask", AA_SFS_SIG_MASK),
  1948. { }
  1949. };
  1950. static struct aa_sfs_entry aa_sfs_entry_attach[] = {
  1951. AA_SFS_FILE_BOOLEAN("xattr", 1),
  1952. { }
  1953. };
  1954. static struct aa_sfs_entry aa_sfs_entry_domain[] = {
  1955. AA_SFS_FILE_BOOLEAN("change_hat", 1),
  1956. AA_SFS_FILE_BOOLEAN("change_hatv", 1),
  1957. AA_SFS_FILE_BOOLEAN("change_onexec", 1),
  1958. AA_SFS_FILE_BOOLEAN("change_profile", 1),
  1959. AA_SFS_FILE_BOOLEAN("stack", 1),
  1960. AA_SFS_FILE_BOOLEAN("fix_binfmt_elf_mmap", 1),
  1961. AA_SFS_FILE_BOOLEAN("post_nnp_subset", 1),
  1962. AA_SFS_FILE_BOOLEAN("computed_longest_left", 1),
  1963. AA_SFS_DIR("attach_conditions", aa_sfs_entry_attach),
  1964. AA_SFS_FILE_BOOLEAN("disconnected.path", 1),
  1965. AA_SFS_FILE_STRING("version", "1.2"),
  1966. { }
  1967. };
  1968. static struct aa_sfs_entry aa_sfs_entry_unconfined[] = {
  1969. AA_SFS_FILE_BOOLEAN("change_profile", 1),
  1970. { }
  1971. };
  1972. static struct aa_sfs_entry aa_sfs_entry_versions[] = {
  1973. AA_SFS_FILE_BOOLEAN("v5", 1),
  1974. AA_SFS_FILE_BOOLEAN("v6", 1),
  1975. AA_SFS_FILE_BOOLEAN("v7", 1),
  1976. AA_SFS_FILE_BOOLEAN("v8", 1),
  1977. AA_SFS_FILE_BOOLEAN("v9", 1),
  1978. { }
  1979. };
  1980. #define PERMS32STR "allow deny subtree cond kill complain prompt audit quiet hide xindex tag label"
  1981. static struct aa_sfs_entry aa_sfs_entry_policy[] = {
  1982. AA_SFS_DIR("versions", aa_sfs_entry_versions),
  1983. AA_SFS_FILE_BOOLEAN("set_load", 1),
  1984. /* number of out of band transitions supported */
  1985. AA_SFS_FILE_U64("outofband", MAX_OOB_SUPPORTED),
  1986. AA_SFS_FILE_U64("permstable32_version", 1),
  1987. AA_SFS_FILE_STRING("permstable32", PERMS32STR),
  1988. AA_SFS_DIR("unconfined_restrictions", aa_sfs_entry_unconfined),
  1989. { }
  1990. };
  1991. static struct aa_sfs_entry aa_sfs_entry_mount[] = {
  1992. AA_SFS_FILE_STRING("mask", "mount umount pivot_root"),
  1993. AA_SFS_FILE_STRING("move_mount", "detached"),
  1994. { }
  1995. };
  1996. static struct aa_sfs_entry aa_sfs_entry_ns[] = {
  1997. AA_SFS_FILE_BOOLEAN("profile", 1),
  1998. AA_SFS_FILE_BOOLEAN("pivot_root", 0),
  1999. AA_SFS_FILE_STRING("mask", "userns_create"),
  2000. { }
  2001. };
  2002. static struct aa_sfs_entry aa_sfs_entry_query_label[] = {
  2003. AA_SFS_FILE_STRING("perms", "allow deny audit quiet"),
  2004. AA_SFS_FILE_BOOLEAN("data", 1),
  2005. AA_SFS_FILE_BOOLEAN("multi_transaction", 1),
  2006. { }
  2007. };
  2008. static struct aa_sfs_entry aa_sfs_entry_query[] = {
  2009. AA_SFS_DIR("label", aa_sfs_entry_query_label),
  2010. { }
  2011. };
  2012. static struct aa_sfs_entry aa_sfs_entry_io_uring[] = {
  2013. AA_SFS_FILE_STRING("mask", "sqpoll override_creds"),
  2014. { }
  2015. };
  2016. static struct aa_sfs_entry aa_sfs_entry_features[] = {
  2017. AA_SFS_DIR("policy", aa_sfs_entry_policy),
  2018. AA_SFS_DIR("domain", aa_sfs_entry_domain),
  2019. AA_SFS_DIR("file", aa_sfs_entry_file),
  2020. AA_SFS_DIR("network_v8", aa_sfs_entry_network),
  2021. AA_SFS_DIR("mount", aa_sfs_entry_mount),
  2022. AA_SFS_DIR("namespaces", aa_sfs_entry_ns),
  2023. AA_SFS_FILE_U64("capability", VFS_CAP_FLAGS_MASK),
  2024. AA_SFS_DIR("rlimit", aa_sfs_entry_rlimit),
  2025. AA_SFS_DIR("caps", aa_sfs_entry_caps),
  2026. AA_SFS_DIR("ptrace", aa_sfs_entry_ptrace),
  2027. AA_SFS_DIR("signal", aa_sfs_entry_signal),
  2028. AA_SFS_DIR("query", aa_sfs_entry_query),
  2029. AA_SFS_DIR("io_uring", aa_sfs_entry_io_uring),
  2030. { }
  2031. };
  2032. static struct aa_sfs_entry aa_sfs_entry_apparmor[] = {
  2033. AA_SFS_FILE_FOPS(".access", 0666, &aa_sfs_access),
  2034. AA_SFS_FILE_FOPS(".stacked", 0444, &seq_ns_stacked_fops),
  2035. AA_SFS_FILE_FOPS(".ns_stacked", 0444, &seq_ns_nsstacked_fops),
  2036. AA_SFS_FILE_FOPS(".ns_level", 0444, &seq_ns_level_fops),
  2037. AA_SFS_FILE_FOPS(".ns_name", 0444, &seq_ns_name_fops),
  2038. AA_SFS_FILE_FOPS("profiles", 0444, &aa_sfs_profiles_fops),
  2039. AA_SFS_FILE_FOPS("raw_data_compression_level_min", 0444, &seq_ns_compress_min_fops),
  2040. AA_SFS_FILE_FOPS("raw_data_compression_level_max", 0444, &seq_ns_compress_max_fops),
  2041. AA_SFS_DIR("features", aa_sfs_entry_features),
  2042. { }
  2043. };
  2044. static struct aa_sfs_entry aa_sfs_entry =
  2045. AA_SFS_DIR("apparmor", aa_sfs_entry_apparmor);
  2046. /**
  2047. * entry_create_file - create a file entry in the apparmor securityfs
  2048. * @fs_file: aa_sfs_entry to build an entry for (NOT NULL)
  2049. * @parent: the parent dentry in the securityfs
  2050. *
  2051. * Use entry_remove_file to remove entries created with this fn.
  2052. */
  2053. static int __init entry_create_file(struct aa_sfs_entry *fs_file,
  2054. struct dentry *parent)
  2055. {
  2056. int error = 0;
  2057. fs_file->dentry = securityfs_create_file(fs_file->name,
  2058. S_IFREG | fs_file->mode,
  2059. parent, fs_file,
  2060. fs_file->file_ops);
  2061. if (IS_ERR(fs_file->dentry)) {
  2062. error = PTR_ERR(fs_file->dentry);
  2063. fs_file->dentry = NULL;
  2064. }
  2065. return error;
  2066. }
  2067. static void __init entry_remove_dir(struct aa_sfs_entry *fs_dir);
  2068. /**
  2069. * entry_create_dir - recursively create a directory entry in the securityfs
  2070. * @fs_dir: aa_sfs_entry (and all child entries) to build (NOT NULL)
  2071. * @parent: the parent dentry in the securityfs
  2072. *
  2073. * Use entry_remove_dir to remove entries created with this fn.
  2074. */
  2075. static int __init entry_create_dir(struct aa_sfs_entry *fs_dir,
  2076. struct dentry *parent)
  2077. {
  2078. struct aa_sfs_entry *fs_file;
  2079. struct dentry *dir;
  2080. int error;
  2081. dir = securityfs_create_dir(fs_dir->name, parent);
  2082. if (IS_ERR(dir))
  2083. return PTR_ERR(dir);
  2084. fs_dir->dentry = dir;
  2085. for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
  2086. if (fs_file->v_type == AA_SFS_TYPE_DIR)
  2087. error = entry_create_dir(fs_file, fs_dir->dentry);
  2088. else
  2089. error = entry_create_file(fs_file, fs_dir->dentry);
  2090. if (error)
  2091. goto failed;
  2092. }
  2093. return 0;
  2094. failed:
  2095. entry_remove_dir(fs_dir);
  2096. return error;
  2097. }
  2098. /**
  2099. * entry_remove_file - drop a single file entry in the apparmor securityfs
  2100. * @fs_file: aa_sfs_entry to detach from the securityfs (NOT NULL)
  2101. */
  2102. static void __init entry_remove_file(struct aa_sfs_entry *fs_file)
  2103. {
  2104. if (!fs_file->dentry)
  2105. return;
  2106. securityfs_remove(fs_file->dentry);
  2107. fs_file->dentry = NULL;
  2108. }
  2109. /**
  2110. * entry_remove_dir - recursively drop a directory entry from the securityfs
  2111. * @fs_dir: aa_sfs_entry (and all child entries) to detach (NOT NULL)
  2112. */
  2113. static void __init entry_remove_dir(struct aa_sfs_entry *fs_dir)
  2114. {
  2115. struct aa_sfs_entry *fs_file;
  2116. for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
  2117. if (fs_file->v_type == AA_SFS_TYPE_DIR)
  2118. entry_remove_dir(fs_file);
  2119. else
  2120. entry_remove_file(fs_file);
  2121. }
  2122. entry_remove_file(fs_dir);
  2123. }
  2124. /**
  2125. * aa_destroy_aafs - cleanup and free aafs
  2126. *
  2127. * releases dentries allocated by aa_create_aafs
  2128. */
  2129. void __init aa_destroy_aafs(void)
  2130. {
  2131. entry_remove_dir(&aa_sfs_entry);
  2132. }
  2133. #define NULL_FILE_NAME ".null"
  2134. struct path aa_null;
  2135. static int aa_mk_null_file(struct dentry *parent)
  2136. {
  2137. struct vfsmount *mount = NULL;
  2138. struct dentry *dentry;
  2139. struct inode *inode;
  2140. int count = 0;
  2141. int error = simple_pin_fs(parent->d_sb->s_type, &mount, &count);
  2142. if (error)
  2143. return error;
  2144. inode_lock(d_inode(parent));
  2145. dentry = lookup_one_len(NULL_FILE_NAME, parent, strlen(NULL_FILE_NAME));
  2146. if (IS_ERR(dentry)) {
  2147. error = PTR_ERR(dentry);
  2148. goto out;
  2149. }
  2150. inode = new_inode(parent->d_inode->i_sb);
  2151. if (!inode) {
  2152. error = -ENOMEM;
  2153. goto out1;
  2154. }
  2155. inode->i_ino = get_next_ino();
  2156. inode->i_mode = S_IFCHR | S_IRUGO | S_IWUGO;
  2157. simple_inode_init_ts(inode);
  2158. init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO,
  2159. MKDEV(MEM_MAJOR, 3));
  2160. d_instantiate(dentry, inode);
  2161. aa_null.dentry = dget(dentry);
  2162. aa_null.mnt = mntget(mount);
  2163. error = 0;
  2164. out1:
  2165. dput(dentry);
  2166. out:
  2167. inode_unlock(d_inode(parent));
  2168. simple_release_fs(&mount, &count);
  2169. return error;
  2170. }
  2171. static const char *policy_get_link(struct dentry *dentry,
  2172. struct inode *inode,
  2173. struct delayed_call *done)
  2174. {
  2175. struct aa_ns *ns;
  2176. struct path path;
  2177. int error;
  2178. if (!dentry)
  2179. return ERR_PTR(-ECHILD);
  2180. ns = aa_get_current_ns();
  2181. path.mnt = mntget(aafs_mnt);
  2182. path.dentry = dget(ns_dir(ns));
  2183. error = nd_jump_link(&path);
  2184. aa_put_ns(ns);
  2185. return ERR_PTR(error);
  2186. }
  2187. static int policy_readlink(struct dentry *dentry, char __user *buffer,
  2188. int buflen)
  2189. {
  2190. char name[32];
  2191. int res;
  2192. res = snprintf(name, sizeof(name), "%s:[%lu]", AAFS_NAME,
  2193. d_inode(dentry)->i_ino);
  2194. if (res > 0 && res < sizeof(name))
  2195. res = readlink_copy(buffer, buflen, name);
  2196. else
  2197. res = -ENOENT;
  2198. return res;
  2199. }
  2200. static const struct inode_operations policy_link_iops = {
  2201. .readlink = policy_readlink,
  2202. .get_link = policy_get_link,
  2203. };
  2204. /**
  2205. * aa_create_aafs - create the apparmor security filesystem
  2206. *
  2207. * dentries created here are released by aa_destroy_aafs
  2208. *
  2209. * Returns: error on failure
  2210. */
  2211. static int __init aa_create_aafs(void)
  2212. {
  2213. struct dentry *dent;
  2214. int error;
  2215. if (!apparmor_initialized)
  2216. return 0;
  2217. if (aa_sfs_entry.dentry) {
  2218. AA_ERROR("%s: AppArmor securityfs already exists\n", __func__);
  2219. return -EEXIST;
  2220. }
  2221. /* setup apparmorfs used to virtualize policy/ */
  2222. aafs_mnt = kern_mount(&aafs_ops);
  2223. if (IS_ERR(aafs_mnt))
  2224. panic("can't set apparmorfs up\n");
  2225. aafs_mnt->mnt_sb->s_flags &= ~SB_NOUSER;
  2226. /* Populate fs tree. */
  2227. error = entry_create_dir(&aa_sfs_entry, NULL);
  2228. if (error)
  2229. goto error;
  2230. dent = securityfs_create_file(".load", 0666, aa_sfs_entry.dentry,
  2231. NULL, &aa_fs_profile_load);
  2232. if (IS_ERR(dent))
  2233. goto dent_error;
  2234. ns_subload(root_ns) = dent;
  2235. dent = securityfs_create_file(".replace", 0666, aa_sfs_entry.dentry,
  2236. NULL, &aa_fs_profile_replace);
  2237. if (IS_ERR(dent))
  2238. goto dent_error;
  2239. ns_subreplace(root_ns) = dent;
  2240. dent = securityfs_create_file(".remove", 0666, aa_sfs_entry.dentry,
  2241. NULL, &aa_fs_profile_remove);
  2242. if (IS_ERR(dent))
  2243. goto dent_error;
  2244. ns_subremove(root_ns) = dent;
  2245. dent = securityfs_create_file("revision", 0444, aa_sfs_entry.dentry,
  2246. NULL, &aa_fs_ns_revision_fops);
  2247. if (IS_ERR(dent))
  2248. goto dent_error;
  2249. ns_subrevision(root_ns) = dent;
  2250. /* policy tree referenced by magic policy symlink */
  2251. mutex_lock_nested(&root_ns->lock, root_ns->level);
  2252. error = __aafs_ns_mkdir(root_ns, aafs_mnt->mnt_root, ".policy",
  2253. aafs_mnt->mnt_root);
  2254. mutex_unlock(&root_ns->lock);
  2255. if (error)
  2256. goto error;
  2257. /* magic symlink similar to nsfs redirects based on task policy */
  2258. dent = securityfs_create_symlink("policy", aa_sfs_entry.dentry,
  2259. NULL, &policy_link_iops);
  2260. if (IS_ERR(dent))
  2261. goto dent_error;
  2262. error = aa_mk_null_file(aa_sfs_entry.dentry);
  2263. if (error)
  2264. goto error;
  2265. /* TODO: add default profile to apparmorfs */
  2266. /* Report that AppArmor fs is enabled */
  2267. aa_info_message("AppArmor Filesystem Enabled");
  2268. return 0;
  2269. dent_error:
  2270. error = PTR_ERR(dent);
  2271. error:
  2272. aa_destroy_aafs();
  2273. AA_ERROR("Error creating AppArmor securityfs\n");
  2274. return error;
  2275. }
  2276. fs_initcall(aa_create_aafs);