lsm.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AppArmor security module
  4. *
  5. * This file contains AppArmor LSM hooks.
  6. *
  7. * Copyright (C) 1998-2008 Novell/SUSE
  8. * Copyright 2009-2010 Canonical Ltd.
  9. */
  10. #include <linux/lsm_hooks.h>
  11. #include <linux/moduleparam.h>
  12. #include <linux/mm.h>
  13. #include <linux/mman.h>
  14. #include <linux/mount.h>
  15. #include <linux/namei.h>
  16. #include <linux/ptrace.h>
  17. #include <linux/ctype.h>
  18. #include <linux/sysctl.h>
  19. #include <linux/audit.h>
  20. #include <linux/user_namespace.h>
  21. #include <linux/netfilter_ipv4.h>
  22. #include <linux/netfilter_ipv6.h>
  23. #include <linux/zstd.h>
  24. #include <net/sock.h>
  25. #include <uapi/linux/mount.h>
  26. #include <uapi/linux/lsm.h>
  27. #include "include/af_unix.h"
  28. #include "include/apparmor.h"
  29. #include "include/apparmorfs.h"
  30. #include "include/audit.h"
  31. #include "include/capability.h"
  32. #include "include/cred.h"
  33. #include "include/crypto.h"
  34. #include "include/file.h"
  35. #include "include/ipc.h"
  36. #include "include/net.h"
  37. #include "include/path.h"
  38. #include "include/label.h"
  39. #include "include/policy.h"
  40. #include "include/policy_ns.h"
  41. #include "include/procattr.h"
  42. #include "include/mount.h"
  43. #include "include/secid.h"
  44. /* Flag indicating whether initialization completed */
  45. int apparmor_initialized;
  46. union aa_buffer {
  47. struct list_head list;
  48. DECLARE_FLEX_ARRAY(char, buffer);
  49. };
  50. struct aa_local_cache {
  51. unsigned int hold;
  52. unsigned int count;
  53. struct list_head head;
  54. };
  55. #define RESERVE_COUNT 2
  56. static int reserve_count = RESERVE_COUNT;
  57. static int buffer_count;
  58. static LIST_HEAD(aa_global_buffers);
  59. static DEFINE_SPINLOCK(aa_buffers_lock);
  60. static DEFINE_PER_CPU(struct aa_local_cache, aa_local_buffers);
  61. /*
  62. * LSM hook functions
  63. */
  64. /*
  65. * put the associated labels
  66. */
  67. static void apparmor_cred_free(struct cred *cred)
  68. {
  69. aa_put_label(cred_label(cred));
  70. set_cred_label(cred, NULL);
  71. }
  72. /*
  73. * allocate the apparmor part of blank credentials
  74. */
  75. static int apparmor_cred_alloc_blank(struct cred *cred, gfp_t gfp)
  76. {
  77. set_cred_label(cred, NULL);
  78. return 0;
  79. }
  80. /*
  81. * prepare new cred label for modification by prepare_cred block
  82. */
  83. static int apparmor_cred_prepare(struct cred *new, const struct cred *old,
  84. gfp_t gfp)
  85. {
  86. set_cred_label(new, aa_get_newest_label(cred_label(old)));
  87. return 0;
  88. }
  89. /*
  90. * transfer the apparmor data to a blank set of creds
  91. */
  92. static void apparmor_cred_transfer(struct cred *new, const struct cred *old)
  93. {
  94. set_cred_label(new, aa_get_newest_label(cred_label(old)));
  95. }
  96. static void apparmor_task_free(struct task_struct *task)
  97. {
  98. aa_free_task_ctx(task_ctx(task));
  99. }
  100. static int apparmor_task_alloc(struct task_struct *task,
  101. u64 clone_flags)
  102. {
  103. struct aa_task_ctx *new = task_ctx(task);
  104. aa_dup_task_ctx(new, task_ctx(current));
  105. return 0;
  106. }
  107. static int apparmor_ptrace_access_check(struct task_struct *child,
  108. unsigned int mode)
  109. {
  110. struct aa_label *tracer, *tracee;
  111. const struct cred *cred;
  112. int error;
  113. bool needput;
  114. cred = get_task_cred(child);
  115. tracee = cred_label(cred); /* ref count on cred */
  116. tracer = __begin_current_label_crit_section(&needput);
  117. error = aa_may_ptrace(current_cred(), tracer, cred, tracee,
  118. (mode & PTRACE_MODE_READ) ? AA_PTRACE_READ
  119. : AA_PTRACE_TRACE);
  120. __end_current_label_crit_section(tracer, needput);
  121. put_cred(cred);
  122. return error;
  123. }
  124. static int apparmor_ptrace_traceme(struct task_struct *parent)
  125. {
  126. struct aa_label *tracer, *tracee;
  127. const struct cred *cred;
  128. int error;
  129. bool needput;
  130. tracee = __begin_current_label_crit_section(&needput);
  131. cred = get_task_cred(parent);
  132. tracer = cred_label(cred); /* ref count on cred */
  133. error = aa_may_ptrace(cred, tracer, current_cred(), tracee,
  134. AA_PTRACE_TRACE);
  135. put_cred(cred);
  136. __end_current_label_crit_section(tracee, needput);
  137. return error;
  138. }
  139. /* Derived from security/commoncap.c:cap_capget */
  140. static int apparmor_capget(const struct task_struct *target, kernel_cap_t *effective,
  141. kernel_cap_t *inheritable, kernel_cap_t *permitted)
  142. {
  143. struct aa_label *label;
  144. const struct cred *cred;
  145. rcu_read_lock();
  146. cred = __task_cred(target);
  147. label = aa_get_newest_cred_label(cred);
  148. /*
  149. * cap_capget is stacked ahead of this and will
  150. * initialize effective and permitted.
  151. */
  152. if (!unconfined(label)) {
  153. struct aa_profile *profile;
  154. struct label_it i;
  155. label_for_each_confined(i, label, profile) {
  156. kernel_cap_t allowed;
  157. allowed = aa_profile_capget(profile);
  158. *effective = cap_intersect(*effective, allowed);
  159. *permitted = cap_intersect(*permitted, allowed);
  160. }
  161. }
  162. rcu_read_unlock();
  163. aa_put_label(label);
  164. return 0;
  165. }
  166. static int apparmor_capable(const struct cred *cred, struct user_namespace *ns,
  167. int cap, unsigned int opts)
  168. {
  169. struct aa_label *label;
  170. int error = 0;
  171. label = aa_get_newest_cred_label(cred);
  172. if (!unconfined(label))
  173. error = aa_capable(cred, label, cap, opts);
  174. aa_put_label(label);
  175. return error;
  176. }
  177. /**
  178. * common_perm - basic common permission check wrapper fn for paths
  179. * @op: operation being checked
  180. * @path: path to check permission of (NOT NULL)
  181. * @mask: requested permissions mask
  182. * @cond: conditional info for the permission request (NOT NULL)
  183. *
  184. * Returns: %0 else error code if error or permission denied
  185. */
  186. static int common_perm(const char *op, const struct path *path, u32 mask,
  187. struct path_cond *cond)
  188. {
  189. struct aa_label *label;
  190. int error = 0;
  191. bool needput;
  192. label = __begin_current_label_crit_section(&needput);
  193. if (!unconfined(label))
  194. error = aa_path_perm(op, current_cred(), label, path, 0, mask,
  195. cond);
  196. __end_current_label_crit_section(label, needput);
  197. return error;
  198. }
  199. /**
  200. * common_perm_cond - common permission wrapper around inode cond
  201. * @op: operation being checked
  202. * @path: location to check (NOT NULL)
  203. * @mask: requested permissions mask
  204. *
  205. * Returns: %0 else error code if error or permission denied
  206. */
  207. static int common_perm_cond(const char *op, const struct path *path, u32 mask)
  208. {
  209. vfsuid_t vfsuid = i_uid_into_vfsuid(mnt_idmap(path->mnt),
  210. d_backing_inode(path->dentry));
  211. struct path_cond cond = {
  212. vfsuid_into_kuid(vfsuid),
  213. d_backing_inode(path->dentry)->i_mode
  214. };
  215. if (!path_mediated_fs(path->dentry))
  216. return 0;
  217. return common_perm(op, path, mask, &cond);
  218. }
  219. /**
  220. * common_perm_dir_dentry - common permission wrapper when path is dir, dentry
  221. * @op: operation being checked
  222. * @dir: directory of the dentry (NOT NULL)
  223. * @dentry: dentry to check (NOT NULL)
  224. * @mask: requested permissions mask
  225. * @cond: conditional info for the permission request (NOT NULL)
  226. *
  227. * Returns: %0 else error code if error or permission denied
  228. */
  229. static int common_perm_dir_dentry(const char *op, const struct path *dir,
  230. struct dentry *dentry, u32 mask,
  231. struct path_cond *cond)
  232. {
  233. struct path path = { .mnt = dir->mnt, .dentry = dentry };
  234. return common_perm(op, &path, mask, cond);
  235. }
  236. /**
  237. * common_perm_rm - common permission wrapper for operations doing rm
  238. * @op: operation being checked
  239. * @dir: directory that the dentry is in (NOT NULL)
  240. * @dentry: dentry being rm'd (NOT NULL)
  241. * @mask: requested permission mask
  242. *
  243. * Returns: %0 else error code if error or permission denied
  244. */
  245. static int common_perm_rm(const char *op, const struct path *dir,
  246. struct dentry *dentry, u32 mask)
  247. {
  248. struct inode *inode = d_backing_inode(dentry);
  249. struct path_cond cond = { };
  250. vfsuid_t vfsuid;
  251. if (!inode || !path_mediated_fs(dentry))
  252. return 0;
  253. vfsuid = i_uid_into_vfsuid(mnt_idmap(dir->mnt), inode);
  254. cond.uid = vfsuid_into_kuid(vfsuid);
  255. cond.mode = inode->i_mode;
  256. return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
  257. }
  258. /**
  259. * common_perm_create - common permission wrapper for operations doing create
  260. * @op: operation being checked
  261. * @dir: directory that dentry will be created in (NOT NULL)
  262. * @dentry: dentry to create (NOT NULL)
  263. * @mask: request permission mask
  264. * @mode: created file mode
  265. *
  266. * Returns: %0 else error code if error or permission denied
  267. */
  268. static int common_perm_create(const char *op, const struct path *dir,
  269. struct dentry *dentry, u32 mask, umode_t mode)
  270. {
  271. struct path_cond cond = { current_fsuid(), mode };
  272. if (!path_mediated_fs(dir->dentry))
  273. return 0;
  274. return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
  275. }
  276. static int apparmor_path_unlink(const struct path *dir, struct dentry *dentry)
  277. {
  278. return common_perm_rm(OP_UNLINK, dir, dentry, AA_MAY_DELETE);
  279. }
  280. static int apparmor_path_mkdir(const struct path *dir, struct dentry *dentry,
  281. umode_t mode)
  282. {
  283. return common_perm_create(OP_MKDIR, dir, dentry, AA_MAY_CREATE,
  284. S_IFDIR);
  285. }
  286. static int apparmor_path_rmdir(const struct path *dir, struct dentry *dentry)
  287. {
  288. return common_perm_rm(OP_RMDIR, dir, dentry, AA_MAY_DELETE);
  289. }
  290. static int apparmor_path_mknod(const struct path *dir, struct dentry *dentry,
  291. umode_t mode, unsigned int dev)
  292. {
  293. return common_perm_create(OP_MKNOD, dir, dentry, AA_MAY_CREATE, mode);
  294. }
  295. static int apparmor_path_truncate(const struct path *path)
  296. {
  297. return common_perm_cond(OP_TRUNC, path, MAY_WRITE | AA_MAY_SETATTR);
  298. }
  299. static int apparmor_file_truncate(struct file *file)
  300. {
  301. return apparmor_path_truncate(&file->f_path);
  302. }
  303. static int apparmor_path_symlink(const struct path *dir, struct dentry *dentry,
  304. const char *old_name)
  305. {
  306. return common_perm_create(OP_SYMLINK, dir, dentry, AA_MAY_CREATE,
  307. S_IFLNK);
  308. }
  309. static int apparmor_path_link(struct dentry *old_dentry, const struct path *new_dir,
  310. struct dentry *new_dentry)
  311. {
  312. struct aa_label *label;
  313. int error = 0;
  314. if (!path_mediated_fs(old_dentry))
  315. return 0;
  316. label = begin_current_label_crit_section();
  317. if (!unconfined(label))
  318. error = aa_path_link(current_cred(), label, old_dentry, new_dir,
  319. new_dentry);
  320. end_current_label_crit_section(label);
  321. return error;
  322. }
  323. static int apparmor_path_rename(const struct path *old_dir, struct dentry *old_dentry,
  324. const struct path *new_dir, struct dentry *new_dentry,
  325. const unsigned int flags)
  326. {
  327. struct aa_label *label;
  328. int error = 0;
  329. if (!path_mediated_fs(old_dentry))
  330. return 0;
  331. if ((flags & RENAME_EXCHANGE) && !path_mediated_fs(new_dentry))
  332. return 0;
  333. label = begin_current_label_crit_section();
  334. if (!unconfined(label)) {
  335. struct mnt_idmap *idmap = mnt_idmap(old_dir->mnt);
  336. vfsuid_t vfsuid;
  337. struct path old_path = { .mnt = old_dir->mnt,
  338. .dentry = old_dentry };
  339. struct path new_path = { .mnt = new_dir->mnt,
  340. .dentry = new_dentry };
  341. struct path_cond cond = {
  342. .mode = d_backing_inode(old_dentry)->i_mode
  343. };
  344. vfsuid = i_uid_into_vfsuid(idmap, d_backing_inode(old_dentry));
  345. cond.uid = vfsuid_into_kuid(vfsuid);
  346. if (flags & RENAME_EXCHANGE) {
  347. struct path_cond cond_exchange = {
  348. .mode = d_backing_inode(new_dentry)->i_mode,
  349. };
  350. vfsuid = i_uid_into_vfsuid(idmap, d_backing_inode(old_dentry));
  351. cond_exchange.uid = vfsuid_into_kuid(vfsuid);
  352. error = aa_path_perm(OP_RENAME_SRC, current_cred(),
  353. label, &new_path, 0,
  354. MAY_READ | AA_MAY_GETATTR | MAY_WRITE |
  355. AA_MAY_SETATTR | AA_MAY_DELETE,
  356. &cond_exchange);
  357. if (!error)
  358. error = aa_path_perm(OP_RENAME_DEST, current_cred(),
  359. label, &old_path,
  360. 0, MAY_WRITE | AA_MAY_SETATTR |
  361. AA_MAY_CREATE, &cond_exchange);
  362. }
  363. if (!error)
  364. error = aa_path_perm(OP_RENAME_SRC, current_cred(),
  365. label, &old_path, 0,
  366. MAY_READ | AA_MAY_GETATTR | MAY_WRITE |
  367. AA_MAY_SETATTR | AA_MAY_DELETE,
  368. &cond);
  369. if (!error)
  370. error = aa_path_perm(OP_RENAME_DEST, current_cred(),
  371. label, &new_path,
  372. 0, MAY_WRITE | AA_MAY_SETATTR |
  373. AA_MAY_CREATE, &cond);
  374. }
  375. end_current_label_crit_section(label);
  376. return error;
  377. }
  378. static int apparmor_path_chmod(const struct path *path, umode_t mode)
  379. {
  380. return common_perm_cond(OP_CHMOD, path, AA_MAY_CHMOD);
  381. }
  382. static int apparmor_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
  383. {
  384. return common_perm_cond(OP_CHOWN, path, AA_MAY_CHOWN);
  385. }
  386. static int apparmor_inode_getattr(const struct path *path)
  387. {
  388. return common_perm_cond(OP_GETATTR, path, AA_MAY_GETATTR);
  389. }
  390. static int apparmor_file_open(struct file *file)
  391. {
  392. struct aa_file_ctx *fctx = file_ctx(file);
  393. struct aa_label *label;
  394. int error = 0;
  395. bool needput;
  396. if (!path_mediated_fs(file->f_path.dentry))
  397. return 0;
  398. /* If in exec, permission is handled by bprm hooks.
  399. * Cache permissions granted by the previous exec check, with
  400. * implicit read and executable mmap which are required to
  401. * actually execute the image.
  402. *
  403. * Illogically, FMODE_EXEC is in f_flags, not f_mode.
  404. */
  405. if (file->f_flags & __FMODE_EXEC) {
  406. fctx->allow = MAY_EXEC | MAY_READ | AA_EXEC_MMAP;
  407. return 0;
  408. }
  409. label = aa_get_newest_cred_label_condref(file->f_cred, &needput);
  410. if (!unconfined(label)) {
  411. struct mnt_idmap *idmap = file_mnt_idmap(file);
  412. struct inode *inode = file_inode(file);
  413. vfsuid_t vfsuid;
  414. struct path_cond cond = {
  415. .mode = inode->i_mode,
  416. };
  417. vfsuid = i_uid_into_vfsuid(idmap, inode);
  418. cond.uid = vfsuid_into_kuid(vfsuid);
  419. error = aa_path_perm(OP_OPEN, file->f_cred,
  420. label, &file->f_path, 0,
  421. aa_map_file_to_perms(file), &cond);
  422. /* todo cache full allowed permissions set and state */
  423. fctx->allow = aa_map_file_to_perms(file);
  424. }
  425. aa_put_label_condref(label, needput);
  426. return error;
  427. }
  428. static int apparmor_file_alloc_security(struct file *file)
  429. {
  430. struct aa_file_ctx *ctx = file_ctx(file);
  431. struct aa_label *label = begin_current_label_crit_section();
  432. spin_lock_init(&ctx->lock);
  433. rcu_assign_pointer(ctx->label, aa_get_label(label));
  434. end_current_label_crit_section(label);
  435. return 0;
  436. }
  437. static void apparmor_file_free_security(struct file *file)
  438. {
  439. struct aa_file_ctx *ctx = file_ctx(file);
  440. if (ctx)
  441. aa_put_label(rcu_access_pointer(ctx->label));
  442. }
  443. static int common_file_perm(const char *op, struct file *file, u32 mask)
  444. {
  445. struct aa_label *label;
  446. int error = 0;
  447. label = begin_current_label_crit_section();
  448. error = aa_file_perm(op, current_cred(), label, file, mask, false);
  449. end_current_label_crit_section(label);
  450. return error;
  451. }
  452. static int apparmor_file_receive(struct file *file)
  453. {
  454. return common_file_perm(OP_FRECEIVE, file, aa_map_file_to_perms(file));
  455. }
  456. static int apparmor_file_permission(struct file *file, int mask)
  457. {
  458. return common_file_perm(OP_FPERM, file, mask);
  459. }
  460. static int apparmor_file_lock(struct file *file, unsigned int cmd)
  461. {
  462. u32 mask = AA_MAY_LOCK;
  463. if (cmd == F_WRLCK)
  464. mask |= MAY_WRITE;
  465. return common_file_perm(OP_FLOCK, file, mask);
  466. }
  467. static int common_mmap(const char *op, struct file *file, unsigned long prot,
  468. unsigned long flags)
  469. {
  470. int mask = 0;
  471. if (!file || !file_ctx(file))
  472. return 0;
  473. if (prot & PROT_READ)
  474. mask |= MAY_READ;
  475. /*
  476. * Private mappings don't require write perms since they don't
  477. * write back to the files
  478. */
  479. if ((prot & PROT_WRITE) && !(flags & MAP_PRIVATE))
  480. mask |= MAY_WRITE;
  481. if (prot & PROT_EXEC)
  482. mask |= AA_EXEC_MMAP;
  483. return common_file_perm(op, file, mask);
  484. }
  485. static int apparmor_mmap_file(struct file *file, unsigned long reqprot,
  486. unsigned long prot, unsigned long flags)
  487. {
  488. return common_mmap(OP_FMMAP, file, prot, flags);
  489. }
  490. static int apparmor_file_mprotect(struct vm_area_struct *vma,
  491. unsigned long reqprot, unsigned long prot)
  492. {
  493. return common_mmap(OP_FMPROT, vma->vm_file, prot,
  494. !(vma->vm_flags & VM_SHARED) ? MAP_PRIVATE : 0);
  495. }
  496. #ifdef CONFIG_IO_URING
  497. static const char *audit_uring_mask(u32 mask)
  498. {
  499. if (mask & AA_MAY_CREATE_SQPOLL)
  500. return "sqpoll";
  501. if (mask & AA_MAY_OVERRIDE_CRED)
  502. return "override_creds";
  503. return "";
  504. }
  505. static void audit_uring_cb(struct audit_buffer *ab, void *va)
  506. {
  507. struct apparmor_audit_data *ad = aad_of_va(va);
  508. if (ad->request & AA_URING_PERM_MASK) {
  509. audit_log_format(ab, " requested=\"%s\"",
  510. audit_uring_mask(ad->request));
  511. if (ad->denied & AA_URING_PERM_MASK) {
  512. audit_log_format(ab, " denied=\"%s\"",
  513. audit_uring_mask(ad->denied));
  514. }
  515. }
  516. if (ad->uring.target) {
  517. audit_log_format(ab, " tcontext=");
  518. aa_label_xaudit(ab, labels_ns(ad->subj_label),
  519. ad->uring.target,
  520. FLAGS_NONE, GFP_ATOMIC);
  521. }
  522. }
  523. static int profile_uring(struct aa_profile *profile, u32 request,
  524. struct aa_label *new, int cap,
  525. struct apparmor_audit_data *ad)
  526. {
  527. unsigned int state;
  528. struct aa_ruleset *rules;
  529. int error = 0;
  530. AA_BUG(!profile);
  531. rules = profile->label.rules[0];
  532. state = RULE_MEDIATES(rules, AA_CLASS_IO_URING);
  533. if (state) {
  534. struct aa_perms perms = { };
  535. if (new) {
  536. aa_label_match(profile, rules, new, state,
  537. false, request, &perms);
  538. } else {
  539. perms = *aa_lookup_perms(rules->policy, state);
  540. }
  541. aa_apply_modes_to_perms(profile, &perms);
  542. error = aa_check_perms(profile, &perms, request, ad,
  543. audit_uring_cb);
  544. }
  545. return error;
  546. }
  547. /**
  548. * apparmor_uring_override_creds - check the requested cred override
  549. * @new: the target creds
  550. *
  551. * Check to see if the current task is allowed to override it's credentials
  552. * to service an io_uring operation.
  553. */
  554. static int apparmor_uring_override_creds(const struct cred *new)
  555. {
  556. struct aa_profile *profile;
  557. struct aa_label *label;
  558. int error;
  559. bool needput;
  560. DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_NONE, AA_CLASS_IO_URING,
  561. OP_URING_OVERRIDE);
  562. ad.uring.target = cred_label(new);
  563. label = __begin_current_label_crit_section(&needput);
  564. error = fn_for_each(label, profile,
  565. profile_uring(profile, AA_MAY_OVERRIDE_CRED,
  566. cred_label(new), CAP_SYS_ADMIN, &ad));
  567. __end_current_label_crit_section(label, needput);
  568. return error;
  569. }
  570. /**
  571. * apparmor_uring_sqpoll - check if a io_uring polling thread can be created
  572. *
  573. * Check to see if the current task is allowed to create a new io_uring
  574. * kernel polling thread.
  575. */
  576. static int apparmor_uring_sqpoll(void)
  577. {
  578. struct aa_profile *profile;
  579. struct aa_label *label;
  580. int error;
  581. bool needput;
  582. DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_NONE, AA_CLASS_IO_URING,
  583. OP_URING_SQPOLL);
  584. label = __begin_current_label_crit_section(&needput);
  585. error = fn_for_each(label, profile,
  586. profile_uring(profile, AA_MAY_CREATE_SQPOLL,
  587. NULL, CAP_SYS_ADMIN, &ad));
  588. __end_current_label_crit_section(label, needput);
  589. return error;
  590. }
  591. #endif /* CONFIG_IO_URING */
  592. static int apparmor_sb_mount(const char *dev_name, const struct path *path,
  593. const char *type, unsigned long flags, void *data)
  594. {
  595. struct aa_label *label;
  596. int error = 0;
  597. bool needput;
  598. /* Discard magic */
  599. if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
  600. flags &= ~MS_MGC_MSK;
  601. flags &= ~AA_MS_IGNORE_MASK;
  602. label = __begin_current_label_crit_section(&needput);
  603. if (!unconfined(label)) {
  604. if (flags & MS_REMOUNT)
  605. error = aa_remount(current_cred(), label, path, flags,
  606. data);
  607. else if (flags & MS_BIND)
  608. error = aa_bind_mount(current_cred(), label, path,
  609. dev_name, flags);
  610. else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE |
  611. MS_UNBINDABLE))
  612. error = aa_mount_change_type(current_cred(), label,
  613. path, flags);
  614. else if (flags & MS_MOVE)
  615. error = aa_move_mount_old(current_cred(), label, path,
  616. dev_name);
  617. else
  618. error = aa_new_mount(current_cred(), label, dev_name,
  619. path, type, flags, data);
  620. }
  621. __end_current_label_crit_section(label, needput);
  622. return error;
  623. }
  624. static int apparmor_move_mount(const struct path *from_path,
  625. const struct path *to_path)
  626. {
  627. struct aa_label *label;
  628. int error = 0;
  629. bool needput;
  630. label = __begin_current_label_crit_section(&needput);
  631. if (!unconfined(label))
  632. error = aa_move_mount(current_cred(), label, from_path,
  633. to_path);
  634. __end_current_label_crit_section(label, needput);
  635. return error;
  636. }
  637. static int apparmor_sb_umount(struct vfsmount *mnt, int flags)
  638. {
  639. struct aa_label *label;
  640. int error = 0;
  641. bool needput;
  642. label = __begin_current_label_crit_section(&needput);
  643. if (!unconfined(label))
  644. error = aa_umount(current_cred(), label, mnt, flags);
  645. __end_current_label_crit_section(label, needput);
  646. return error;
  647. }
  648. static int apparmor_sb_pivotroot(const struct path *old_path,
  649. const struct path *new_path)
  650. {
  651. struct aa_label *label;
  652. int error = 0;
  653. label = aa_get_current_label();
  654. if (!unconfined(label))
  655. error = aa_pivotroot(current_cred(), label, old_path, new_path);
  656. aa_put_label(label);
  657. return error;
  658. }
  659. static int apparmor_getselfattr(unsigned int attr, struct lsm_ctx __user *lx,
  660. u32 *size, u32 flags)
  661. {
  662. int error = -ENOENT;
  663. struct aa_task_ctx *ctx = task_ctx(current);
  664. struct aa_label *label = NULL;
  665. char *value = NULL;
  666. switch (attr) {
  667. case LSM_ATTR_CURRENT:
  668. label = aa_get_newest_label(cred_label(current_cred()));
  669. break;
  670. case LSM_ATTR_PREV:
  671. if (ctx->previous)
  672. label = aa_get_newest_label(ctx->previous);
  673. break;
  674. case LSM_ATTR_EXEC:
  675. if (ctx->onexec)
  676. label = aa_get_newest_label(ctx->onexec);
  677. break;
  678. default:
  679. error = -EOPNOTSUPP;
  680. break;
  681. }
  682. if (label) {
  683. error = aa_getprocattr(label, &value, false);
  684. if (error > 0)
  685. error = lsm_fill_user_ctx(lx, size, value, error,
  686. LSM_ID_APPARMOR, 0);
  687. kfree(value);
  688. }
  689. aa_put_label(label);
  690. if (error < 0)
  691. return error;
  692. return 1;
  693. }
  694. static int apparmor_getprocattr(struct task_struct *task, const char *name,
  695. char **value)
  696. {
  697. int error = -ENOENT;
  698. /* released below */
  699. const struct cred *cred = get_task_cred(task);
  700. struct aa_task_ctx *ctx = task_ctx(current);
  701. struct aa_label *label = NULL;
  702. if (strcmp(name, "current") == 0)
  703. label = aa_get_newest_label(cred_label(cred));
  704. else if (strcmp(name, "prev") == 0 && ctx->previous)
  705. label = aa_get_newest_label(ctx->previous);
  706. else if (strcmp(name, "exec") == 0 && ctx->onexec)
  707. label = aa_get_newest_label(ctx->onexec);
  708. else
  709. error = -EINVAL;
  710. if (label)
  711. error = aa_getprocattr(label, value, true);
  712. aa_put_label(label);
  713. put_cred(cred);
  714. return error;
  715. }
  716. static int do_setattr(u64 attr, void *value, size_t size)
  717. {
  718. char *command, *largs = NULL, *args = value;
  719. size_t arg_size;
  720. int error;
  721. DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_NONE, AA_CLASS_NONE,
  722. OP_SETPROCATTR);
  723. if (size == 0)
  724. return -EINVAL;
  725. /* AppArmor requires that the buffer must be null terminated atm */
  726. if (args[size - 1] != '\0') {
  727. /* null terminate */
  728. largs = args = kmalloc(size + 1, GFP_KERNEL);
  729. if (!args)
  730. return -ENOMEM;
  731. memcpy(args, value, size);
  732. args[size] = '\0';
  733. }
  734. error = -EINVAL;
  735. args = strim(args);
  736. command = strsep(&args, " ");
  737. if (!args)
  738. goto out;
  739. args = skip_spaces(args);
  740. if (!*args)
  741. goto out;
  742. arg_size = size - (args - (largs ? largs : (char *) value));
  743. if (attr == LSM_ATTR_CURRENT) {
  744. if (strcmp(command, "changehat") == 0) {
  745. error = aa_setprocattr_changehat(args, arg_size,
  746. AA_CHANGE_NOFLAGS);
  747. } else if (strcmp(command, "permhat") == 0) {
  748. error = aa_setprocattr_changehat(args, arg_size,
  749. AA_CHANGE_TEST);
  750. } else if (strcmp(command, "changeprofile") == 0) {
  751. error = aa_change_profile(args, AA_CHANGE_NOFLAGS);
  752. } else if (strcmp(command, "permprofile") == 0) {
  753. error = aa_change_profile(args, AA_CHANGE_TEST);
  754. } else if (strcmp(command, "stack") == 0) {
  755. error = aa_change_profile(args, AA_CHANGE_STACK);
  756. } else
  757. goto fail;
  758. } else if (attr == LSM_ATTR_EXEC) {
  759. if (strcmp(command, "exec") == 0)
  760. error = aa_change_profile(args, AA_CHANGE_ONEXEC);
  761. else if (strcmp(command, "stack") == 0)
  762. error = aa_change_profile(args, (AA_CHANGE_ONEXEC |
  763. AA_CHANGE_STACK));
  764. else
  765. goto fail;
  766. } else
  767. /* only support the "current" and "exec" process attributes */
  768. goto fail;
  769. if (!error)
  770. error = size;
  771. out:
  772. kfree(largs);
  773. return error;
  774. fail:
  775. ad.subj_label = begin_current_label_crit_section();
  776. if (attr == LSM_ATTR_CURRENT)
  777. ad.info = "current";
  778. else if (attr == LSM_ATTR_EXEC)
  779. ad.info = "exec";
  780. else
  781. ad.info = "invalid";
  782. ad.error = error = -EINVAL;
  783. aa_audit_msg(AUDIT_APPARMOR_DENIED, &ad, NULL);
  784. end_current_label_crit_section(ad.subj_label);
  785. goto out;
  786. }
  787. static int apparmor_setselfattr(unsigned int attr, struct lsm_ctx *ctx,
  788. u32 size, u32 flags)
  789. {
  790. int rc;
  791. if (attr != LSM_ATTR_CURRENT && attr != LSM_ATTR_EXEC)
  792. return -EOPNOTSUPP;
  793. rc = do_setattr(attr, ctx->ctx, ctx->ctx_len);
  794. if (rc > 0)
  795. return 0;
  796. return rc;
  797. }
  798. static int apparmor_setprocattr(const char *name, void *value,
  799. size_t size)
  800. {
  801. int attr = lsm_name_to_attr(name);
  802. if (attr)
  803. return do_setattr(attr, value, size);
  804. return -EINVAL;
  805. }
  806. /**
  807. * apparmor_bprm_committing_creds - do task cleanup on committing new creds
  808. * @bprm: binprm for the exec (NOT NULL)
  809. */
  810. static void apparmor_bprm_committing_creds(const struct linux_binprm *bprm)
  811. {
  812. struct aa_label *label = aa_current_raw_label();
  813. struct aa_label *new_label = cred_label(bprm->cred);
  814. /* bail out if unconfined or not changing profile */
  815. if ((new_label->proxy == label->proxy) ||
  816. (unconfined(new_label)))
  817. return;
  818. aa_inherit_files(bprm->cred, current->files);
  819. current->pdeath_signal = 0;
  820. /* reset soft limits and set hard limits for the new label */
  821. __aa_transition_rlimits(label, new_label);
  822. }
  823. /**
  824. * apparmor_bprm_committed_creds() - do cleanup after new creds committed
  825. * @bprm: binprm for the exec (NOT NULL)
  826. */
  827. static void apparmor_bprm_committed_creds(const struct linux_binprm *bprm)
  828. {
  829. /* clear out temporary/transitional state from the context */
  830. aa_clear_task_ctx_trans(task_ctx(current));
  831. return;
  832. }
  833. static void apparmor_current_getlsmprop_subj(struct lsm_prop *prop)
  834. {
  835. struct aa_label *label;
  836. bool needput;
  837. label = __begin_current_label_crit_section(&needput);
  838. prop->apparmor.label = label;
  839. __end_current_label_crit_section(label, needput);
  840. }
  841. static void apparmor_task_getlsmprop_obj(struct task_struct *p,
  842. struct lsm_prop *prop)
  843. {
  844. struct aa_label *label = aa_get_task_label(p);
  845. prop->apparmor.label = label;
  846. aa_put_label(label);
  847. }
  848. static int apparmor_task_setrlimit(struct task_struct *task,
  849. unsigned int resource, struct rlimit *new_rlim)
  850. {
  851. struct aa_label *label;
  852. int error = 0;
  853. bool needput;
  854. label = __begin_current_label_crit_section(&needput);
  855. if (!unconfined(label))
  856. error = aa_task_setrlimit(current_cred(), label, task,
  857. resource, new_rlim);
  858. __end_current_label_crit_section(label, needput);
  859. return error;
  860. }
  861. static int apparmor_task_kill(struct task_struct *target, struct kernel_siginfo *info,
  862. int sig, const struct cred *cred)
  863. {
  864. const struct cred *tc;
  865. struct aa_label *cl, *tl;
  866. int error;
  867. bool needput;
  868. tc = get_task_cred(target);
  869. tl = aa_get_newest_cred_label(tc);
  870. if (cred) {
  871. /*
  872. * Dealing with USB IO specific behavior
  873. */
  874. cl = aa_get_newest_cred_label(cred);
  875. error = aa_may_signal(cred, cl, tc, tl, sig);
  876. aa_put_label(cl);
  877. } else {
  878. cl = __begin_current_label_crit_section(&needput);
  879. error = aa_may_signal(current_cred(), cl, tc, tl, sig);
  880. __end_current_label_crit_section(cl, needput);
  881. }
  882. aa_put_label(tl);
  883. put_cred(tc);
  884. return error;
  885. }
  886. static int apparmor_userns_create(const struct cred *cred)
  887. {
  888. struct aa_label *label;
  889. struct aa_profile *profile;
  890. int error = 0;
  891. DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_TASK, AA_CLASS_NS,
  892. OP_USERNS_CREATE);
  893. ad.subj_cred = current_cred();
  894. label = begin_current_label_crit_section();
  895. if (!unconfined(label)) {
  896. error = fn_for_each(label, profile,
  897. aa_profile_ns_perm(profile, &ad,
  898. AA_USERNS_CREATE));
  899. }
  900. end_current_label_crit_section(label);
  901. return error;
  902. }
  903. static int apparmor_sk_alloc_security(struct sock *sk, int family, gfp_t gfp)
  904. {
  905. struct aa_sk_ctx *ctx = aa_sock(sk);
  906. struct aa_label *label;
  907. bool needput;
  908. label = __begin_current_label_crit_section(&needput);
  909. //spin_lock_init(&ctx->lock);
  910. rcu_assign_pointer(ctx->label, aa_get_label(label));
  911. rcu_assign_pointer(ctx->peer, NULL);
  912. rcu_assign_pointer(ctx->peer_lastupdate, NULL);
  913. __end_current_label_crit_section(label, needput);
  914. return 0;
  915. }
  916. static void apparmor_sk_free_security(struct sock *sk)
  917. {
  918. struct aa_sk_ctx *ctx = aa_sock(sk);
  919. /* dead these won't be updated any more */
  920. aa_put_label(rcu_dereference_protected(ctx->label, true));
  921. aa_put_label(rcu_dereference_protected(ctx->peer, true));
  922. aa_put_label(rcu_dereference_protected(ctx->peer_lastupdate, true));
  923. }
  924. /**
  925. * apparmor_sk_clone_security - clone the sk_security field
  926. * @sk: sock to have security cloned
  927. * @newsk: sock getting clone
  928. */
  929. static void apparmor_sk_clone_security(const struct sock *sk,
  930. struct sock *newsk)
  931. {
  932. struct aa_sk_ctx *ctx = aa_sock(sk);
  933. struct aa_sk_ctx *new = aa_sock(newsk);
  934. /* not actually in use yet */
  935. if (rcu_access_pointer(ctx->label) != rcu_access_pointer(new->label)) {
  936. aa_put_label(rcu_dereference_protected(new->label, true));
  937. rcu_assign_pointer(new->label, aa_get_label_rcu(&ctx->label));
  938. }
  939. if (rcu_access_pointer(ctx->peer) != rcu_access_pointer(new->peer)) {
  940. aa_put_label(rcu_dereference_protected(new->peer, true));
  941. rcu_assign_pointer(new->peer, aa_get_label_rcu(&ctx->peer));
  942. }
  943. if (rcu_access_pointer(ctx->peer_lastupdate) != rcu_access_pointer(new->peer_lastupdate)) {
  944. aa_put_label(rcu_dereference_protected(new->peer_lastupdate, true));
  945. rcu_assign_pointer(new->peer_lastupdate,
  946. aa_get_label_rcu(&ctx->peer_lastupdate));
  947. }
  948. }
  949. static int unix_connect_perm(const struct cred *cred, struct aa_label *label,
  950. struct sock *sk, struct sock *peer_sk)
  951. {
  952. struct aa_sk_ctx *peer_ctx = aa_sock(peer_sk);
  953. int error;
  954. error = aa_unix_peer_perm(cred, label, OP_CONNECT,
  955. (AA_MAY_CONNECT | AA_MAY_SEND | AA_MAY_RECEIVE),
  956. sk, peer_sk,
  957. rcu_dereference_protected(peer_ctx->label,
  958. lockdep_is_held(&unix_sk(peer_sk)->lock)));
  959. if (!is_unix_fs(peer_sk)) {
  960. last_error(error,
  961. aa_unix_peer_perm(cred,
  962. rcu_dereference_protected(peer_ctx->label,
  963. lockdep_is_held(&unix_sk(peer_sk)->lock)),
  964. OP_CONNECT,
  965. (AA_MAY_ACCEPT | AA_MAY_SEND | AA_MAY_RECEIVE),
  966. peer_sk, sk, label));
  967. }
  968. return error;
  969. }
  970. /* lockdep check in unix_connect_perm - push sks here to check */
  971. static void unix_connect_peers(struct aa_sk_ctx *sk_ctx,
  972. struct aa_sk_ctx *peer_ctx)
  973. {
  974. /* Cross reference the peer labels for SO_PEERSEC */
  975. struct aa_label *label = rcu_dereference_protected(sk_ctx->label, true);
  976. aa_get_label(label);
  977. aa_put_label(rcu_dereference_protected(peer_ctx->peer,
  978. true));
  979. rcu_assign_pointer(peer_ctx->peer, label); /* transfer cnt */
  980. label = aa_get_label(rcu_dereference_protected(peer_ctx->label,
  981. true));
  982. //spin_unlock(&peer_ctx->lock);
  983. //spin_lock(&sk_ctx->lock);
  984. aa_put_label(rcu_dereference_protected(sk_ctx->peer,
  985. true));
  986. aa_put_label(rcu_dereference_protected(sk_ctx->peer_lastupdate,
  987. true));
  988. rcu_assign_pointer(sk_ctx->peer, aa_get_label(label));
  989. rcu_assign_pointer(sk_ctx->peer_lastupdate, label); /* transfer cnt */
  990. //spin_unlock(&sk_ctx->lock);
  991. }
  992. /**
  993. * apparmor_unix_stream_connect - check perms before making unix domain conn
  994. * @sk: sk attempting to connect
  995. * @peer_sk: sk that is accepting the connection
  996. * @newsk: new sk created for this connection
  997. * peer is locked when this hook is called
  998. *
  999. * Return:
  1000. * 0 if connection is permitted
  1001. * error code on denial or failure
  1002. */
  1003. static int apparmor_unix_stream_connect(struct sock *sk, struct sock *peer_sk,
  1004. struct sock *newsk)
  1005. {
  1006. struct aa_sk_ctx *sk_ctx = aa_sock(sk);
  1007. struct aa_sk_ctx *peer_ctx = aa_sock(peer_sk);
  1008. struct aa_sk_ctx *new_ctx = aa_sock(newsk);
  1009. struct aa_label *label;
  1010. int error;
  1011. bool needput;
  1012. label = __begin_current_label_crit_section(&needput);
  1013. error = unix_connect_perm(current_cred(), label, sk, peer_sk);
  1014. __end_current_label_crit_section(label, needput);
  1015. if (error)
  1016. return error;
  1017. /* newsk doesn't go through post_create, but does go through
  1018. * security_sk_alloc()
  1019. */
  1020. rcu_assign_pointer(new_ctx->label,
  1021. aa_get_label(rcu_dereference_protected(peer_ctx->label,
  1022. true)));
  1023. /* Cross reference the peer labels for SO_PEERSEC */
  1024. unix_connect_peers(sk_ctx, new_ctx);
  1025. return 0;
  1026. }
  1027. /**
  1028. * apparmor_unix_may_send - check perms before conn or sending unix dgrams
  1029. * @sock: socket sending the message
  1030. * @peer: socket message is being send to
  1031. *
  1032. * Performs bidirectional permission checks for Unix domain socket communication:
  1033. * 1. Verifies sender has AA_MAY_SEND to target socket
  1034. * 2. Verifies receiver has AA_MAY_RECEIVE from source socket
  1035. *
  1036. * sock and peer are locked when this hook is called
  1037. * called by: dgram_connect peer setup but path not copied to newsk
  1038. *
  1039. * Return:
  1040. * 0 if transmission is permitted
  1041. * error code on denial or failure
  1042. */
  1043. static int apparmor_unix_may_send(struct socket *sock, struct socket *peer)
  1044. {
  1045. struct aa_sk_ctx *peer_ctx = aa_sock(peer->sk);
  1046. struct aa_label *label;
  1047. int error;
  1048. bool needput;
  1049. label = __begin_current_label_crit_section(&needput);
  1050. error = xcheck(aa_unix_peer_perm(current_cred(),
  1051. label, OP_SENDMSG, AA_MAY_SEND,
  1052. sock->sk, peer->sk,
  1053. rcu_dereference_protected(peer_ctx->label,
  1054. true)),
  1055. aa_unix_peer_perm(peer->file ? peer->file->f_cred : NULL,
  1056. rcu_dereference_protected(peer_ctx->label,
  1057. true),
  1058. OP_SENDMSG, AA_MAY_RECEIVE, peer->sk,
  1059. sock->sk, label));
  1060. __end_current_label_crit_section(label, needput);
  1061. return error;
  1062. }
  1063. static int apparmor_socket_create(int family, int type, int protocol, int kern)
  1064. {
  1065. struct aa_label *label;
  1066. int error = 0;
  1067. AA_BUG(in_interrupt());
  1068. if (kern)
  1069. return 0;
  1070. label = begin_current_label_crit_section();
  1071. if (!unconfined(label)) {
  1072. if (family == PF_UNIX)
  1073. error = aa_unix_create_perm(label, family, type,
  1074. protocol);
  1075. else
  1076. error = aa_af_perm(current_cred(), label, OP_CREATE,
  1077. AA_MAY_CREATE, family, type,
  1078. protocol);
  1079. }
  1080. end_current_label_crit_section(label);
  1081. return error;
  1082. }
  1083. /**
  1084. * apparmor_socket_post_create - setup the per-socket security struct
  1085. * @sock: socket that is being setup
  1086. * @family: family of socket being created
  1087. * @type: type of the socket
  1088. * @protocol: protocol of the socket
  1089. * @kern: socket is a special kernel socket
  1090. *
  1091. * Note:
  1092. * - kernel sockets labeled kernel_t used to use unconfined
  1093. * - socket may not have sk here if created with sock_create_lite or
  1094. * sock_alloc. These should be accept cases which will be handled in
  1095. * sock_graft.
  1096. */
  1097. static int apparmor_socket_post_create(struct socket *sock, int family,
  1098. int type, int protocol, int kern)
  1099. {
  1100. struct aa_label *label;
  1101. if (kern) {
  1102. label = aa_get_label(kernel_t);
  1103. } else
  1104. label = aa_get_current_label();
  1105. if (sock->sk) {
  1106. struct aa_sk_ctx *ctx = aa_sock(sock->sk);
  1107. /* still not live */
  1108. aa_put_label(rcu_dereference_protected(ctx->label, true));
  1109. rcu_assign_pointer(ctx->label, aa_get_label(label));
  1110. }
  1111. aa_put_label(label);
  1112. return 0;
  1113. }
  1114. static int apparmor_socket_socketpair(struct socket *socka,
  1115. struct socket *sockb)
  1116. {
  1117. struct aa_sk_ctx *a_ctx = aa_sock(socka->sk);
  1118. struct aa_sk_ctx *b_ctx = aa_sock(sockb->sk);
  1119. struct aa_label *label;
  1120. /* socks not live yet - initial values set in sk_alloc */
  1121. label = begin_current_label_crit_section();
  1122. if (rcu_access_pointer(a_ctx->label) != label) {
  1123. AA_BUG("a_ctx != label");
  1124. aa_put_label(rcu_dereference_protected(a_ctx->label, true));
  1125. rcu_assign_pointer(a_ctx->label, aa_get_label(label));
  1126. }
  1127. if (rcu_access_pointer(b_ctx->label) != label) {
  1128. AA_BUG("b_ctx != label");
  1129. aa_put_label(rcu_dereference_protected(b_ctx->label, true));
  1130. rcu_assign_pointer(b_ctx->label, aa_get_label(label));
  1131. }
  1132. if (socka->sk->sk_family == PF_UNIX) {
  1133. /* unix socket pairs by-pass unix_stream_connect */
  1134. unix_connect_peers(a_ctx, b_ctx);
  1135. }
  1136. end_current_label_crit_section(label);
  1137. return 0;
  1138. }
  1139. /**
  1140. * apparmor_socket_bind - check perms before bind addr to socket
  1141. * @sock: socket to bind the address to (must be non-NULL)
  1142. * @address: address that is being bound (must be non-NULL)
  1143. * @addrlen: length of @address
  1144. *
  1145. * Performs security checks before allowing a socket to bind to an address.
  1146. * Handles Unix domain sockets specially through aa_unix_bind_perm().
  1147. * For other socket families, uses generic permission check via aa_sk_perm().
  1148. *
  1149. * Return:
  1150. * 0 if binding is permitted
  1151. * error code on denial or invalid parameters
  1152. */
  1153. static int apparmor_socket_bind(struct socket *sock,
  1154. struct sockaddr *address, int addrlen)
  1155. {
  1156. AA_BUG(!sock);
  1157. AA_BUG(!sock->sk);
  1158. AA_BUG(!address);
  1159. AA_BUG(in_interrupt());
  1160. if (sock->sk->sk_family == PF_UNIX)
  1161. return aa_unix_bind_perm(sock, address, addrlen);
  1162. return aa_sk_perm(OP_BIND, AA_MAY_BIND, sock->sk);
  1163. }
  1164. static int apparmor_socket_connect(struct socket *sock,
  1165. struct sockaddr *address, int addrlen)
  1166. {
  1167. AA_BUG(!sock);
  1168. AA_BUG(!sock->sk);
  1169. AA_BUG(!address);
  1170. AA_BUG(in_interrupt());
  1171. /* PF_UNIX goes through unix_stream_connect && unix_may_send */
  1172. if (sock->sk->sk_family == PF_UNIX)
  1173. return 0;
  1174. return aa_sk_perm(OP_CONNECT, AA_MAY_CONNECT, sock->sk);
  1175. }
  1176. static int apparmor_socket_listen(struct socket *sock, int backlog)
  1177. {
  1178. AA_BUG(!sock);
  1179. AA_BUG(!sock->sk);
  1180. AA_BUG(in_interrupt());
  1181. if (sock->sk->sk_family == PF_UNIX)
  1182. return aa_unix_listen_perm(sock, backlog);
  1183. return aa_sk_perm(OP_LISTEN, AA_MAY_LISTEN, sock->sk);
  1184. }
  1185. /*
  1186. * Note: while @newsock is created and has some information, the accept
  1187. * has not been done.
  1188. */
  1189. static int apparmor_socket_accept(struct socket *sock, struct socket *newsock)
  1190. {
  1191. AA_BUG(!sock);
  1192. AA_BUG(!sock->sk);
  1193. AA_BUG(!newsock);
  1194. AA_BUG(in_interrupt());
  1195. if (sock->sk->sk_family == PF_UNIX)
  1196. return aa_unix_accept_perm(sock, newsock);
  1197. return aa_sk_perm(OP_ACCEPT, AA_MAY_ACCEPT, sock->sk);
  1198. }
  1199. static int aa_sock_msg_perm(const char *op, u32 request, struct socket *sock,
  1200. struct msghdr *msg, int size)
  1201. {
  1202. AA_BUG(!sock);
  1203. AA_BUG(!sock->sk);
  1204. AA_BUG(!msg);
  1205. AA_BUG(in_interrupt());
  1206. /* PF_UNIX goes through unix_may_send */
  1207. if (sock->sk->sk_family == PF_UNIX)
  1208. return 0;
  1209. return aa_sk_perm(op, request, sock->sk);
  1210. }
  1211. static int apparmor_socket_sendmsg(struct socket *sock,
  1212. struct msghdr *msg, int size)
  1213. {
  1214. return aa_sock_msg_perm(OP_SENDMSG, AA_MAY_SEND, sock, msg, size);
  1215. }
  1216. static int apparmor_socket_recvmsg(struct socket *sock,
  1217. struct msghdr *msg, int size, int flags)
  1218. {
  1219. return aa_sock_msg_perm(OP_RECVMSG, AA_MAY_RECEIVE, sock, msg, size);
  1220. }
  1221. /* revaliation, get/set attr, shutdown */
  1222. static int aa_sock_perm(const char *op, u32 request, struct socket *sock)
  1223. {
  1224. AA_BUG(!sock);
  1225. AA_BUG(!sock->sk);
  1226. AA_BUG(in_interrupt());
  1227. if (sock->sk->sk_family == PF_UNIX)
  1228. return aa_unix_sock_perm(op, request, sock);
  1229. return aa_sk_perm(op, request, sock->sk);
  1230. }
  1231. static int apparmor_socket_getsockname(struct socket *sock)
  1232. {
  1233. return aa_sock_perm(OP_GETSOCKNAME, AA_MAY_GETATTR, sock);
  1234. }
  1235. static int apparmor_socket_getpeername(struct socket *sock)
  1236. {
  1237. return aa_sock_perm(OP_GETPEERNAME, AA_MAY_GETATTR, sock);
  1238. }
  1239. /* revaliation, get/set attr, opt */
  1240. static int aa_sock_opt_perm(const char *op, u32 request, struct socket *sock,
  1241. int level, int optname)
  1242. {
  1243. AA_BUG(!sock);
  1244. AA_BUG(!sock->sk);
  1245. AA_BUG(in_interrupt());
  1246. if (sock->sk->sk_family == PF_UNIX)
  1247. return aa_unix_opt_perm(op, request, sock, level, optname);
  1248. return aa_sk_perm(op, request, sock->sk);
  1249. }
  1250. static int apparmor_socket_getsockopt(struct socket *sock, int level,
  1251. int optname)
  1252. {
  1253. return aa_sock_opt_perm(OP_GETSOCKOPT, AA_MAY_GETOPT, sock,
  1254. level, optname);
  1255. }
  1256. static int apparmor_socket_setsockopt(struct socket *sock, int level,
  1257. int optname)
  1258. {
  1259. return aa_sock_opt_perm(OP_SETSOCKOPT, AA_MAY_SETOPT, sock,
  1260. level, optname);
  1261. }
  1262. static int apparmor_socket_shutdown(struct socket *sock, int how)
  1263. {
  1264. return aa_sock_perm(OP_SHUTDOWN, AA_MAY_SHUTDOWN, sock);
  1265. }
  1266. #ifdef CONFIG_NETWORK_SECMARK
  1267. /**
  1268. * apparmor_socket_sock_rcv_skb - check perms before associating skb to sk
  1269. * @sk: sk to associate @skb with
  1270. * @skb: skb to check for perms
  1271. *
  1272. * Note: can not sleep may be called with locks held
  1273. *
  1274. * dont want protocol specific in __skb_recv_datagram()
  1275. * to deny an incoming connection socket_sock_rcv_skb()
  1276. */
  1277. static int apparmor_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
  1278. {
  1279. struct aa_sk_ctx *ctx = aa_sock(sk);
  1280. int error;
  1281. if (!skb->secmark)
  1282. return 0;
  1283. /*
  1284. * If reach here before socket_post_create hook is called, in which
  1285. * case label is null, drop the packet.
  1286. */
  1287. if (!rcu_access_pointer(ctx->label))
  1288. return -EACCES;
  1289. rcu_read_lock();
  1290. error = apparmor_secmark_check(rcu_dereference(ctx->label), OP_RECVMSG,
  1291. AA_MAY_RECEIVE, skb->secmark, sk);
  1292. rcu_read_unlock();
  1293. return error;
  1294. }
  1295. #endif
  1296. static struct aa_label *sk_peer_get_label(struct sock *sk)
  1297. {
  1298. struct aa_sk_ctx *ctx = aa_sock(sk);
  1299. struct aa_label *label = ERR_PTR(-ENOPROTOOPT);
  1300. if (rcu_access_pointer(ctx->peer))
  1301. return aa_get_label_rcu(&ctx->peer);
  1302. if (sk->sk_family != PF_UNIX)
  1303. return ERR_PTR(-ENOPROTOOPT);
  1304. return label;
  1305. }
  1306. /**
  1307. * apparmor_socket_getpeersec_stream - get security context of peer
  1308. * @sock: socket that we are trying to get the peer context of
  1309. * @optval: output - buffer to copy peer name to
  1310. * @optlen: output - size of copied name in @optval
  1311. * @len: size of @optval buffer
  1312. * Returns: 0 on success, -errno of failure
  1313. *
  1314. * Note: for tcp only valid if using ipsec or cipso on lan
  1315. */
  1316. static int apparmor_socket_getpeersec_stream(struct socket *sock,
  1317. sockptr_t optval, sockptr_t optlen,
  1318. unsigned int len)
  1319. {
  1320. char *name = NULL;
  1321. int slen, error = 0;
  1322. struct aa_label *label;
  1323. struct aa_label *peer;
  1324. peer = sk_peer_get_label(sock->sk);
  1325. if (IS_ERR(peer)) {
  1326. error = PTR_ERR(peer);
  1327. goto done;
  1328. }
  1329. label = begin_current_label_crit_section();
  1330. slen = aa_label_asxprint(&name, labels_ns(label), peer,
  1331. FLAG_SHOW_MODE | FLAG_VIEW_SUBNS |
  1332. FLAG_HIDDEN_UNCONFINED, GFP_KERNEL);
  1333. /* don't include terminating \0 in slen, it breaks some apps */
  1334. if (slen < 0) {
  1335. error = -ENOMEM;
  1336. goto done_put;
  1337. }
  1338. if (slen > len) {
  1339. error = -ERANGE;
  1340. goto done_len;
  1341. }
  1342. if (copy_to_sockptr(optval, name, slen))
  1343. error = -EFAULT;
  1344. done_len:
  1345. if (copy_to_sockptr(optlen, &slen, sizeof(slen)))
  1346. error = -EFAULT;
  1347. done_put:
  1348. end_current_label_crit_section(label);
  1349. aa_put_label(peer);
  1350. done:
  1351. kfree(name);
  1352. return error;
  1353. }
  1354. /**
  1355. * apparmor_socket_getpeersec_dgram - get security label of packet
  1356. * @sock: the peer socket
  1357. * @skb: packet data
  1358. * @secid: pointer to where to put the secid of the packet
  1359. *
  1360. * Sets the netlabel socket state on sk from parent
  1361. */
  1362. static int apparmor_socket_getpeersec_dgram(struct socket *sock,
  1363. struct sk_buff *skb, u32 *secid)
  1364. {
  1365. /* TODO: requires secid support */
  1366. return -ENOPROTOOPT;
  1367. }
  1368. /**
  1369. * apparmor_sock_graft - Initialize newly created socket
  1370. * @sk: child sock
  1371. * @parent: parent socket
  1372. *
  1373. * Note: could set off of SOCK_CTX(parent) but need to track inode and we can
  1374. * just set sk security information off of current creating process label
  1375. * Labeling of sk for accept case - probably should be sock based
  1376. * instead of task, because of the case where an implicitly labeled
  1377. * socket is shared by different tasks.
  1378. */
  1379. static void apparmor_sock_graft(struct sock *sk, struct socket *parent)
  1380. {
  1381. struct aa_sk_ctx *ctx = aa_sock(sk);
  1382. /* setup - not live */
  1383. if (!rcu_access_pointer(ctx->label))
  1384. rcu_assign_pointer(ctx->label, aa_get_current_label());
  1385. }
  1386. #ifdef CONFIG_NETWORK_SECMARK
  1387. static int apparmor_inet_conn_request(const struct sock *sk, struct sk_buff *skb,
  1388. struct request_sock *req)
  1389. {
  1390. struct aa_sk_ctx *ctx = aa_sock(sk);
  1391. int error;
  1392. if (!skb->secmark)
  1393. return 0;
  1394. rcu_read_lock();
  1395. error = apparmor_secmark_check(rcu_dereference(ctx->label), OP_CONNECT,
  1396. AA_MAY_CONNECT, skb->secmark, sk);
  1397. rcu_read_unlock();
  1398. return error;
  1399. }
  1400. #endif
  1401. /*
  1402. * The cred blob is a pointer to, not an instance of, an aa_label.
  1403. */
  1404. struct lsm_blob_sizes apparmor_blob_sizes __ro_after_init = {
  1405. .lbs_cred = sizeof(struct aa_label *),
  1406. .lbs_file = sizeof(struct aa_file_ctx),
  1407. .lbs_task = sizeof(struct aa_task_ctx),
  1408. .lbs_sock = sizeof(struct aa_sk_ctx),
  1409. };
  1410. static const struct lsm_id apparmor_lsmid = {
  1411. .name = "apparmor",
  1412. .id = LSM_ID_APPARMOR,
  1413. };
  1414. static struct security_hook_list apparmor_hooks[] __ro_after_init = {
  1415. LSM_HOOK_INIT(ptrace_access_check, apparmor_ptrace_access_check),
  1416. LSM_HOOK_INIT(ptrace_traceme, apparmor_ptrace_traceme),
  1417. LSM_HOOK_INIT(capget, apparmor_capget),
  1418. LSM_HOOK_INIT(capable, apparmor_capable),
  1419. LSM_HOOK_INIT(move_mount, apparmor_move_mount),
  1420. LSM_HOOK_INIT(sb_mount, apparmor_sb_mount),
  1421. LSM_HOOK_INIT(sb_umount, apparmor_sb_umount),
  1422. LSM_HOOK_INIT(sb_pivotroot, apparmor_sb_pivotroot),
  1423. LSM_HOOK_INIT(path_link, apparmor_path_link),
  1424. LSM_HOOK_INIT(path_unlink, apparmor_path_unlink),
  1425. LSM_HOOK_INIT(path_symlink, apparmor_path_symlink),
  1426. LSM_HOOK_INIT(path_mkdir, apparmor_path_mkdir),
  1427. LSM_HOOK_INIT(path_rmdir, apparmor_path_rmdir),
  1428. LSM_HOOK_INIT(path_mknod, apparmor_path_mknod),
  1429. LSM_HOOK_INIT(path_rename, apparmor_path_rename),
  1430. LSM_HOOK_INIT(path_chmod, apparmor_path_chmod),
  1431. LSM_HOOK_INIT(path_chown, apparmor_path_chown),
  1432. LSM_HOOK_INIT(path_truncate, apparmor_path_truncate),
  1433. LSM_HOOK_INIT(inode_getattr, apparmor_inode_getattr),
  1434. LSM_HOOK_INIT(file_open, apparmor_file_open),
  1435. LSM_HOOK_INIT(file_receive, apparmor_file_receive),
  1436. LSM_HOOK_INIT(file_permission, apparmor_file_permission),
  1437. LSM_HOOK_INIT(file_alloc_security, apparmor_file_alloc_security),
  1438. LSM_HOOK_INIT(file_free_security, apparmor_file_free_security),
  1439. LSM_HOOK_INIT(mmap_file, apparmor_mmap_file),
  1440. LSM_HOOK_INIT(file_mprotect, apparmor_file_mprotect),
  1441. LSM_HOOK_INIT(file_lock, apparmor_file_lock),
  1442. LSM_HOOK_INIT(file_truncate, apparmor_file_truncate),
  1443. LSM_HOOK_INIT(getselfattr, apparmor_getselfattr),
  1444. LSM_HOOK_INIT(setselfattr, apparmor_setselfattr),
  1445. LSM_HOOK_INIT(getprocattr, apparmor_getprocattr),
  1446. LSM_HOOK_INIT(setprocattr, apparmor_setprocattr),
  1447. LSM_HOOK_INIT(sk_alloc_security, apparmor_sk_alloc_security),
  1448. LSM_HOOK_INIT(sk_free_security, apparmor_sk_free_security),
  1449. LSM_HOOK_INIT(sk_clone_security, apparmor_sk_clone_security),
  1450. LSM_HOOK_INIT(unix_stream_connect, apparmor_unix_stream_connect),
  1451. LSM_HOOK_INIT(unix_may_send, apparmor_unix_may_send),
  1452. LSM_HOOK_INIT(socket_create, apparmor_socket_create),
  1453. LSM_HOOK_INIT(socket_post_create, apparmor_socket_post_create),
  1454. LSM_HOOK_INIT(socket_socketpair, apparmor_socket_socketpair),
  1455. LSM_HOOK_INIT(socket_bind, apparmor_socket_bind),
  1456. LSM_HOOK_INIT(socket_connect, apparmor_socket_connect),
  1457. LSM_HOOK_INIT(socket_listen, apparmor_socket_listen),
  1458. LSM_HOOK_INIT(socket_accept, apparmor_socket_accept),
  1459. LSM_HOOK_INIT(socket_sendmsg, apparmor_socket_sendmsg),
  1460. LSM_HOOK_INIT(socket_recvmsg, apparmor_socket_recvmsg),
  1461. LSM_HOOK_INIT(socket_getsockname, apparmor_socket_getsockname),
  1462. LSM_HOOK_INIT(socket_getpeername, apparmor_socket_getpeername),
  1463. LSM_HOOK_INIT(socket_getsockopt, apparmor_socket_getsockopt),
  1464. LSM_HOOK_INIT(socket_setsockopt, apparmor_socket_setsockopt),
  1465. LSM_HOOK_INIT(socket_shutdown, apparmor_socket_shutdown),
  1466. #ifdef CONFIG_NETWORK_SECMARK
  1467. LSM_HOOK_INIT(socket_sock_rcv_skb, apparmor_socket_sock_rcv_skb),
  1468. #endif
  1469. LSM_HOOK_INIT(socket_getpeersec_stream,
  1470. apparmor_socket_getpeersec_stream),
  1471. LSM_HOOK_INIT(socket_getpeersec_dgram,
  1472. apparmor_socket_getpeersec_dgram),
  1473. LSM_HOOK_INIT(sock_graft, apparmor_sock_graft),
  1474. #ifdef CONFIG_NETWORK_SECMARK
  1475. LSM_HOOK_INIT(inet_conn_request, apparmor_inet_conn_request),
  1476. #endif
  1477. LSM_HOOK_INIT(cred_alloc_blank, apparmor_cred_alloc_blank),
  1478. LSM_HOOK_INIT(cred_free, apparmor_cred_free),
  1479. LSM_HOOK_INIT(cred_prepare, apparmor_cred_prepare),
  1480. LSM_HOOK_INIT(cred_transfer, apparmor_cred_transfer),
  1481. LSM_HOOK_INIT(bprm_creds_for_exec, apparmor_bprm_creds_for_exec),
  1482. LSM_HOOK_INIT(bprm_committing_creds, apparmor_bprm_committing_creds),
  1483. LSM_HOOK_INIT(bprm_committed_creds, apparmor_bprm_committed_creds),
  1484. LSM_HOOK_INIT(task_free, apparmor_task_free),
  1485. LSM_HOOK_INIT(task_alloc, apparmor_task_alloc),
  1486. LSM_HOOK_INIT(current_getlsmprop_subj,
  1487. apparmor_current_getlsmprop_subj),
  1488. LSM_HOOK_INIT(task_getlsmprop_obj, apparmor_task_getlsmprop_obj),
  1489. LSM_HOOK_INIT(task_setrlimit, apparmor_task_setrlimit),
  1490. LSM_HOOK_INIT(task_kill, apparmor_task_kill),
  1491. LSM_HOOK_INIT(userns_create, apparmor_userns_create),
  1492. #ifdef CONFIG_AUDIT
  1493. LSM_HOOK_INIT(audit_rule_init, aa_audit_rule_init),
  1494. LSM_HOOK_INIT(audit_rule_known, aa_audit_rule_known),
  1495. LSM_HOOK_INIT(audit_rule_match, aa_audit_rule_match),
  1496. LSM_HOOK_INIT(audit_rule_free, aa_audit_rule_free),
  1497. #endif
  1498. LSM_HOOK_INIT(secid_to_secctx, apparmor_secid_to_secctx),
  1499. LSM_HOOK_INIT(lsmprop_to_secctx, apparmor_lsmprop_to_secctx),
  1500. LSM_HOOK_INIT(secctx_to_secid, apparmor_secctx_to_secid),
  1501. LSM_HOOK_INIT(release_secctx, apparmor_release_secctx),
  1502. #ifdef CONFIG_IO_URING
  1503. LSM_HOOK_INIT(uring_override_creds, apparmor_uring_override_creds),
  1504. LSM_HOOK_INIT(uring_sqpoll, apparmor_uring_sqpoll),
  1505. #endif
  1506. };
  1507. /*
  1508. * AppArmor sysfs module parameters
  1509. */
  1510. static int param_set_aabool(const char *val, const struct kernel_param *kp);
  1511. static int param_get_aabool(char *buffer, const struct kernel_param *kp);
  1512. #define param_check_aabool param_check_bool
  1513. static const struct kernel_param_ops param_ops_aabool = {
  1514. .flags = KERNEL_PARAM_OPS_FL_NOARG,
  1515. .set = param_set_aabool,
  1516. .get = param_get_aabool
  1517. };
  1518. static int param_set_aauint(const char *val, const struct kernel_param *kp);
  1519. static int param_get_aauint(char *buffer, const struct kernel_param *kp);
  1520. #define param_check_aauint param_check_uint
  1521. static const struct kernel_param_ops param_ops_aauint = {
  1522. .set = param_set_aauint,
  1523. .get = param_get_aauint
  1524. };
  1525. static int param_set_aacompressionlevel(const char *val,
  1526. const struct kernel_param *kp);
  1527. static int param_get_aacompressionlevel(char *buffer,
  1528. const struct kernel_param *kp);
  1529. #define param_check_aacompressionlevel param_check_int
  1530. static const struct kernel_param_ops param_ops_aacompressionlevel = {
  1531. .set = param_set_aacompressionlevel,
  1532. .get = param_get_aacompressionlevel
  1533. };
  1534. static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp);
  1535. static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp);
  1536. #define param_check_aalockpolicy param_check_bool
  1537. static const struct kernel_param_ops param_ops_aalockpolicy = {
  1538. .flags = KERNEL_PARAM_OPS_FL_NOARG,
  1539. .set = param_set_aalockpolicy,
  1540. .get = param_get_aalockpolicy
  1541. };
  1542. static int param_set_debug(const char *val, const struct kernel_param *kp);
  1543. static int param_get_debug(char *buffer, const struct kernel_param *kp);
  1544. static int param_set_audit(const char *val, const struct kernel_param *kp);
  1545. static int param_get_audit(char *buffer, const struct kernel_param *kp);
  1546. static int param_set_mode(const char *val, const struct kernel_param *kp);
  1547. static int param_get_mode(char *buffer, const struct kernel_param *kp);
  1548. /* Flag values, also controllable via /sys/module/apparmor/parameters
  1549. * We define special types as we want to do additional mediation.
  1550. */
  1551. /* AppArmor global enforcement switch - complain, enforce, kill */
  1552. enum profile_mode aa_g_profile_mode = APPARMOR_ENFORCE;
  1553. module_param_call(mode, param_set_mode, param_get_mode,
  1554. &aa_g_profile_mode, S_IRUSR | S_IWUSR);
  1555. /* whether policy verification hashing is enabled */
  1556. bool aa_g_hash_policy = IS_ENABLED(CONFIG_SECURITY_APPARMOR_HASH_DEFAULT);
  1557. #ifdef CONFIG_SECURITY_APPARMOR_HASH
  1558. module_param_named(hash_policy, aa_g_hash_policy, aabool, S_IRUSR | S_IWUSR);
  1559. #endif
  1560. /* whether policy exactly as loaded is retained for debug and checkpointing */
  1561. bool aa_g_export_binary = IS_ENABLED(CONFIG_SECURITY_APPARMOR_EXPORT_BINARY);
  1562. #ifdef CONFIG_SECURITY_APPARMOR_EXPORT_BINARY
  1563. module_param_named(export_binary, aa_g_export_binary, aabool, 0600);
  1564. #endif
  1565. /* policy loaddata compression level */
  1566. int aa_g_rawdata_compression_level = AA_DEFAULT_CLEVEL;
  1567. module_param_named(rawdata_compression_level, aa_g_rawdata_compression_level,
  1568. aacompressionlevel, 0400);
  1569. /* Debug mode */
  1570. int aa_g_debug;
  1571. module_param_call(debug, param_set_debug, param_get_debug,
  1572. &aa_g_debug, 0600);
  1573. /* Audit mode */
  1574. enum audit_mode aa_g_audit;
  1575. module_param_call(audit, param_set_audit, param_get_audit,
  1576. &aa_g_audit, S_IRUSR | S_IWUSR);
  1577. /* Determines if audit header is included in audited messages. This
  1578. * provides more context if the audit daemon is not running
  1579. */
  1580. bool aa_g_audit_header = true;
  1581. module_param_named(audit_header, aa_g_audit_header, aabool,
  1582. S_IRUSR | S_IWUSR);
  1583. /* lock out loading/removal of policy
  1584. * TODO: add in at boot loading of policy, which is the only way to
  1585. * load policy, if lock_policy is set
  1586. */
  1587. bool aa_g_lock_policy;
  1588. module_param_named(lock_policy, aa_g_lock_policy, aalockpolicy,
  1589. S_IRUSR | S_IWUSR);
  1590. /* Syscall logging mode */
  1591. bool aa_g_logsyscall;
  1592. module_param_named(logsyscall, aa_g_logsyscall, aabool, S_IRUSR | S_IWUSR);
  1593. /* Maximum pathname length before accesses will start getting rejected */
  1594. unsigned int aa_g_path_max = 2 * PATH_MAX;
  1595. module_param_named(path_max, aa_g_path_max, aauint, S_IRUSR);
  1596. /* Determines how paranoid loading of policy is and how much verification
  1597. * on the loaded policy is done.
  1598. * DEPRECATED: read only as strict checking of load is always done now
  1599. * that none root users (user namespaces) can load policy.
  1600. */
  1601. bool aa_g_paranoid_load = IS_ENABLED(CONFIG_SECURITY_APPARMOR_PARANOID_LOAD);
  1602. module_param_named(paranoid_load, aa_g_paranoid_load, aabool, S_IRUGO);
  1603. static int param_get_aaintbool(char *buffer, const struct kernel_param *kp);
  1604. static int param_set_aaintbool(const char *val, const struct kernel_param *kp);
  1605. #define param_check_aaintbool param_check_int
  1606. static const struct kernel_param_ops param_ops_aaintbool = {
  1607. .set = param_set_aaintbool,
  1608. .get = param_get_aaintbool
  1609. };
  1610. /* Boot time disable flag */
  1611. static int apparmor_enabled __ro_after_init = 1;
  1612. module_param_named(enabled, apparmor_enabled, aaintbool, 0444);
  1613. static int __init apparmor_enabled_setup(char *str)
  1614. {
  1615. unsigned long enabled;
  1616. int error = kstrtoul(str, 0, &enabled);
  1617. if (!error)
  1618. apparmor_enabled = enabled ? 1 : 0;
  1619. return 1;
  1620. }
  1621. __setup("apparmor=", apparmor_enabled_setup);
  1622. /* set global flag turning off the ability to load policy */
  1623. static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp)
  1624. {
  1625. if (!apparmor_enabled)
  1626. return -EINVAL;
  1627. if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
  1628. return -EPERM;
  1629. return param_set_bool(val, kp);
  1630. }
  1631. static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp)
  1632. {
  1633. if (!apparmor_enabled)
  1634. return -EINVAL;
  1635. if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
  1636. return -EPERM;
  1637. return param_get_bool(buffer, kp);
  1638. }
  1639. static int param_set_aabool(const char *val, const struct kernel_param *kp)
  1640. {
  1641. if (!apparmor_enabled)
  1642. return -EINVAL;
  1643. if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
  1644. return -EPERM;
  1645. return param_set_bool(val, kp);
  1646. }
  1647. static int param_get_aabool(char *buffer, const struct kernel_param *kp)
  1648. {
  1649. if (!apparmor_enabled)
  1650. return -EINVAL;
  1651. if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
  1652. return -EPERM;
  1653. return param_get_bool(buffer, kp);
  1654. }
  1655. static int param_set_aauint(const char *val, const struct kernel_param *kp)
  1656. {
  1657. int error;
  1658. if (!apparmor_enabled)
  1659. return -EINVAL;
  1660. /* file is ro but enforce 2nd line check */
  1661. if (apparmor_initialized)
  1662. return -EPERM;
  1663. error = param_set_uint(val, kp);
  1664. aa_g_path_max = max_t(uint32_t, aa_g_path_max, sizeof(union aa_buffer));
  1665. pr_info("AppArmor: buffer size set to %d bytes\n", aa_g_path_max);
  1666. return error;
  1667. }
  1668. static int param_get_aauint(char *buffer, const struct kernel_param *kp)
  1669. {
  1670. if (!apparmor_enabled)
  1671. return -EINVAL;
  1672. if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
  1673. return -EPERM;
  1674. return param_get_uint(buffer, kp);
  1675. }
  1676. /* Can only be set before AppArmor is initialized (i.e. on boot cmdline). */
  1677. static int param_set_aaintbool(const char *val, const struct kernel_param *kp)
  1678. {
  1679. struct kernel_param kp_local;
  1680. bool value;
  1681. int error;
  1682. if (apparmor_initialized)
  1683. return -EPERM;
  1684. /* Create local copy, with arg pointing to bool type. */
  1685. value = !!*((int *)kp->arg);
  1686. memcpy(&kp_local, kp, sizeof(kp_local));
  1687. kp_local.arg = &value;
  1688. error = param_set_bool(val, &kp_local);
  1689. if (!error)
  1690. *((int *)kp->arg) = *((bool *)kp_local.arg);
  1691. return error;
  1692. }
  1693. /*
  1694. * To avoid changing /sys/module/apparmor/parameters/enabled from Y/N to
  1695. * 1/0, this converts the "int that is actually bool" back to bool for
  1696. * display in the /sys filesystem, while keeping it "int" for the LSM
  1697. * infrastructure.
  1698. */
  1699. static int param_get_aaintbool(char *buffer, const struct kernel_param *kp)
  1700. {
  1701. struct kernel_param kp_local;
  1702. bool value;
  1703. /* Create local copy, with arg pointing to bool type. */
  1704. value = !!*((int *)kp->arg);
  1705. memcpy(&kp_local, kp, sizeof(kp_local));
  1706. kp_local.arg = &value;
  1707. return param_get_bool(buffer, &kp_local);
  1708. }
  1709. static int param_set_aacompressionlevel(const char *val,
  1710. const struct kernel_param *kp)
  1711. {
  1712. int error;
  1713. if (!apparmor_enabled)
  1714. return -EINVAL;
  1715. if (apparmor_initialized)
  1716. return -EPERM;
  1717. error = param_set_int(val, kp);
  1718. aa_g_rawdata_compression_level = clamp(aa_g_rawdata_compression_level,
  1719. AA_MIN_CLEVEL, AA_MAX_CLEVEL);
  1720. pr_info("AppArmor: policy rawdata compression level set to %d\n",
  1721. aa_g_rawdata_compression_level);
  1722. return error;
  1723. }
  1724. static int param_get_aacompressionlevel(char *buffer,
  1725. const struct kernel_param *kp)
  1726. {
  1727. if (!apparmor_enabled)
  1728. return -EINVAL;
  1729. if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
  1730. return -EPERM;
  1731. return param_get_int(buffer, kp);
  1732. }
  1733. static int param_get_debug(char *buffer, const struct kernel_param *kp)
  1734. {
  1735. if (!apparmor_enabled)
  1736. return -EINVAL;
  1737. if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
  1738. return -EPERM;
  1739. return aa_print_debug_params(buffer);
  1740. }
  1741. static int param_set_debug(const char *val, const struct kernel_param *kp)
  1742. {
  1743. int i;
  1744. if (!apparmor_enabled)
  1745. return -EINVAL;
  1746. if (!val)
  1747. return -EINVAL;
  1748. if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
  1749. return -EPERM;
  1750. i = aa_parse_debug_params(val);
  1751. if (i == DEBUG_PARSE_ERROR)
  1752. return -EINVAL;
  1753. aa_g_debug = i;
  1754. return 0;
  1755. }
  1756. static int param_get_audit(char *buffer, const struct kernel_param *kp)
  1757. {
  1758. if (!apparmor_enabled)
  1759. return -EINVAL;
  1760. if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
  1761. return -EPERM;
  1762. return sprintf(buffer, "%s", audit_mode_names[aa_g_audit]);
  1763. }
  1764. static int param_set_audit(const char *val, const struct kernel_param *kp)
  1765. {
  1766. int i;
  1767. if (!apparmor_enabled)
  1768. return -EINVAL;
  1769. if (!val)
  1770. return -EINVAL;
  1771. if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
  1772. return -EPERM;
  1773. i = match_string(audit_mode_names, AUDIT_MAX_INDEX, val);
  1774. if (i < 0)
  1775. return -EINVAL;
  1776. aa_g_audit = i;
  1777. return 0;
  1778. }
  1779. static int param_get_mode(char *buffer, const struct kernel_param *kp)
  1780. {
  1781. if (!apparmor_enabled)
  1782. return -EINVAL;
  1783. if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
  1784. return -EPERM;
  1785. return sprintf(buffer, "%s", aa_profile_mode_names[aa_g_profile_mode]);
  1786. }
  1787. static int param_set_mode(const char *val, const struct kernel_param *kp)
  1788. {
  1789. int i;
  1790. if (!apparmor_enabled)
  1791. return -EINVAL;
  1792. if (!val)
  1793. return -EINVAL;
  1794. if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
  1795. return -EPERM;
  1796. i = match_string(aa_profile_mode_names, APPARMOR_MODE_NAMES_MAX_INDEX,
  1797. val);
  1798. if (i < 0)
  1799. return -EINVAL;
  1800. aa_g_profile_mode = i;
  1801. return 0;
  1802. }
  1803. /* arbitrary cap on how long to hold buffer because contention was
  1804. * encountered before trying to put it back into the global pool
  1805. */
  1806. #define MAX_HOLD_COUNT 64
  1807. /* the hold count is a heuristic for lock contention, and can be
  1808. * incremented async to actual buffer alloc/free. Because buffers
  1809. * may be put back onto a percpu cache different than the ->hold was
  1810. * added to the counts can be out of sync. Guard against underflow
  1811. * and overflow
  1812. */
  1813. static void cache_hold_inc(unsigned int *hold)
  1814. {
  1815. if (*hold > MAX_HOLD_COUNT)
  1816. (*hold)++;
  1817. }
  1818. char *aa_get_buffer(bool in_atomic)
  1819. {
  1820. union aa_buffer *aa_buf;
  1821. struct aa_local_cache *cache;
  1822. bool try_again = true;
  1823. gfp_t flags = (GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
  1824. /* use per cpu cached buffers first */
  1825. cache = get_cpu_ptr(&aa_local_buffers);
  1826. if (!list_empty(&cache->head)) {
  1827. aa_buf = list_first_entry(&cache->head, union aa_buffer, list);
  1828. list_del(&aa_buf->list);
  1829. if (cache->hold)
  1830. cache->hold--;
  1831. cache->count--;
  1832. put_cpu_ptr(&aa_local_buffers);
  1833. return &aa_buf->buffer[0];
  1834. }
  1835. /* exit percpu as spinlocks may sleep on realtime kernels */
  1836. put_cpu_ptr(&aa_local_buffers);
  1837. if (!spin_trylock(&aa_buffers_lock)) {
  1838. /* had contention on lock so increase hold count. Doesn't
  1839. * really matter if recorded before or after the spin lock
  1840. * as there is no way to guarantee the buffer will be put
  1841. * back on the same percpu cache. Instead rely on holds
  1842. * roughly averaging out over time.
  1843. */
  1844. cache = get_cpu_ptr(&aa_local_buffers);
  1845. cache_hold_inc(&cache->hold);
  1846. put_cpu_ptr(&aa_local_buffers);
  1847. spin_lock(&aa_buffers_lock);
  1848. }
  1849. retry:
  1850. if (buffer_count > reserve_count ||
  1851. (in_atomic && !list_empty(&aa_global_buffers))) {
  1852. aa_buf = list_first_entry(&aa_global_buffers, union aa_buffer,
  1853. list);
  1854. list_del(&aa_buf->list);
  1855. buffer_count--;
  1856. spin_unlock(&aa_buffers_lock);
  1857. return aa_buf->buffer;
  1858. }
  1859. if (in_atomic) {
  1860. /*
  1861. * out of reserve buffers and in atomic context so increase
  1862. * how many buffers to keep in reserve
  1863. */
  1864. reserve_count++;
  1865. flags = GFP_ATOMIC;
  1866. }
  1867. spin_unlock(&aa_buffers_lock);
  1868. if (!in_atomic)
  1869. might_sleep();
  1870. aa_buf = kmalloc(aa_g_path_max, flags);
  1871. if (!aa_buf) {
  1872. if (try_again) {
  1873. try_again = false;
  1874. spin_lock(&aa_buffers_lock);
  1875. goto retry;
  1876. }
  1877. pr_warn_once("AppArmor: Failed to allocate a memory buffer.\n");
  1878. return NULL;
  1879. }
  1880. return aa_buf->buffer;
  1881. }
  1882. void aa_put_buffer(char *buf)
  1883. {
  1884. union aa_buffer *aa_buf;
  1885. struct aa_local_cache *cache;
  1886. if (!buf)
  1887. return;
  1888. aa_buf = container_of(buf, union aa_buffer, buffer[0]);
  1889. cache = get_cpu_ptr(&aa_local_buffers);
  1890. if (!cache->hold) {
  1891. put_cpu_ptr(&aa_local_buffers);
  1892. if (spin_trylock(&aa_buffers_lock)) {
  1893. /* put back on global list */
  1894. list_add(&aa_buf->list, &aa_global_buffers);
  1895. buffer_count++;
  1896. spin_unlock(&aa_buffers_lock);
  1897. return;
  1898. }
  1899. /* contention on global list, fallback to percpu */
  1900. cache = get_cpu_ptr(&aa_local_buffers);
  1901. cache_hold_inc(&cache->hold);
  1902. }
  1903. /* cache in percpu list */
  1904. list_add(&aa_buf->list, &cache->head);
  1905. cache->count++;
  1906. put_cpu_ptr(&aa_local_buffers);
  1907. }
  1908. /*
  1909. * AppArmor init functions
  1910. */
  1911. /**
  1912. * set_init_ctx - set a task context and profile on the first task.
  1913. *
  1914. * TODO: allow setting an alternate profile than unconfined
  1915. */
  1916. static int __init set_init_ctx(void)
  1917. {
  1918. struct cred *cred = (__force struct cred *)current->real_cred;
  1919. set_cred_label(cred, aa_get_label(ns_unconfined(root_ns)));
  1920. return 0;
  1921. }
  1922. static void destroy_buffers(void)
  1923. {
  1924. union aa_buffer *aa_buf;
  1925. spin_lock(&aa_buffers_lock);
  1926. while (!list_empty(&aa_global_buffers)) {
  1927. aa_buf = list_first_entry(&aa_global_buffers, union aa_buffer,
  1928. list);
  1929. list_del(&aa_buf->list);
  1930. spin_unlock(&aa_buffers_lock);
  1931. kfree(aa_buf);
  1932. spin_lock(&aa_buffers_lock);
  1933. }
  1934. spin_unlock(&aa_buffers_lock);
  1935. }
  1936. static int __init alloc_buffers(void)
  1937. {
  1938. union aa_buffer *aa_buf;
  1939. int i, num;
  1940. /*
  1941. * per cpu set of cached allocated buffers used to help reduce
  1942. * lock contention
  1943. */
  1944. for_each_possible_cpu(i) {
  1945. per_cpu(aa_local_buffers, i).hold = 0;
  1946. per_cpu(aa_local_buffers, i).count = 0;
  1947. INIT_LIST_HEAD(&per_cpu(aa_local_buffers, i).head);
  1948. }
  1949. /*
  1950. * A function may require two buffers at once. Usually the buffers are
  1951. * used for a short period of time and are shared. On UP kernel buffers
  1952. * two should be enough, with more CPUs it is possible that more
  1953. * buffers will be used simultaneously. The preallocated pool may grow.
  1954. * This preallocation has also the side-effect that AppArmor will be
  1955. * disabled early at boot if aa_g_path_max is extremely high.
  1956. */
  1957. if (num_online_cpus() > 1)
  1958. num = 4 + RESERVE_COUNT;
  1959. else
  1960. num = 2 + RESERVE_COUNT;
  1961. for (i = 0; i < num; i++) {
  1962. aa_buf = kmalloc(aa_g_path_max, GFP_KERNEL |
  1963. __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
  1964. if (!aa_buf) {
  1965. destroy_buffers();
  1966. return -ENOMEM;
  1967. }
  1968. aa_put_buffer(aa_buf->buffer);
  1969. }
  1970. return 0;
  1971. }
  1972. #ifdef CONFIG_SYSCTL
  1973. static int apparmor_dointvec(const struct ctl_table *table, int write,
  1974. void *buffer, size_t *lenp, loff_t *ppos)
  1975. {
  1976. if (!aa_current_policy_admin_capable(NULL))
  1977. return -EPERM;
  1978. if (!apparmor_enabled)
  1979. return -EINVAL;
  1980. return proc_dointvec(table, write, buffer, lenp, ppos);
  1981. }
  1982. static const struct ctl_table apparmor_sysctl_table[] = {
  1983. #ifdef CONFIG_USER_NS
  1984. {
  1985. .procname = "unprivileged_userns_apparmor_policy",
  1986. .data = &unprivileged_userns_apparmor_policy,
  1987. .maxlen = sizeof(int),
  1988. .mode = 0600,
  1989. .proc_handler = apparmor_dointvec,
  1990. },
  1991. #endif /* CONFIG_USER_NS */
  1992. {
  1993. .procname = "apparmor_display_secid_mode",
  1994. .data = &apparmor_display_secid_mode,
  1995. .maxlen = sizeof(int),
  1996. .mode = 0600,
  1997. .proc_handler = apparmor_dointvec,
  1998. },
  1999. {
  2000. .procname = "apparmor_restrict_unprivileged_unconfined",
  2001. .data = &aa_unprivileged_unconfined_restricted,
  2002. .maxlen = sizeof(int),
  2003. .mode = 0600,
  2004. .proc_handler = apparmor_dointvec,
  2005. },
  2006. };
  2007. static int __init apparmor_init_sysctl(void)
  2008. {
  2009. return register_sysctl("kernel", apparmor_sysctl_table) ? 0 : -ENOMEM;
  2010. }
  2011. #else
  2012. static inline int apparmor_init_sysctl(void)
  2013. {
  2014. return 0;
  2015. }
  2016. #endif /* CONFIG_SYSCTL */
  2017. #if defined(CONFIG_NETFILTER) && defined(CONFIG_NETWORK_SECMARK)
  2018. static unsigned int apparmor_ip_postroute(void *priv,
  2019. struct sk_buff *skb,
  2020. const struct nf_hook_state *state)
  2021. {
  2022. struct aa_sk_ctx *ctx;
  2023. struct sock *sk;
  2024. int error;
  2025. if (!skb->secmark)
  2026. return NF_ACCEPT;
  2027. sk = skb_to_full_sk(skb);
  2028. if (sk == NULL)
  2029. return NF_ACCEPT;
  2030. ctx = aa_sock(sk);
  2031. rcu_read_lock();
  2032. error = apparmor_secmark_check(rcu_dereference(ctx->label), OP_SENDMSG,
  2033. AA_MAY_SEND, skb->secmark, sk);
  2034. rcu_read_unlock();
  2035. if (!error)
  2036. return NF_ACCEPT;
  2037. return NF_DROP_ERR(-ECONNREFUSED);
  2038. }
  2039. static const struct nf_hook_ops apparmor_nf_ops[] = {
  2040. {
  2041. .hook = apparmor_ip_postroute,
  2042. .pf = NFPROTO_IPV4,
  2043. .hooknum = NF_INET_POST_ROUTING,
  2044. .priority = NF_IP_PRI_SELINUX_FIRST,
  2045. },
  2046. #if IS_ENABLED(CONFIG_IPV6)
  2047. {
  2048. .hook = apparmor_ip_postroute,
  2049. .pf = NFPROTO_IPV6,
  2050. .hooknum = NF_INET_POST_ROUTING,
  2051. .priority = NF_IP6_PRI_SELINUX_FIRST,
  2052. },
  2053. #endif
  2054. };
  2055. static int __net_init apparmor_nf_register(struct net *net)
  2056. {
  2057. return nf_register_net_hooks(net, apparmor_nf_ops,
  2058. ARRAY_SIZE(apparmor_nf_ops));
  2059. }
  2060. static void __net_exit apparmor_nf_unregister(struct net *net)
  2061. {
  2062. nf_unregister_net_hooks(net, apparmor_nf_ops,
  2063. ARRAY_SIZE(apparmor_nf_ops));
  2064. }
  2065. static struct pernet_operations apparmor_net_ops = {
  2066. .init = apparmor_nf_register,
  2067. .exit = apparmor_nf_unregister,
  2068. };
  2069. static int __init apparmor_nf_ip_init(void)
  2070. {
  2071. int err;
  2072. if (!apparmor_enabled)
  2073. return 0;
  2074. err = register_pernet_subsys(&apparmor_net_ops);
  2075. if (err)
  2076. panic("Apparmor: register_pernet_subsys: error %d\n", err);
  2077. return 0;
  2078. }
  2079. #endif
  2080. static char nulldfa_src[] __aligned(8) = {
  2081. #include "nulldfa.in"
  2082. };
  2083. static struct aa_dfa *nulldfa;
  2084. static char stacksplitdfa_src[] __aligned(8) = {
  2085. #include "stacksplitdfa.in"
  2086. };
  2087. struct aa_dfa *stacksplitdfa;
  2088. struct aa_policydb *nullpdb;
  2089. static int __init aa_setup_dfa_engine(void)
  2090. {
  2091. int error = -ENOMEM;
  2092. nullpdb = aa_alloc_pdb(GFP_KERNEL);
  2093. if (!nullpdb)
  2094. return -ENOMEM;
  2095. nulldfa = aa_dfa_unpack(nulldfa_src, sizeof(nulldfa_src),
  2096. TO_ACCEPT1_FLAG(YYTD_DATA32) |
  2097. TO_ACCEPT2_FLAG(YYTD_DATA32));
  2098. if (IS_ERR(nulldfa)) {
  2099. error = PTR_ERR(nulldfa);
  2100. goto fail;
  2101. }
  2102. nullpdb->dfa = aa_get_dfa(nulldfa);
  2103. nullpdb->perms = kzalloc_objs(struct aa_perms, 2);
  2104. if (!nullpdb->perms)
  2105. goto fail;
  2106. nullpdb->size = 2;
  2107. stacksplitdfa = aa_dfa_unpack(stacksplitdfa_src,
  2108. sizeof(stacksplitdfa_src),
  2109. TO_ACCEPT1_FLAG(YYTD_DATA32) |
  2110. TO_ACCEPT2_FLAG(YYTD_DATA32));
  2111. if (IS_ERR(stacksplitdfa)) {
  2112. error = PTR_ERR(stacksplitdfa);
  2113. goto fail;
  2114. }
  2115. return 0;
  2116. fail:
  2117. aa_put_pdb(nullpdb);
  2118. aa_put_dfa(nulldfa);
  2119. nullpdb = NULL;
  2120. nulldfa = NULL;
  2121. stacksplitdfa = NULL;
  2122. return error;
  2123. }
  2124. static void __init aa_teardown_dfa_engine(void)
  2125. {
  2126. aa_put_dfa(stacksplitdfa);
  2127. aa_put_dfa(nulldfa);
  2128. aa_put_pdb(nullpdb);
  2129. nullpdb = NULL;
  2130. stacksplitdfa = NULL;
  2131. nulldfa = NULL;
  2132. }
  2133. static int __init apparmor_init(void)
  2134. {
  2135. int error;
  2136. error = aa_setup_dfa_engine();
  2137. if (error) {
  2138. AA_ERROR("Unable to setup dfa engine\n");
  2139. goto alloc_out;
  2140. }
  2141. error = aa_alloc_root_ns();
  2142. if (error) {
  2143. AA_ERROR("Unable to allocate default profile namespace\n");
  2144. goto alloc_out;
  2145. }
  2146. error = apparmor_init_sysctl();
  2147. if (error) {
  2148. AA_ERROR("Unable to register sysctls\n");
  2149. goto alloc_out;
  2150. }
  2151. error = alloc_buffers();
  2152. if (error) {
  2153. AA_ERROR("Unable to allocate work buffers\n");
  2154. goto alloc_out;
  2155. }
  2156. error = set_init_ctx();
  2157. if (error) {
  2158. AA_ERROR("Failed to set context on init task\n");
  2159. aa_free_root_ns();
  2160. goto buffers_out;
  2161. }
  2162. security_add_hooks(apparmor_hooks, ARRAY_SIZE(apparmor_hooks),
  2163. &apparmor_lsmid);
  2164. /* Inform the audit system that secctx is used */
  2165. audit_cfg_lsm(&apparmor_lsmid, AUDIT_CFG_LSM_SECCTX_SUBJECT);
  2166. /* Report that AppArmor successfully initialized */
  2167. apparmor_initialized = 1;
  2168. if (aa_g_profile_mode == APPARMOR_COMPLAIN)
  2169. aa_info_message("AppArmor initialized: complain mode enabled");
  2170. else if (aa_g_profile_mode == APPARMOR_KILL)
  2171. aa_info_message("AppArmor initialized: kill mode enabled");
  2172. else
  2173. aa_info_message("AppArmor initialized");
  2174. return error;
  2175. buffers_out:
  2176. destroy_buffers();
  2177. alloc_out:
  2178. aa_destroy_aafs();
  2179. aa_teardown_dfa_engine();
  2180. apparmor_enabled = false;
  2181. return error;
  2182. }
  2183. DEFINE_LSM(apparmor) = {
  2184. .id = &apparmor_lsmid,
  2185. .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
  2186. .enabled = &apparmor_enabled,
  2187. .blobs = &apparmor_blob_sizes,
  2188. .init = apparmor_init,
  2189. .initcall_fs = aa_create_aafs,
  2190. #if defined(CONFIG_NETFILTER) && defined(CONFIG_NETWORK_SECMARK)
  2191. .initcall_device = apparmor_nf_ip_init,
  2192. #endif
  2193. #ifdef CONFIG_SECURITY_APPARMOR_HASH
  2194. .initcall_late = init_profile_hash,
  2195. #endif
  2196. };