lsm.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610
  1. /*
  2. * AppArmor security module
  3. *
  4. * This file contains AppArmor LSM hooks.
  5. *
  6. * Copyright (C) 1998-2008 Novell/SUSE
  7. * Copyright 2009-2010 Canonical Ltd.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation, version 2 of the
  12. * License.
  13. */
  14. #include <linux/lsm_hooks.h>
  15. #include <linux/moduleparam.h>
  16. #include <linux/mm.h>
  17. #include <linux/mman.h>
  18. #include <linux/mount.h>
  19. #include <linux/namei.h>
  20. #include <linux/ptrace.h>
  21. #include <linux/ctype.h>
  22. #include <linux/sysctl.h>
  23. #include <linux/audit.h>
  24. #include <linux/user_namespace.h>
  25. #include <net/sock.h>
  26. #include "include/apparmor.h"
  27. #include "include/apparmorfs.h"
  28. #include "include/audit.h"
  29. #include "include/capability.h"
  30. #include "include/cred.h"
  31. #include "include/file.h"
  32. #include "include/ipc.h"
  33. #include "include/net.h"
  34. #include "include/path.h"
  35. #include "include/label.h"
  36. #include "include/policy.h"
  37. #include "include/policy_ns.h"
  38. #include "include/procattr.h"
  39. #include "include/mount.h"
  40. #include "include/secid.h"
  41. /* Flag indicating whether initialization completed */
  42. int apparmor_initialized;
  43. DEFINE_PER_CPU(struct aa_buffers, aa_buffers);
  44. /*
  45. * LSM hook functions
  46. */
  47. /*
  48. * put the associated labels
  49. */
  50. static void apparmor_cred_free(struct cred *cred)
  51. {
  52. aa_put_label(cred_label(cred));
  53. cred_label(cred) = NULL;
  54. }
  55. /*
  56. * allocate the apparmor part of blank credentials
  57. */
  58. static int apparmor_cred_alloc_blank(struct cred *cred, gfp_t gfp)
  59. {
  60. cred_label(cred) = NULL;
  61. return 0;
  62. }
  63. /*
  64. * prepare new cred label for modification by prepare_cred block
  65. */
  66. static int apparmor_cred_prepare(struct cred *new, const struct cred *old,
  67. gfp_t gfp)
  68. {
  69. cred_label(new) = aa_get_newest_label(cred_label(old));
  70. return 0;
  71. }
  72. /*
  73. * transfer the apparmor data to a blank set of creds
  74. */
  75. static void apparmor_cred_transfer(struct cred *new, const struct cred *old)
  76. {
  77. cred_label(new) = aa_get_newest_label(cred_label(old));
  78. }
  79. static void apparmor_task_free(struct task_struct *task)
  80. {
  81. aa_free_task_ctx(task_ctx(task));
  82. task_ctx(task) = NULL;
  83. }
  84. static int apparmor_task_alloc(struct task_struct *task,
  85. unsigned long clone_flags)
  86. {
  87. struct aa_task_ctx *new = aa_alloc_task_ctx(GFP_KERNEL);
  88. if (!new)
  89. return -ENOMEM;
  90. aa_dup_task_ctx(new, task_ctx(current));
  91. task_ctx(task) = new;
  92. return 0;
  93. }
  94. static int apparmor_ptrace_access_check(struct task_struct *child,
  95. unsigned int mode)
  96. {
  97. struct aa_label *tracer, *tracee;
  98. int error;
  99. tracer = __begin_current_label_crit_section();
  100. tracee = aa_get_task_label(child);
  101. error = aa_may_ptrace(tracer, tracee,
  102. (mode & PTRACE_MODE_READ) ? AA_PTRACE_READ
  103. : AA_PTRACE_TRACE);
  104. aa_put_label(tracee);
  105. __end_current_label_crit_section(tracer);
  106. return error;
  107. }
  108. static int apparmor_ptrace_traceme(struct task_struct *parent)
  109. {
  110. struct aa_label *tracer, *tracee;
  111. int error;
  112. tracee = begin_current_label_crit_section();
  113. tracer = aa_get_task_label(parent);
  114. error = aa_may_ptrace(tracer, tracee, AA_PTRACE_TRACE);
  115. aa_put_label(tracer);
  116. end_current_label_crit_section(tracee);
  117. return error;
  118. }
  119. /* Derived from security/commoncap.c:cap_capget */
  120. static int apparmor_capget(struct task_struct *target, kernel_cap_t *effective,
  121. kernel_cap_t *inheritable, kernel_cap_t *permitted)
  122. {
  123. struct aa_label *label;
  124. const struct cred *cred;
  125. rcu_read_lock();
  126. cred = __task_cred(target);
  127. label = aa_get_newest_cred_label(cred);
  128. /*
  129. * cap_capget is stacked ahead of this and will
  130. * initialize effective and permitted.
  131. */
  132. if (!unconfined(label)) {
  133. struct aa_profile *profile;
  134. struct label_it i;
  135. label_for_each_confined(i, label, profile) {
  136. if (COMPLAIN_MODE(profile))
  137. continue;
  138. *effective = cap_intersect(*effective,
  139. profile->caps.allow);
  140. *permitted = cap_intersect(*permitted,
  141. profile->caps.allow);
  142. }
  143. }
  144. rcu_read_unlock();
  145. aa_put_label(label);
  146. return 0;
  147. }
  148. static int apparmor_capable(const struct cred *cred, struct user_namespace *ns,
  149. int cap, unsigned int opts)
  150. {
  151. struct aa_label *label;
  152. int error = 0;
  153. label = aa_get_newest_cred_label(cred);
  154. if (!unconfined(label))
  155. error = aa_capable(label, cap, opts);
  156. aa_put_label(label);
  157. return error;
  158. }
  159. /**
  160. * common_perm - basic common permission check wrapper fn for paths
  161. * @op: operation being checked
  162. * @path: path to check permission of (NOT NULL)
  163. * @mask: requested permissions mask
  164. * @cond: conditional info for the permission request (NOT NULL)
  165. *
  166. * Returns: %0 else error code if error or permission denied
  167. */
  168. static int common_perm(const char *op, const struct path *path, u32 mask,
  169. struct path_cond *cond)
  170. {
  171. struct aa_label *label;
  172. int error = 0;
  173. label = __begin_current_label_crit_section();
  174. if (!unconfined(label))
  175. error = aa_path_perm(op, label, path, 0, mask, cond);
  176. __end_current_label_crit_section(label);
  177. return error;
  178. }
  179. /**
  180. * common_perm_cond - common permission wrapper around inode cond
  181. * @op: operation being checked
  182. * @path: location to check (NOT NULL)
  183. * @mask: requested permissions mask
  184. *
  185. * Returns: %0 else error code if error or permission denied
  186. */
  187. static int common_perm_cond(const char *op, const struct path *path, u32 mask)
  188. {
  189. struct path_cond cond = { d_backing_inode(path->dentry)->i_uid,
  190. d_backing_inode(path->dentry)->i_mode
  191. };
  192. if (!path_mediated_fs(path->dentry))
  193. return 0;
  194. return common_perm(op, path, mask, &cond);
  195. }
  196. /**
  197. * common_perm_dir_dentry - common permission wrapper when path is dir, dentry
  198. * @op: operation being checked
  199. * @dir: directory of the dentry (NOT NULL)
  200. * @dentry: dentry to check (NOT NULL)
  201. * @mask: requested permissions mask
  202. * @cond: conditional info for the permission request (NOT NULL)
  203. *
  204. * Returns: %0 else error code if error or permission denied
  205. */
  206. static int common_perm_dir_dentry(const char *op, const struct path *dir,
  207. struct dentry *dentry, u32 mask,
  208. struct path_cond *cond)
  209. {
  210. struct path path = { .mnt = dir->mnt, .dentry = dentry };
  211. return common_perm(op, &path, mask, cond);
  212. }
  213. /**
  214. * common_perm_rm - common permission wrapper for operations doing rm
  215. * @op: operation being checked
  216. * @dir: directory that the dentry is in (NOT NULL)
  217. * @dentry: dentry being rm'd (NOT NULL)
  218. * @mask: requested permission mask
  219. *
  220. * Returns: %0 else error code if error or permission denied
  221. */
  222. static int common_perm_rm(const char *op, const struct path *dir,
  223. struct dentry *dentry, u32 mask)
  224. {
  225. struct inode *inode = d_backing_inode(dentry);
  226. struct path_cond cond = { };
  227. if (!inode || !path_mediated_fs(dentry))
  228. return 0;
  229. cond.uid = inode->i_uid;
  230. cond.mode = inode->i_mode;
  231. return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
  232. }
  233. /**
  234. * common_perm_create - common permission wrapper for operations doing create
  235. * @op: operation being checked
  236. * @dir: directory that dentry will be created in (NOT NULL)
  237. * @dentry: dentry to create (NOT NULL)
  238. * @mask: request permission mask
  239. * @mode: created file mode
  240. *
  241. * Returns: %0 else error code if error or permission denied
  242. */
  243. static int common_perm_create(const char *op, const struct path *dir,
  244. struct dentry *dentry, u32 mask, umode_t mode)
  245. {
  246. struct path_cond cond = { current_fsuid(), mode };
  247. if (!path_mediated_fs(dir->dentry))
  248. return 0;
  249. return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
  250. }
  251. static int apparmor_path_unlink(const struct path *dir, struct dentry *dentry)
  252. {
  253. return common_perm_rm(OP_UNLINK, dir, dentry, AA_MAY_DELETE);
  254. }
  255. static int apparmor_path_mkdir(const struct path *dir, struct dentry *dentry,
  256. umode_t mode)
  257. {
  258. return common_perm_create(OP_MKDIR, dir, dentry, AA_MAY_CREATE,
  259. S_IFDIR);
  260. }
  261. static int apparmor_path_rmdir(const struct path *dir, struct dentry *dentry)
  262. {
  263. return common_perm_rm(OP_RMDIR, dir, dentry, AA_MAY_DELETE);
  264. }
  265. static int apparmor_path_mknod(const struct path *dir, struct dentry *dentry,
  266. umode_t mode, unsigned int dev)
  267. {
  268. return common_perm_create(OP_MKNOD, dir, dentry, AA_MAY_CREATE, mode);
  269. }
  270. static int apparmor_path_truncate(const struct path *path)
  271. {
  272. return common_perm_cond(OP_TRUNC, path, MAY_WRITE | AA_MAY_SETATTR);
  273. }
  274. static int apparmor_path_symlink(const struct path *dir, struct dentry *dentry,
  275. const char *old_name)
  276. {
  277. return common_perm_create(OP_SYMLINK, dir, dentry, AA_MAY_CREATE,
  278. S_IFLNK);
  279. }
  280. static int apparmor_path_link(struct dentry *old_dentry, const struct path *new_dir,
  281. struct dentry *new_dentry)
  282. {
  283. struct aa_label *label;
  284. int error = 0;
  285. if (!path_mediated_fs(old_dentry))
  286. return 0;
  287. label = begin_current_label_crit_section();
  288. if (!unconfined(label))
  289. error = aa_path_link(label, old_dentry, new_dir, new_dentry);
  290. end_current_label_crit_section(label);
  291. return error;
  292. }
  293. static int apparmor_path_rename(const struct path *old_dir, struct dentry *old_dentry,
  294. const struct path *new_dir, struct dentry *new_dentry)
  295. {
  296. struct aa_label *label;
  297. int error = 0;
  298. if (!path_mediated_fs(old_dentry))
  299. return 0;
  300. label = begin_current_label_crit_section();
  301. if (!unconfined(label)) {
  302. struct path old_path = { .mnt = old_dir->mnt,
  303. .dentry = old_dentry };
  304. struct path new_path = { .mnt = new_dir->mnt,
  305. .dentry = new_dentry };
  306. struct path_cond cond = { d_backing_inode(old_dentry)->i_uid,
  307. d_backing_inode(old_dentry)->i_mode
  308. };
  309. error = aa_path_perm(OP_RENAME_SRC, label, &old_path, 0,
  310. MAY_READ | AA_MAY_GETATTR | MAY_WRITE |
  311. AA_MAY_SETATTR | AA_MAY_DELETE,
  312. &cond);
  313. if (!error)
  314. error = aa_path_perm(OP_RENAME_DEST, label, &new_path,
  315. 0, MAY_WRITE | AA_MAY_SETATTR |
  316. AA_MAY_CREATE, &cond);
  317. }
  318. end_current_label_crit_section(label);
  319. return error;
  320. }
  321. static int apparmor_path_chmod(const struct path *path, umode_t mode)
  322. {
  323. return common_perm_cond(OP_CHMOD, path, AA_MAY_CHMOD);
  324. }
  325. static int apparmor_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
  326. {
  327. return common_perm_cond(OP_CHOWN, path, AA_MAY_CHOWN);
  328. }
  329. static int apparmor_inode_getattr(const struct path *path)
  330. {
  331. return common_perm_cond(OP_GETATTR, path, AA_MAY_GETATTR);
  332. }
  333. static int apparmor_file_open(struct file *file)
  334. {
  335. struct aa_file_ctx *fctx = file_ctx(file);
  336. struct aa_label *label;
  337. int error = 0;
  338. if (!path_mediated_fs(file->f_path.dentry))
  339. return 0;
  340. /* If in exec, permission is handled by bprm hooks.
  341. * Cache permissions granted by the previous exec check, with
  342. * implicit read and executable mmap which are required to
  343. * actually execute the image.
  344. */
  345. if (current->in_execve) {
  346. fctx->allow = MAY_EXEC | MAY_READ | AA_EXEC_MMAP;
  347. return 0;
  348. }
  349. label = aa_get_newest_cred_label(file->f_cred);
  350. if (!unconfined(label)) {
  351. struct inode *inode = file_inode(file);
  352. struct path_cond cond = { inode->i_uid, inode->i_mode };
  353. error = aa_path_perm(OP_OPEN, label, &file->f_path, 0,
  354. aa_map_file_to_perms(file), &cond);
  355. /* todo cache full allowed permissions set and state */
  356. fctx->allow = aa_map_file_to_perms(file);
  357. }
  358. aa_put_label(label);
  359. return error;
  360. }
  361. static int apparmor_file_alloc_security(struct file *file)
  362. {
  363. int error = 0;
  364. /* freed by apparmor_file_free_security */
  365. struct aa_label *label = begin_current_label_crit_section();
  366. file->f_security = aa_alloc_file_ctx(label, GFP_KERNEL);
  367. if (!file_ctx(file))
  368. error = -ENOMEM;
  369. end_current_label_crit_section(label);
  370. return error;
  371. }
  372. static void apparmor_file_free_security(struct file *file)
  373. {
  374. aa_free_file_ctx(file_ctx(file));
  375. }
  376. static int common_file_perm(const char *op, struct file *file, u32 mask)
  377. {
  378. struct aa_label *label;
  379. int error = 0;
  380. /* don't reaudit files closed during inheritance */
  381. if (file->f_path.dentry == aa_null.dentry)
  382. return -EACCES;
  383. label = __begin_current_label_crit_section();
  384. error = aa_file_perm(op, label, file, mask);
  385. __end_current_label_crit_section(label);
  386. return error;
  387. }
  388. static int apparmor_file_receive(struct file *file)
  389. {
  390. return common_file_perm(OP_FRECEIVE, file, aa_map_file_to_perms(file));
  391. }
  392. static int apparmor_file_permission(struct file *file, int mask)
  393. {
  394. return common_file_perm(OP_FPERM, file, mask);
  395. }
  396. static int apparmor_file_lock(struct file *file, unsigned int cmd)
  397. {
  398. u32 mask = AA_MAY_LOCK;
  399. if (cmd == F_WRLCK)
  400. mask |= MAY_WRITE;
  401. return common_file_perm(OP_FLOCK, file, mask);
  402. }
  403. static int common_mmap(const char *op, struct file *file, unsigned long prot,
  404. unsigned long flags)
  405. {
  406. int mask = 0;
  407. if (!file || !file_ctx(file))
  408. return 0;
  409. if (prot & PROT_READ)
  410. mask |= MAY_READ;
  411. /*
  412. * Private mappings don't require write perms since they don't
  413. * write back to the files
  414. */
  415. if ((prot & PROT_WRITE) && !(flags & MAP_PRIVATE))
  416. mask |= MAY_WRITE;
  417. if (prot & PROT_EXEC)
  418. mask |= AA_EXEC_MMAP;
  419. return common_file_perm(op, file, mask);
  420. }
  421. static int apparmor_mmap_file(struct file *file, unsigned long reqprot,
  422. unsigned long prot, unsigned long flags)
  423. {
  424. return common_mmap(OP_FMMAP, file, prot, flags);
  425. }
  426. static int apparmor_file_mprotect(struct vm_area_struct *vma,
  427. unsigned long reqprot, unsigned long prot)
  428. {
  429. return common_mmap(OP_FMPROT, vma->vm_file, prot,
  430. !(vma->vm_flags & VM_SHARED) ? MAP_PRIVATE : 0);
  431. }
  432. static int apparmor_sb_mount(const char *dev_name, const struct path *path,
  433. const char *type, unsigned long flags, void *data)
  434. {
  435. struct aa_label *label;
  436. int error = 0;
  437. /* Discard magic */
  438. if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
  439. flags &= ~MS_MGC_MSK;
  440. flags &= ~AA_MS_IGNORE_MASK;
  441. label = __begin_current_label_crit_section();
  442. if (!unconfined(label)) {
  443. if (flags & MS_REMOUNT)
  444. error = aa_remount(label, path, flags, data);
  445. else if (flags & MS_BIND)
  446. error = aa_bind_mount(label, path, dev_name, flags);
  447. else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE |
  448. MS_UNBINDABLE))
  449. error = aa_mount_change_type(label, path, flags);
  450. else if (flags & MS_MOVE)
  451. error = aa_move_mount(label, path, dev_name);
  452. else
  453. error = aa_new_mount(label, dev_name, path, type,
  454. flags, data);
  455. }
  456. __end_current_label_crit_section(label);
  457. return error;
  458. }
  459. static int apparmor_sb_umount(struct vfsmount *mnt, int flags)
  460. {
  461. struct aa_label *label;
  462. int error = 0;
  463. label = __begin_current_label_crit_section();
  464. if (!unconfined(label))
  465. error = aa_umount(label, mnt, flags);
  466. __end_current_label_crit_section(label);
  467. return error;
  468. }
  469. static int apparmor_sb_pivotroot(const struct path *old_path,
  470. const struct path *new_path)
  471. {
  472. struct aa_label *label;
  473. int error = 0;
  474. label = aa_get_current_label();
  475. if (!unconfined(label))
  476. error = aa_pivotroot(label, old_path, new_path);
  477. aa_put_label(label);
  478. return error;
  479. }
  480. static int apparmor_getprocattr(struct task_struct *task, char *name,
  481. char **value)
  482. {
  483. int error = -ENOENT;
  484. /* released below */
  485. const struct cred *cred = get_task_cred(task);
  486. struct aa_task_ctx *ctx = task_ctx(current);
  487. struct aa_label *label = NULL;
  488. if (strcmp(name, "current") == 0)
  489. label = aa_get_newest_label(cred_label(cred));
  490. else if (strcmp(name, "prev") == 0 && ctx->previous)
  491. label = aa_get_newest_label(ctx->previous);
  492. else if (strcmp(name, "exec") == 0 && ctx->onexec)
  493. label = aa_get_newest_label(ctx->onexec);
  494. else
  495. error = -EINVAL;
  496. if (label)
  497. error = aa_getprocattr(label, value);
  498. aa_put_label(label);
  499. put_cred(cred);
  500. return error;
  501. }
  502. static int apparmor_setprocattr(const char *name, void *value,
  503. size_t size)
  504. {
  505. char *command, *largs = NULL, *args = value;
  506. size_t arg_size;
  507. int error;
  508. DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, OP_SETPROCATTR);
  509. if (size == 0)
  510. return -EINVAL;
  511. /* AppArmor requires that the buffer must be null terminated atm */
  512. if (args[size - 1] != '\0') {
  513. /* null terminate */
  514. largs = args = kmalloc(size + 1, GFP_KERNEL);
  515. if (!args)
  516. return -ENOMEM;
  517. memcpy(args, value, size);
  518. args[size] = '\0';
  519. }
  520. error = -EINVAL;
  521. args = strim(args);
  522. command = strsep(&args, " ");
  523. if (!args)
  524. goto out;
  525. args = skip_spaces(args);
  526. if (!*args)
  527. goto out;
  528. arg_size = size - (args - (largs ? largs : (char *) value));
  529. if (strcmp(name, "current") == 0) {
  530. if (strcmp(command, "changehat") == 0) {
  531. error = aa_setprocattr_changehat(args, arg_size,
  532. AA_CHANGE_NOFLAGS);
  533. } else if (strcmp(command, "permhat") == 0) {
  534. error = aa_setprocattr_changehat(args, arg_size,
  535. AA_CHANGE_TEST);
  536. } else if (strcmp(command, "changeprofile") == 0) {
  537. error = aa_change_profile(args, AA_CHANGE_NOFLAGS);
  538. } else if (strcmp(command, "permprofile") == 0) {
  539. error = aa_change_profile(args, AA_CHANGE_TEST);
  540. } else if (strcmp(command, "stack") == 0) {
  541. error = aa_change_profile(args, AA_CHANGE_STACK);
  542. } else
  543. goto fail;
  544. } else if (strcmp(name, "exec") == 0) {
  545. if (strcmp(command, "exec") == 0)
  546. error = aa_change_profile(args, AA_CHANGE_ONEXEC);
  547. else if (strcmp(command, "stack") == 0)
  548. error = aa_change_profile(args, (AA_CHANGE_ONEXEC |
  549. AA_CHANGE_STACK));
  550. else
  551. goto fail;
  552. } else
  553. /* only support the "current" and "exec" process attributes */
  554. goto fail;
  555. if (!error)
  556. error = size;
  557. out:
  558. kfree(largs);
  559. return error;
  560. fail:
  561. aad(&sa)->label = begin_current_label_crit_section();
  562. aad(&sa)->info = name;
  563. aad(&sa)->error = error = -EINVAL;
  564. aa_audit_msg(AUDIT_APPARMOR_DENIED, &sa, NULL);
  565. end_current_label_crit_section(aad(&sa)->label);
  566. goto out;
  567. }
  568. /**
  569. * apparmor_bprm_committing_creds - do task cleanup on committing new creds
  570. * @bprm: binprm for the exec (NOT NULL)
  571. */
  572. static void apparmor_bprm_committing_creds(struct linux_binprm *bprm)
  573. {
  574. struct aa_label *label = aa_current_raw_label();
  575. struct aa_label *new_label = cred_label(bprm->cred);
  576. /* bail out if unconfined or not changing profile */
  577. if ((new_label->proxy == label->proxy) ||
  578. (unconfined(new_label)))
  579. return;
  580. aa_inherit_files(bprm->cred, current->files);
  581. current->pdeath_signal = 0;
  582. /* reset soft limits and set hard limits for the new label */
  583. __aa_transition_rlimits(label, new_label);
  584. }
  585. /**
  586. * apparmor_bprm_committed_cred - do cleanup after new creds committed
  587. * @bprm: binprm for the exec (NOT NULL)
  588. */
  589. static void apparmor_bprm_committed_creds(struct linux_binprm *bprm)
  590. {
  591. /* clear out temporary/transitional state from the context */
  592. aa_clear_task_ctx_trans(task_ctx(current));
  593. return;
  594. }
  595. static void apparmor_task_getsecid(struct task_struct *p, u32 *secid)
  596. {
  597. struct aa_label *label = aa_get_task_label(p);
  598. *secid = label->secid;
  599. aa_put_label(label);
  600. }
  601. static int apparmor_task_setrlimit(struct task_struct *task,
  602. unsigned int resource, struct rlimit *new_rlim)
  603. {
  604. struct aa_label *label = __begin_current_label_crit_section();
  605. int error = 0;
  606. if (!unconfined(label))
  607. error = aa_task_setrlimit(label, task, resource, new_rlim);
  608. __end_current_label_crit_section(label);
  609. return error;
  610. }
  611. static int apparmor_task_kill(struct task_struct *target, struct siginfo *info,
  612. int sig, const struct cred *cred)
  613. {
  614. struct aa_label *cl, *tl;
  615. int error;
  616. if (cred) {
  617. /*
  618. * Dealing with USB IO specific behavior
  619. */
  620. cl = aa_get_newest_cred_label(cred);
  621. tl = aa_get_task_label(target);
  622. error = aa_may_signal(cl, tl, sig);
  623. aa_put_label(cl);
  624. aa_put_label(tl);
  625. return error;
  626. }
  627. cl = __begin_current_label_crit_section();
  628. tl = aa_get_task_label(target);
  629. error = aa_may_signal(cl, tl, sig);
  630. aa_put_label(tl);
  631. __end_current_label_crit_section(cl);
  632. return error;
  633. }
  634. /**
  635. * apparmor_sk_alloc_security - allocate and attach the sk_security field
  636. */
  637. static int apparmor_sk_alloc_security(struct sock *sk, int family, gfp_t flags)
  638. {
  639. struct aa_sk_ctx *ctx;
  640. ctx = kzalloc(sizeof(*ctx), flags);
  641. if (!ctx)
  642. return -ENOMEM;
  643. SK_CTX(sk) = ctx;
  644. return 0;
  645. }
  646. /**
  647. * apparmor_sk_free_security - free the sk_security field
  648. */
  649. static void apparmor_sk_free_security(struct sock *sk)
  650. {
  651. struct aa_sk_ctx *ctx = SK_CTX(sk);
  652. SK_CTX(sk) = NULL;
  653. aa_put_label(ctx->label);
  654. aa_put_label(ctx->peer);
  655. kfree(ctx);
  656. }
  657. /**
  658. * apparmor_clone_security - clone the sk_security field
  659. */
  660. static void apparmor_sk_clone_security(const struct sock *sk,
  661. struct sock *newsk)
  662. {
  663. struct aa_sk_ctx *ctx = SK_CTX(sk);
  664. struct aa_sk_ctx *new = SK_CTX(newsk);
  665. new->label = aa_get_label(ctx->label);
  666. new->peer = aa_get_label(ctx->peer);
  667. }
  668. /**
  669. * apparmor_socket_create - check perms before creating a new socket
  670. */
  671. static int apparmor_socket_create(int family, int type, int protocol, int kern)
  672. {
  673. struct aa_label *label;
  674. int error = 0;
  675. AA_BUG(in_interrupt());
  676. label = begin_current_label_crit_section();
  677. if (!(kern || unconfined(label)))
  678. error = af_select(family,
  679. create_perm(label, family, type, protocol),
  680. aa_af_perm(label, OP_CREATE, AA_MAY_CREATE,
  681. family, type, protocol));
  682. end_current_label_crit_section(label);
  683. return error;
  684. }
  685. /**
  686. * apparmor_socket_post_create - setup the per-socket security struct
  687. *
  688. * Note:
  689. * - kernel sockets currently labeled unconfined but we may want to
  690. * move to a special kernel label
  691. * - socket may not have sk here if created with sock_create_lite or
  692. * sock_alloc. These should be accept cases which will be handled in
  693. * sock_graft.
  694. */
  695. static int apparmor_socket_post_create(struct socket *sock, int family,
  696. int type, int protocol, int kern)
  697. {
  698. struct aa_label *label;
  699. if (kern) {
  700. struct aa_ns *ns = aa_get_current_ns();
  701. label = aa_get_label(ns_unconfined(ns));
  702. aa_put_ns(ns);
  703. } else
  704. label = aa_get_current_label();
  705. if (sock->sk) {
  706. struct aa_sk_ctx *ctx = SK_CTX(sock->sk);
  707. aa_put_label(ctx->label);
  708. ctx->label = aa_get_label(label);
  709. }
  710. aa_put_label(label);
  711. return 0;
  712. }
  713. /**
  714. * apparmor_socket_bind - check perms before bind addr to socket
  715. */
  716. static int apparmor_socket_bind(struct socket *sock,
  717. struct sockaddr *address, int addrlen)
  718. {
  719. AA_BUG(!sock);
  720. AA_BUG(!sock->sk);
  721. AA_BUG(!address);
  722. AA_BUG(in_interrupt());
  723. return af_select(sock->sk->sk_family,
  724. bind_perm(sock, address, addrlen),
  725. aa_sk_perm(OP_BIND, AA_MAY_BIND, sock->sk));
  726. }
  727. /**
  728. * apparmor_socket_connect - check perms before connecting @sock to @address
  729. */
  730. static int apparmor_socket_connect(struct socket *sock,
  731. struct sockaddr *address, int addrlen)
  732. {
  733. AA_BUG(!sock);
  734. AA_BUG(!sock->sk);
  735. AA_BUG(!address);
  736. AA_BUG(in_interrupt());
  737. return af_select(sock->sk->sk_family,
  738. connect_perm(sock, address, addrlen),
  739. aa_sk_perm(OP_CONNECT, AA_MAY_CONNECT, sock->sk));
  740. }
  741. /**
  742. * apparmor_socket_list - check perms before allowing listen
  743. */
  744. static int apparmor_socket_listen(struct socket *sock, int backlog)
  745. {
  746. AA_BUG(!sock);
  747. AA_BUG(!sock->sk);
  748. AA_BUG(in_interrupt());
  749. return af_select(sock->sk->sk_family,
  750. listen_perm(sock, backlog),
  751. aa_sk_perm(OP_LISTEN, AA_MAY_LISTEN, sock->sk));
  752. }
  753. /**
  754. * apparmor_socket_accept - check perms before accepting a new connection.
  755. *
  756. * Note: while @newsock is created and has some information, the accept
  757. * has not been done.
  758. */
  759. static int apparmor_socket_accept(struct socket *sock, struct socket *newsock)
  760. {
  761. AA_BUG(!sock);
  762. AA_BUG(!sock->sk);
  763. AA_BUG(!newsock);
  764. AA_BUG(in_interrupt());
  765. return af_select(sock->sk->sk_family,
  766. accept_perm(sock, newsock),
  767. aa_sk_perm(OP_ACCEPT, AA_MAY_ACCEPT, sock->sk));
  768. }
  769. static int aa_sock_msg_perm(const char *op, u32 request, struct socket *sock,
  770. struct msghdr *msg, int size)
  771. {
  772. AA_BUG(!sock);
  773. AA_BUG(!sock->sk);
  774. AA_BUG(!msg);
  775. AA_BUG(in_interrupt());
  776. return af_select(sock->sk->sk_family,
  777. msg_perm(op, request, sock, msg, size),
  778. aa_sk_perm(op, request, sock->sk));
  779. }
  780. /**
  781. * apparmor_socket_sendmsg - check perms before sending msg to another socket
  782. */
  783. static int apparmor_socket_sendmsg(struct socket *sock,
  784. struct msghdr *msg, int size)
  785. {
  786. return aa_sock_msg_perm(OP_SENDMSG, AA_MAY_SEND, sock, msg, size);
  787. }
  788. /**
  789. * apparmor_socket_recvmsg - check perms before receiving a message
  790. */
  791. static int apparmor_socket_recvmsg(struct socket *sock,
  792. struct msghdr *msg, int size, int flags)
  793. {
  794. return aa_sock_msg_perm(OP_RECVMSG, AA_MAY_RECEIVE, sock, msg, size);
  795. }
  796. /* revaliation, get/set attr, shutdown */
  797. static int aa_sock_perm(const char *op, u32 request, struct socket *sock)
  798. {
  799. AA_BUG(!sock);
  800. AA_BUG(!sock->sk);
  801. AA_BUG(in_interrupt());
  802. return af_select(sock->sk->sk_family,
  803. sock_perm(op, request, sock),
  804. aa_sk_perm(op, request, sock->sk));
  805. }
  806. /**
  807. * apparmor_socket_getsockname - check perms before getting the local address
  808. */
  809. static int apparmor_socket_getsockname(struct socket *sock)
  810. {
  811. return aa_sock_perm(OP_GETSOCKNAME, AA_MAY_GETATTR, sock);
  812. }
  813. /**
  814. * apparmor_socket_getpeername - check perms before getting remote address
  815. */
  816. static int apparmor_socket_getpeername(struct socket *sock)
  817. {
  818. return aa_sock_perm(OP_GETPEERNAME, AA_MAY_GETATTR, sock);
  819. }
  820. /* revaliation, get/set attr, opt */
  821. static int aa_sock_opt_perm(const char *op, u32 request, struct socket *sock,
  822. int level, int optname)
  823. {
  824. AA_BUG(!sock);
  825. AA_BUG(!sock->sk);
  826. AA_BUG(in_interrupt());
  827. return af_select(sock->sk->sk_family,
  828. opt_perm(op, request, sock, level, optname),
  829. aa_sk_perm(op, request, sock->sk));
  830. }
  831. /**
  832. * apparmor_getsockopt - check perms before getting socket options
  833. */
  834. static int apparmor_socket_getsockopt(struct socket *sock, int level,
  835. int optname)
  836. {
  837. return aa_sock_opt_perm(OP_GETSOCKOPT, AA_MAY_GETOPT, sock,
  838. level, optname);
  839. }
  840. /**
  841. * apparmor_setsockopt - check perms before setting socket options
  842. */
  843. static int apparmor_socket_setsockopt(struct socket *sock, int level,
  844. int optname)
  845. {
  846. return aa_sock_opt_perm(OP_SETSOCKOPT, AA_MAY_SETOPT, sock,
  847. level, optname);
  848. }
  849. /**
  850. * apparmor_socket_shutdown - check perms before shutting down @sock conn
  851. */
  852. static int apparmor_socket_shutdown(struct socket *sock, int how)
  853. {
  854. return aa_sock_perm(OP_SHUTDOWN, AA_MAY_SHUTDOWN, sock);
  855. }
  856. /**
  857. * apparmor_socket_sock_recv_skb - check perms before associating skb to sk
  858. *
  859. * Note: can not sleep may be called with locks held
  860. *
  861. * dont want protocol specific in __skb_recv_datagram()
  862. * to deny an incoming connection socket_sock_rcv_skb()
  863. */
  864. static int apparmor_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
  865. {
  866. return 0;
  867. }
  868. static struct aa_label *sk_peer_label(struct sock *sk)
  869. {
  870. struct aa_sk_ctx *ctx = SK_CTX(sk);
  871. if (ctx->peer)
  872. return ctx->peer;
  873. return ERR_PTR(-ENOPROTOOPT);
  874. }
  875. /**
  876. * apparmor_socket_getpeersec_stream - get security context of peer
  877. *
  878. * Note: for tcp only valid if using ipsec or cipso on lan
  879. */
  880. static int apparmor_socket_getpeersec_stream(struct socket *sock,
  881. char __user *optval,
  882. int __user *optlen,
  883. unsigned int len)
  884. {
  885. char *name;
  886. int slen, error = 0;
  887. struct aa_label *label;
  888. struct aa_label *peer;
  889. label = begin_current_label_crit_section();
  890. peer = sk_peer_label(sock->sk);
  891. if (IS_ERR(peer)) {
  892. error = PTR_ERR(peer);
  893. goto done;
  894. }
  895. slen = aa_label_asxprint(&name, labels_ns(label), peer,
  896. FLAG_SHOW_MODE | FLAG_VIEW_SUBNS |
  897. FLAG_HIDDEN_UNCONFINED, GFP_KERNEL);
  898. /* don't include terminating \0 in slen, it breaks some apps */
  899. if (slen < 0) {
  900. error = -ENOMEM;
  901. } else {
  902. if (slen > len) {
  903. error = -ERANGE;
  904. } else if (copy_to_user(optval, name, slen)) {
  905. error = -EFAULT;
  906. goto out;
  907. }
  908. if (put_user(slen, optlen))
  909. error = -EFAULT;
  910. out:
  911. kfree(name);
  912. }
  913. done:
  914. end_current_label_crit_section(label);
  915. return error;
  916. }
  917. /**
  918. * apparmor_socket_getpeersec_dgram - get security label of packet
  919. * @sock: the peer socket
  920. * @skb: packet data
  921. * @secid: pointer to where to put the secid of the packet
  922. *
  923. * Sets the netlabel socket state on sk from parent
  924. */
  925. static int apparmor_socket_getpeersec_dgram(struct socket *sock,
  926. struct sk_buff *skb, u32 *secid)
  927. {
  928. /* TODO: requires secid support */
  929. return -ENOPROTOOPT;
  930. }
  931. /**
  932. * apparmor_sock_graft - Initialize newly created socket
  933. * @sk: child sock
  934. * @parent: parent socket
  935. *
  936. * Note: could set off of SOCK_CTX(parent) but need to track inode and we can
  937. * just set sk security information off of current creating process label
  938. * Labeling of sk for accept case - probably should be sock based
  939. * instead of task, because of the case where an implicitly labeled
  940. * socket is shared by different tasks.
  941. */
  942. static void apparmor_sock_graft(struct sock *sk, struct socket *parent)
  943. {
  944. struct aa_sk_ctx *ctx = SK_CTX(sk);
  945. if (!ctx->label)
  946. ctx->label = aa_get_current_label();
  947. }
  948. static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = {
  949. LSM_HOOK_INIT(ptrace_access_check, apparmor_ptrace_access_check),
  950. LSM_HOOK_INIT(ptrace_traceme, apparmor_ptrace_traceme),
  951. LSM_HOOK_INIT(capget, apparmor_capget),
  952. LSM_HOOK_INIT(capable, apparmor_capable),
  953. LSM_HOOK_INIT(sb_mount, apparmor_sb_mount),
  954. LSM_HOOK_INIT(sb_umount, apparmor_sb_umount),
  955. LSM_HOOK_INIT(sb_pivotroot, apparmor_sb_pivotroot),
  956. LSM_HOOK_INIT(path_link, apparmor_path_link),
  957. LSM_HOOK_INIT(path_unlink, apparmor_path_unlink),
  958. LSM_HOOK_INIT(path_symlink, apparmor_path_symlink),
  959. LSM_HOOK_INIT(path_mkdir, apparmor_path_mkdir),
  960. LSM_HOOK_INIT(path_rmdir, apparmor_path_rmdir),
  961. LSM_HOOK_INIT(path_mknod, apparmor_path_mknod),
  962. LSM_HOOK_INIT(path_rename, apparmor_path_rename),
  963. LSM_HOOK_INIT(path_chmod, apparmor_path_chmod),
  964. LSM_HOOK_INIT(path_chown, apparmor_path_chown),
  965. LSM_HOOK_INIT(path_truncate, apparmor_path_truncate),
  966. LSM_HOOK_INIT(inode_getattr, apparmor_inode_getattr),
  967. LSM_HOOK_INIT(file_open, apparmor_file_open),
  968. LSM_HOOK_INIT(file_receive, apparmor_file_receive),
  969. LSM_HOOK_INIT(file_permission, apparmor_file_permission),
  970. LSM_HOOK_INIT(file_alloc_security, apparmor_file_alloc_security),
  971. LSM_HOOK_INIT(file_free_security, apparmor_file_free_security),
  972. LSM_HOOK_INIT(mmap_file, apparmor_mmap_file),
  973. LSM_HOOK_INIT(file_mprotect, apparmor_file_mprotect),
  974. LSM_HOOK_INIT(file_lock, apparmor_file_lock),
  975. LSM_HOOK_INIT(getprocattr, apparmor_getprocattr),
  976. LSM_HOOK_INIT(setprocattr, apparmor_setprocattr),
  977. LSM_HOOK_INIT(sk_alloc_security, apparmor_sk_alloc_security),
  978. LSM_HOOK_INIT(sk_free_security, apparmor_sk_free_security),
  979. LSM_HOOK_INIT(sk_clone_security, apparmor_sk_clone_security),
  980. LSM_HOOK_INIT(socket_create, apparmor_socket_create),
  981. LSM_HOOK_INIT(socket_post_create, apparmor_socket_post_create),
  982. LSM_HOOK_INIT(socket_bind, apparmor_socket_bind),
  983. LSM_HOOK_INIT(socket_connect, apparmor_socket_connect),
  984. LSM_HOOK_INIT(socket_listen, apparmor_socket_listen),
  985. LSM_HOOK_INIT(socket_accept, apparmor_socket_accept),
  986. LSM_HOOK_INIT(socket_sendmsg, apparmor_socket_sendmsg),
  987. LSM_HOOK_INIT(socket_recvmsg, apparmor_socket_recvmsg),
  988. LSM_HOOK_INIT(socket_getsockname, apparmor_socket_getsockname),
  989. LSM_HOOK_INIT(socket_getpeername, apparmor_socket_getpeername),
  990. LSM_HOOK_INIT(socket_getsockopt, apparmor_socket_getsockopt),
  991. LSM_HOOK_INIT(socket_setsockopt, apparmor_socket_setsockopt),
  992. LSM_HOOK_INIT(socket_shutdown, apparmor_socket_shutdown),
  993. LSM_HOOK_INIT(socket_sock_rcv_skb, apparmor_socket_sock_rcv_skb),
  994. LSM_HOOK_INIT(socket_getpeersec_stream,
  995. apparmor_socket_getpeersec_stream),
  996. LSM_HOOK_INIT(socket_getpeersec_dgram,
  997. apparmor_socket_getpeersec_dgram),
  998. LSM_HOOK_INIT(sock_graft, apparmor_sock_graft),
  999. LSM_HOOK_INIT(cred_alloc_blank, apparmor_cred_alloc_blank),
  1000. LSM_HOOK_INIT(cred_free, apparmor_cred_free),
  1001. LSM_HOOK_INIT(cred_prepare, apparmor_cred_prepare),
  1002. LSM_HOOK_INIT(cred_transfer, apparmor_cred_transfer),
  1003. LSM_HOOK_INIT(bprm_set_creds, apparmor_bprm_set_creds),
  1004. LSM_HOOK_INIT(bprm_committing_creds, apparmor_bprm_committing_creds),
  1005. LSM_HOOK_INIT(bprm_committed_creds, apparmor_bprm_committed_creds),
  1006. LSM_HOOK_INIT(task_free, apparmor_task_free),
  1007. LSM_HOOK_INIT(task_alloc, apparmor_task_alloc),
  1008. LSM_HOOK_INIT(task_getsecid, apparmor_task_getsecid),
  1009. LSM_HOOK_INIT(task_setrlimit, apparmor_task_setrlimit),
  1010. LSM_HOOK_INIT(task_kill, apparmor_task_kill),
  1011. #ifdef CONFIG_AUDIT
  1012. LSM_HOOK_INIT(audit_rule_init, aa_audit_rule_init),
  1013. LSM_HOOK_INIT(audit_rule_known, aa_audit_rule_known),
  1014. LSM_HOOK_INIT(audit_rule_match, aa_audit_rule_match),
  1015. LSM_HOOK_INIT(audit_rule_free, aa_audit_rule_free),
  1016. #endif
  1017. LSM_HOOK_INIT(secid_to_secctx, apparmor_secid_to_secctx),
  1018. LSM_HOOK_INIT(secctx_to_secid, apparmor_secctx_to_secid),
  1019. LSM_HOOK_INIT(release_secctx, apparmor_release_secctx),
  1020. };
  1021. /*
  1022. * AppArmor sysfs module parameters
  1023. */
  1024. static int param_set_aabool(const char *val, const struct kernel_param *kp);
  1025. static int param_get_aabool(char *buffer, const struct kernel_param *kp);
  1026. #define param_check_aabool param_check_bool
  1027. static const struct kernel_param_ops param_ops_aabool = {
  1028. .flags = KERNEL_PARAM_OPS_FL_NOARG,
  1029. .set = param_set_aabool,
  1030. .get = param_get_aabool
  1031. };
  1032. static int param_set_aauint(const char *val, const struct kernel_param *kp);
  1033. static int param_get_aauint(char *buffer, const struct kernel_param *kp);
  1034. #define param_check_aauint param_check_uint
  1035. static const struct kernel_param_ops param_ops_aauint = {
  1036. .set = param_set_aauint,
  1037. .get = param_get_aauint
  1038. };
  1039. static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp);
  1040. static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp);
  1041. #define param_check_aalockpolicy param_check_bool
  1042. static const struct kernel_param_ops param_ops_aalockpolicy = {
  1043. .flags = KERNEL_PARAM_OPS_FL_NOARG,
  1044. .set = param_set_aalockpolicy,
  1045. .get = param_get_aalockpolicy
  1046. };
  1047. static int param_set_audit(const char *val, const struct kernel_param *kp);
  1048. static int param_get_audit(char *buffer, const struct kernel_param *kp);
  1049. static int param_set_mode(const char *val, const struct kernel_param *kp);
  1050. static int param_get_mode(char *buffer, const struct kernel_param *kp);
  1051. /* Flag values, also controllable via /sys/module/apparmor/parameters
  1052. * We define special types as we want to do additional mediation.
  1053. */
  1054. /* AppArmor global enforcement switch - complain, enforce, kill */
  1055. enum profile_mode aa_g_profile_mode = APPARMOR_ENFORCE;
  1056. module_param_call(mode, param_set_mode, param_get_mode,
  1057. &aa_g_profile_mode, S_IRUSR | S_IWUSR);
  1058. /* whether policy verification hashing is enabled */
  1059. bool aa_g_hash_policy = IS_ENABLED(CONFIG_SECURITY_APPARMOR_HASH_DEFAULT);
  1060. #ifdef CONFIG_SECURITY_APPARMOR_HASH
  1061. module_param_named(hash_policy, aa_g_hash_policy, aabool, S_IRUSR | S_IWUSR);
  1062. #endif
  1063. /* Debug mode */
  1064. bool aa_g_debug = IS_ENABLED(CONFIG_SECURITY_APPARMOR_DEBUG_MESSAGES);
  1065. module_param_named(debug, aa_g_debug, aabool, S_IRUSR | S_IWUSR);
  1066. /* Audit mode */
  1067. enum audit_mode aa_g_audit;
  1068. module_param_call(audit, param_set_audit, param_get_audit,
  1069. &aa_g_audit, S_IRUSR | S_IWUSR);
  1070. /* Determines if audit header is included in audited messages. This
  1071. * provides more context if the audit daemon is not running
  1072. */
  1073. bool aa_g_audit_header = true;
  1074. module_param_named(audit_header, aa_g_audit_header, aabool,
  1075. S_IRUSR | S_IWUSR);
  1076. /* lock out loading/removal of policy
  1077. * TODO: add in at boot loading of policy, which is the only way to
  1078. * load policy, if lock_policy is set
  1079. */
  1080. bool aa_g_lock_policy;
  1081. module_param_named(lock_policy, aa_g_lock_policy, aalockpolicy,
  1082. S_IRUSR | S_IWUSR);
  1083. /* Syscall logging mode */
  1084. bool aa_g_logsyscall;
  1085. module_param_named(logsyscall, aa_g_logsyscall, aabool, S_IRUSR | S_IWUSR);
  1086. /* Maximum pathname length before accesses will start getting rejected */
  1087. unsigned int aa_g_path_max = 2 * PATH_MAX;
  1088. module_param_named(path_max, aa_g_path_max, aauint, S_IRUSR);
  1089. /* Determines how paranoid loading of policy is and how much verification
  1090. * on the loaded policy is done.
  1091. * DEPRECATED: read only as strict checking of load is always done now
  1092. * that none root users (user namespaces) can load policy.
  1093. */
  1094. bool aa_g_paranoid_load = true;
  1095. module_param_named(paranoid_load, aa_g_paranoid_load, aabool, S_IRUGO);
  1096. /* Boot time disable flag */
  1097. static bool apparmor_enabled = CONFIG_SECURITY_APPARMOR_BOOTPARAM_VALUE;
  1098. module_param_named(enabled, apparmor_enabled, bool, S_IRUGO);
  1099. static int __init apparmor_enabled_setup(char *str)
  1100. {
  1101. unsigned long enabled;
  1102. int error = kstrtoul(str, 0, &enabled);
  1103. if (!error)
  1104. apparmor_enabled = enabled ? 1 : 0;
  1105. return 1;
  1106. }
  1107. __setup("apparmor=", apparmor_enabled_setup);
  1108. /* set global flag turning off the ability to load policy */
  1109. static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp)
  1110. {
  1111. if (!apparmor_enabled)
  1112. return -EINVAL;
  1113. if (apparmor_initialized && !policy_admin_capable(NULL))
  1114. return -EPERM;
  1115. return param_set_bool(val, kp);
  1116. }
  1117. static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp)
  1118. {
  1119. if (!apparmor_enabled)
  1120. return -EINVAL;
  1121. if (apparmor_initialized && !policy_view_capable(NULL))
  1122. return -EPERM;
  1123. return param_get_bool(buffer, kp);
  1124. }
  1125. static int param_set_aabool(const char *val, const struct kernel_param *kp)
  1126. {
  1127. if (!apparmor_enabled)
  1128. return -EINVAL;
  1129. if (apparmor_initialized && !policy_admin_capable(NULL))
  1130. return -EPERM;
  1131. return param_set_bool(val, kp);
  1132. }
  1133. static int param_get_aabool(char *buffer, const struct kernel_param *kp)
  1134. {
  1135. if (!apparmor_enabled)
  1136. return -EINVAL;
  1137. if (apparmor_initialized && !policy_view_capable(NULL))
  1138. return -EPERM;
  1139. return param_get_bool(buffer, kp);
  1140. }
  1141. static int param_set_aauint(const char *val, const struct kernel_param *kp)
  1142. {
  1143. int error;
  1144. if (!apparmor_enabled)
  1145. return -EINVAL;
  1146. /* file is ro but enforce 2nd line check */
  1147. if (apparmor_initialized)
  1148. return -EPERM;
  1149. error = param_set_uint(val, kp);
  1150. pr_info("AppArmor: buffer size set to %d bytes\n", aa_g_path_max);
  1151. return error;
  1152. }
  1153. static int param_get_aauint(char *buffer, const struct kernel_param *kp)
  1154. {
  1155. if (!apparmor_enabled)
  1156. return -EINVAL;
  1157. if (apparmor_initialized && !policy_view_capable(NULL))
  1158. return -EPERM;
  1159. return param_get_uint(buffer, kp);
  1160. }
  1161. static int param_get_audit(char *buffer, const struct kernel_param *kp)
  1162. {
  1163. if (!apparmor_enabled)
  1164. return -EINVAL;
  1165. if (apparmor_initialized && !policy_view_capable(NULL))
  1166. return -EPERM;
  1167. return sprintf(buffer, "%s", audit_mode_names[aa_g_audit]);
  1168. }
  1169. static int param_set_audit(const char *val, const struct kernel_param *kp)
  1170. {
  1171. int i;
  1172. if (!apparmor_enabled)
  1173. return -EINVAL;
  1174. if (!val)
  1175. return -EINVAL;
  1176. if (apparmor_initialized && !policy_admin_capable(NULL))
  1177. return -EPERM;
  1178. i = match_string(audit_mode_names, AUDIT_MAX_INDEX, val);
  1179. if (i < 0)
  1180. return -EINVAL;
  1181. aa_g_audit = i;
  1182. return 0;
  1183. }
  1184. static int param_get_mode(char *buffer, const struct kernel_param *kp)
  1185. {
  1186. if (!apparmor_enabled)
  1187. return -EINVAL;
  1188. if (apparmor_initialized && !policy_view_capable(NULL))
  1189. return -EPERM;
  1190. return sprintf(buffer, "%s", aa_profile_mode_names[aa_g_profile_mode]);
  1191. }
  1192. static int param_set_mode(const char *val, const struct kernel_param *kp)
  1193. {
  1194. int i;
  1195. if (!apparmor_enabled)
  1196. return -EINVAL;
  1197. if (!val)
  1198. return -EINVAL;
  1199. if (apparmor_initialized && !policy_admin_capable(NULL))
  1200. return -EPERM;
  1201. i = match_string(aa_profile_mode_names, APPARMOR_MODE_NAMES_MAX_INDEX,
  1202. val);
  1203. if (i < 0)
  1204. return -EINVAL;
  1205. aa_g_profile_mode = i;
  1206. return 0;
  1207. }
  1208. /*
  1209. * AppArmor init functions
  1210. */
  1211. /**
  1212. * set_init_ctx - set a task context and profile on the first task.
  1213. *
  1214. * TODO: allow setting an alternate profile than unconfined
  1215. */
  1216. static int __init set_init_ctx(void)
  1217. {
  1218. struct cred *cred = (struct cred *)current->real_cred;
  1219. struct aa_task_ctx *ctx;
  1220. ctx = aa_alloc_task_ctx(GFP_KERNEL);
  1221. if (!ctx)
  1222. return -ENOMEM;
  1223. cred_label(cred) = aa_get_label(ns_unconfined(root_ns));
  1224. task_ctx(current) = ctx;
  1225. return 0;
  1226. }
  1227. static void destroy_buffers(void)
  1228. {
  1229. u32 i, j;
  1230. for_each_possible_cpu(i) {
  1231. for_each_cpu_buffer(j) {
  1232. kfree(per_cpu(aa_buffers, i).buf[j]);
  1233. per_cpu(aa_buffers, i).buf[j] = NULL;
  1234. }
  1235. }
  1236. }
  1237. static int __init alloc_buffers(void)
  1238. {
  1239. u32 i, j;
  1240. for_each_possible_cpu(i) {
  1241. for_each_cpu_buffer(j) {
  1242. char *buffer;
  1243. if (cpu_to_node(i) > num_online_nodes())
  1244. /* fallback to kmalloc for offline nodes */
  1245. buffer = kmalloc(aa_g_path_max, GFP_KERNEL);
  1246. else
  1247. buffer = kmalloc_node(aa_g_path_max, GFP_KERNEL,
  1248. cpu_to_node(i));
  1249. if (!buffer) {
  1250. destroy_buffers();
  1251. return -ENOMEM;
  1252. }
  1253. per_cpu(aa_buffers, i).buf[j] = buffer;
  1254. }
  1255. }
  1256. return 0;
  1257. }
  1258. #ifdef CONFIG_SYSCTL
  1259. static int apparmor_dointvec(struct ctl_table *table, int write,
  1260. void __user *buffer, size_t *lenp, loff_t *ppos)
  1261. {
  1262. if (!policy_admin_capable(NULL))
  1263. return -EPERM;
  1264. if (!apparmor_enabled)
  1265. return -EINVAL;
  1266. return proc_dointvec(table, write, buffer, lenp, ppos);
  1267. }
  1268. static struct ctl_path apparmor_sysctl_path[] = {
  1269. { .procname = "kernel", },
  1270. { }
  1271. };
  1272. static struct ctl_table apparmor_sysctl_table[] = {
  1273. {
  1274. .procname = "unprivileged_userns_apparmor_policy",
  1275. .data = &unprivileged_userns_apparmor_policy,
  1276. .maxlen = sizeof(int),
  1277. .mode = 0600,
  1278. .proc_handler = apparmor_dointvec,
  1279. },
  1280. { }
  1281. };
  1282. static int __init apparmor_init_sysctl(void)
  1283. {
  1284. return register_sysctl_paths(apparmor_sysctl_path,
  1285. apparmor_sysctl_table) ? 0 : -ENOMEM;
  1286. }
  1287. #else
  1288. static inline int apparmor_init_sysctl(void)
  1289. {
  1290. return 0;
  1291. }
  1292. #endif /* CONFIG_SYSCTL */
  1293. static int __init apparmor_init(void)
  1294. {
  1295. int error;
  1296. if (!apparmor_enabled || !security_module_enable("apparmor")) {
  1297. aa_info_message("AppArmor disabled by boot time parameter");
  1298. apparmor_enabled = false;
  1299. return 0;
  1300. }
  1301. aa_secids_init();
  1302. error = aa_setup_dfa_engine();
  1303. if (error) {
  1304. AA_ERROR("Unable to setup dfa engine\n");
  1305. goto alloc_out;
  1306. }
  1307. error = aa_alloc_root_ns();
  1308. if (error) {
  1309. AA_ERROR("Unable to allocate default profile namespace\n");
  1310. goto alloc_out;
  1311. }
  1312. error = apparmor_init_sysctl();
  1313. if (error) {
  1314. AA_ERROR("Unable to register sysctls\n");
  1315. goto alloc_out;
  1316. }
  1317. error = alloc_buffers();
  1318. if (error) {
  1319. AA_ERROR("Unable to allocate work buffers\n");
  1320. goto buffers_out;
  1321. }
  1322. error = set_init_ctx();
  1323. if (error) {
  1324. AA_ERROR("Failed to set context on init task\n");
  1325. aa_free_root_ns();
  1326. goto buffers_out;
  1327. }
  1328. security_add_hooks(apparmor_hooks, ARRAY_SIZE(apparmor_hooks),
  1329. "apparmor");
  1330. /* Report that AppArmor successfully initialized */
  1331. apparmor_initialized = 1;
  1332. if (aa_g_profile_mode == APPARMOR_COMPLAIN)
  1333. aa_info_message("AppArmor initialized: complain mode enabled");
  1334. else if (aa_g_profile_mode == APPARMOR_KILL)
  1335. aa_info_message("AppArmor initialized: kill mode enabled");
  1336. else
  1337. aa_info_message("AppArmor initialized");
  1338. return error;
  1339. buffers_out:
  1340. destroy_buffers();
  1341. alloc_out:
  1342. aa_destroy_aafs();
  1343. aa_teardown_dfa_engine();
  1344. apparmor_enabled = false;
  1345. return error;
  1346. }
  1347. security_initcall(apparmor_init);