lsm.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614
  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. if (new->label)
  666. aa_put_label(new->label);
  667. new->label = aa_get_label(ctx->label);
  668. if (new->peer)
  669. aa_put_label(new->peer);
  670. new->peer = aa_get_label(ctx->peer);
  671. }
  672. /**
  673. * apparmor_socket_create - check perms before creating a new socket
  674. */
  675. static int apparmor_socket_create(int family, int type, int protocol, int kern)
  676. {
  677. struct aa_label *label;
  678. int error = 0;
  679. AA_BUG(in_interrupt());
  680. label = begin_current_label_crit_section();
  681. if (!(kern || unconfined(label)))
  682. error = af_select(family,
  683. create_perm(label, family, type, protocol),
  684. aa_af_perm(label, OP_CREATE, AA_MAY_CREATE,
  685. family, type, protocol));
  686. end_current_label_crit_section(label);
  687. return error;
  688. }
  689. /**
  690. * apparmor_socket_post_create - setup the per-socket security struct
  691. *
  692. * Note:
  693. * - kernel sockets currently labeled unconfined but we may want to
  694. * move to a special kernel label
  695. * - socket may not have sk here if created with sock_create_lite or
  696. * sock_alloc. These should be accept cases which will be handled in
  697. * sock_graft.
  698. */
  699. static int apparmor_socket_post_create(struct socket *sock, int family,
  700. int type, int protocol, int kern)
  701. {
  702. struct aa_label *label;
  703. if (kern) {
  704. struct aa_ns *ns = aa_get_current_ns();
  705. label = aa_get_label(ns_unconfined(ns));
  706. aa_put_ns(ns);
  707. } else
  708. label = aa_get_current_label();
  709. if (sock->sk) {
  710. struct aa_sk_ctx *ctx = SK_CTX(sock->sk);
  711. aa_put_label(ctx->label);
  712. ctx->label = aa_get_label(label);
  713. }
  714. aa_put_label(label);
  715. return 0;
  716. }
  717. /**
  718. * apparmor_socket_bind - check perms before bind addr to socket
  719. */
  720. static int apparmor_socket_bind(struct socket *sock,
  721. struct sockaddr *address, int addrlen)
  722. {
  723. AA_BUG(!sock);
  724. AA_BUG(!sock->sk);
  725. AA_BUG(!address);
  726. AA_BUG(in_interrupt());
  727. return af_select(sock->sk->sk_family,
  728. bind_perm(sock, address, addrlen),
  729. aa_sk_perm(OP_BIND, AA_MAY_BIND, sock->sk));
  730. }
  731. /**
  732. * apparmor_socket_connect - check perms before connecting @sock to @address
  733. */
  734. static int apparmor_socket_connect(struct socket *sock,
  735. struct sockaddr *address, int addrlen)
  736. {
  737. AA_BUG(!sock);
  738. AA_BUG(!sock->sk);
  739. AA_BUG(!address);
  740. AA_BUG(in_interrupt());
  741. return af_select(sock->sk->sk_family,
  742. connect_perm(sock, address, addrlen),
  743. aa_sk_perm(OP_CONNECT, AA_MAY_CONNECT, sock->sk));
  744. }
  745. /**
  746. * apparmor_socket_list - check perms before allowing listen
  747. */
  748. static int apparmor_socket_listen(struct socket *sock, int backlog)
  749. {
  750. AA_BUG(!sock);
  751. AA_BUG(!sock->sk);
  752. AA_BUG(in_interrupt());
  753. return af_select(sock->sk->sk_family,
  754. listen_perm(sock, backlog),
  755. aa_sk_perm(OP_LISTEN, AA_MAY_LISTEN, sock->sk));
  756. }
  757. /**
  758. * apparmor_socket_accept - check perms before accepting a new connection.
  759. *
  760. * Note: while @newsock is created and has some information, the accept
  761. * has not been done.
  762. */
  763. static int apparmor_socket_accept(struct socket *sock, struct socket *newsock)
  764. {
  765. AA_BUG(!sock);
  766. AA_BUG(!sock->sk);
  767. AA_BUG(!newsock);
  768. AA_BUG(in_interrupt());
  769. return af_select(sock->sk->sk_family,
  770. accept_perm(sock, newsock),
  771. aa_sk_perm(OP_ACCEPT, AA_MAY_ACCEPT, sock->sk));
  772. }
  773. static int aa_sock_msg_perm(const char *op, u32 request, struct socket *sock,
  774. struct msghdr *msg, int size)
  775. {
  776. AA_BUG(!sock);
  777. AA_BUG(!sock->sk);
  778. AA_BUG(!msg);
  779. AA_BUG(in_interrupt());
  780. return af_select(sock->sk->sk_family,
  781. msg_perm(op, request, sock, msg, size),
  782. aa_sk_perm(op, request, sock->sk));
  783. }
  784. /**
  785. * apparmor_socket_sendmsg - check perms before sending msg to another socket
  786. */
  787. static int apparmor_socket_sendmsg(struct socket *sock,
  788. struct msghdr *msg, int size)
  789. {
  790. return aa_sock_msg_perm(OP_SENDMSG, AA_MAY_SEND, sock, msg, size);
  791. }
  792. /**
  793. * apparmor_socket_recvmsg - check perms before receiving a message
  794. */
  795. static int apparmor_socket_recvmsg(struct socket *sock,
  796. struct msghdr *msg, int size, int flags)
  797. {
  798. return aa_sock_msg_perm(OP_RECVMSG, AA_MAY_RECEIVE, sock, msg, size);
  799. }
  800. /* revaliation, get/set attr, shutdown */
  801. static int aa_sock_perm(const char *op, u32 request, struct socket *sock)
  802. {
  803. AA_BUG(!sock);
  804. AA_BUG(!sock->sk);
  805. AA_BUG(in_interrupt());
  806. return af_select(sock->sk->sk_family,
  807. sock_perm(op, request, sock),
  808. aa_sk_perm(op, request, sock->sk));
  809. }
  810. /**
  811. * apparmor_socket_getsockname - check perms before getting the local address
  812. */
  813. static int apparmor_socket_getsockname(struct socket *sock)
  814. {
  815. return aa_sock_perm(OP_GETSOCKNAME, AA_MAY_GETATTR, sock);
  816. }
  817. /**
  818. * apparmor_socket_getpeername - check perms before getting remote address
  819. */
  820. static int apparmor_socket_getpeername(struct socket *sock)
  821. {
  822. return aa_sock_perm(OP_GETPEERNAME, AA_MAY_GETATTR, sock);
  823. }
  824. /* revaliation, get/set attr, opt */
  825. static int aa_sock_opt_perm(const char *op, u32 request, struct socket *sock,
  826. int level, int optname)
  827. {
  828. AA_BUG(!sock);
  829. AA_BUG(!sock->sk);
  830. AA_BUG(in_interrupt());
  831. return af_select(sock->sk->sk_family,
  832. opt_perm(op, request, sock, level, optname),
  833. aa_sk_perm(op, request, sock->sk));
  834. }
  835. /**
  836. * apparmor_getsockopt - check perms before getting socket options
  837. */
  838. static int apparmor_socket_getsockopt(struct socket *sock, int level,
  839. int optname)
  840. {
  841. return aa_sock_opt_perm(OP_GETSOCKOPT, AA_MAY_GETOPT, sock,
  842. level, optname);
  843. }
  844. /**
  845. * apparmor_setsockopt - check perms before setting socket options
  846. */
  847. static int apparmor_socket_setsockopt(struct socket *sock, int level,
  848. int optname)
  849. {
  850. return aa_sock_opt_perm(OP_SETSOCKOPT, AA_MAY_SETOPT, sock,
  851. level, optname);
  852. }
  853. /**
  854. * apparmor_socket_shutdown - check perms before shutting down @sock conn
  855. */
  856. static int apparmor_socket_shutdown(struct socket *sock, int how)
  857. {
  858. return aa_sock_perm(OP_SHUTDOWN, AA_MAY_SHUTDOWN, sock);
  859. }
  860. /**
  861. * apparmor_socket_sock_recv_skb - check perms before associating skb to sk
  862. *
  863. * Note: can not sleep may be called with locks held
  864. *
  865. * dont want protocol specific in __skb_recv_datagram()
  866. * to deny an incoming connection socket_sock_rcv_skb()
  867. */
  868. static int apparmor_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
  869. {
  870. return 0;
  871. }
  872. static struct aa_label *sk_peer_label(struct sock *sk)
  873. {
  874. struct aa_sk_ctx *ctx = SK_CTX(sk);
  875. if (ctx->peer)
  876. return ctx->peer;
  877. return ERR_PTR(-ENOPROTOOPT);
  878. }
  879. /**
  880. * apparmor_socket_getpeersec_stream - get security context of peer
  881. *
  882. * Note: for tcp only valid if using ipsec or cipso on lan
  883. */
  884. static int apparmor_socket_getpeersec_stream(struct socket *sock,
  885. char __user *optval,
  886. int __user *optlen,
  887. unsigned int len)
  888. {
  889. char *name;
  890. int slen, error = 0;
  891. struct aa_label *label;
  892. struct aa_label *peer;
  893. label = begin_current_label_crit_section();
  894. peer = sk_peer_label(sock->sk);
  895. if (IS_ERR(peer)) {
  896. error = PTR_ERR(peer);
  897. goto done;
  898. }
  899. slen = aa_label_asxprint(&name, labels_ns(label), peer,
  900. FLAG_SHOW_MODE | FLAG_VIEW_SUBNS |
  901. FLAG_HIDDEN_UNCONFINED, GFP_KERNEL);
  902. /* don't include terminating \0 in slen, it breaks some apps */
  903. if (slen < 0) {
  904. error = -ENOMEM;
  905. } else {
  906. if (slen > len) {
  907. error = -ERANGE;
  908. } else if (copy_to_user(optval, name, slen)) {
  909. error = -EFAULT;
  910. goto out;
  911. }
  912. if (put_user(slen, optlen))
  913. error = -EFAULT;
  914. out:
  915. kfree(name);
  916. }
  917. done:
  918. end_current_label_crit_section(label);
  919. return error;
  920. }
  921. /**
  922. * apparmor_socket_getpeersec_dgram - get security label of packet
  923. * @sock: the peer socket
  924. * @skb: packet data
  925. * @secid: pointer to where to put the secid of the packet
  926. *
  927. * Sets the netlabel socket state on sk from parent
  928. */
  929. static int apparmor_socket_getpeersec_dgram(struct socket *sock,
  930. struct sk_buff *skb, u32 *secid)
  931. {
  932. /* TODO: requires secid support */
  933. return -ENOPROTOOPT;
  934. }
  935. /**
  936. * apparmor_sock_graft - Initialize newly created socket
  937. * @sk: child sock
  938. * @parent: parent socket
  939. *
  940. * Note: could set off of SOCK_CTX(parent) but need to track inode and we can
  941. * just set sk security information off of current creating process label
  942. * Labeling of sk for accept case - probably should be sock based
  943. * instead of task, because of the case where an implicitly labeled
  944. * socket is shared by different tasks.
  945. */
  946. static void apparmor_sock_graft(struct sock *sk, struct socket *parent)
  947. {
  948. struct aa_sk_ctx *ctx = SK_CTX(sk);
  949. if (!ctx->label)
  950. ctx->label = aa_get_current_label();
  951. }
  952. static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = {
  953. LSM_HOOK_INIT(ptrace_access_check, apparmor_ptrace_access_check),
  954. LSM_HOOK_INIT(ptrace_traceme, apparmor_ptrace_traceme),
  955. LSM_HOOK_INIT(capget, apparmor_capget),
  956. LSM_HOOK_INIT(capable, apparmor_capable),
  957. LSM_HOOK_INIT(sb_mount, apparmor_sb_mount),
  958. LSM_HOOK_INIT(sb_umount, apparmor_sb_umount),
  959. LSM_HOOK_INIT(sb_pivotroot, apparmor_sb_pivotroot),
  960. LSM_HOOK_INIT(path_link, apparmor_path_link),
  961. LSM_HOOK_INIT(path_unlink, apparmor_path_unlink),
  962. LSM_HOOK_INIT(path_symlink, apparmor_path_symlink),
  963. LSM_HOOK_INIT(path_mkdir, apparmor_path_mkdir),
  964. LSM_HOOK_INIT(path_rmdir, apparmor_path_rmdir),
  965. LSM_HOOK_INIT(path_mknod, apparmor_path_mknod),
  966. LSM_HOOK_INIT(path_rename, apparmor_path_rename),
  967. LSM_HOOK_INIT(path_chmod, apparmor_path_chmod),
  968. LSM_HOOK_INIT(path_chown, apparmor_path_chown),
  969. LSM_HOOK_INIT(path_truncate, apparmor_path_truncate),
  970. LSM_HOOK_INIT(inode_getattr, apparmor_inode_getattr),
  971. LSM_HOOK_INIT(file_open, apparmor_file_open),
  972. LSM_HOOK_INIT(file_receive, apparmor_file_receive),
  973. LSM_HOOK_INIT(file_permission, apparmor_file_permission),
  974. LSM_HOOK_INIT(file_alloc_security, apparmor_file_alloc_security),
  975. LSM_HOOK_INIT(file_free_security, apparmor_file_free_security),
  976. LSM_HOOK_INIT(mmap_file, apparmor_mmap_file),
  977. LSM_HOOK_INIT(file_mprotect, apparmor_file_mprotect),
  978. LSM_HOOK_INIT(file_lock, apparmor_file_lock),
  979. LSM_HOOK_INIT(getprocattr, apparmor_getprocattr),
  980. LSM_HOOK_INIT(setprocattr, apparmor_setprocattr),
  981. LSM_HOOK_INIT(sk_alloc_security, apparmor_sk_alloc_security),
  982. LSM_HOOK_INIT(sk_free_security, apparmor_sk_free_security),
  983. LSM_HOOK_INIT(sk_clone_security, apparmor_sk_clone_security),
  984. LSM_HOOK_INIT(socket_create, apparmor_socket_create),
  985. LSM_HOOK_INIT(socket_post_create, apparmor_socket_post_create),
  986. LSM_HOOK_INIT(socket_bind, apparmor_socket_bind),
  987. LSM_HOOK_INIT(socket_connect, apparmor_socket_connect),
  988. LSM_HOOK_INIT(socket_listen, apparmor_socket_listen),
  989. LSM_HOOK_INIT(socket_accept, apparmor_socket_accept),
  990. LSM_HOOK_INIT(socket_sendmsg, apparmor_socket_sendmsg),
  991. LSM_HOOK_INIT(socket_recvmsg, apparmor_socket_recvmsg),
  992. LSM_HOOK_INIT(socket_getsockname, apparmor_socket_getsockname),
  993. LSM_HOOK_INIT(socket_getpeername, apparmor_socket_getpeername),
  994. LSM_HOOK_INIT(socket_getsockopt, apparmor_socket_getsockopt),
  995. LSM_HOOK_INIT(socket_setsockopt, apparmor_socket_setsockopt),
  996. LSM_HOOK_INIT(socket_shutdown, apparmor_socket_shutdown),
  997. LSM_HOOK_INIT(socket_sock_rcv_skb, apparmor_socket_sock_rcv_skb),
  998. LSM_HOOK_INIT(socket_getpeersec_stream,
  999. apparmor_socket_getpeersec_stream),
  1000. LSM_HOOK_INIT(socket_getpeersec_dgram,
  1001. apparmor_socket_getpeersec_dgram),
  1002. LSM_HOOK_INIT(sock_graft, apparmor_sock_graft),
  1003. LSM_HOOK_INIT(cred_alloc_blank, apparmor_cred_alloc_blank),
  1004. LSM_HOOK_INIT(cred_free, apparmor_cred_free),
  1005. LSM_HOOK_INIT(cred_prepare, apparmor_cred_prepare),
  1006. LSM_HOOK_INIT(cred_transfer, apparmor_cred_transfer),
  1007. LSM_HOOK_INIT(bprm_set_creds, apparmor_bprm_set_creds),
  1008. LSM_HOOK_INIT(bprm_committing_creds, apparmor_bprm_committing_creds),
  1009. LSM_HOOK_INIT(bprm_committed_creds, apparmor_bprm_committed_creds),
  1010. LSM_HOOK_INIT(task_free, apparmor_task_free),
  1011. LSM_HOOK_INIT(task_alloc, apparmor_task_alloc),
  1012. LSM_HOOK_INIT(task_getsecid, apparmor_task_getsecid),
  1013. LSM_HOOK_INIT(task_setrlimit, apparmor_task_setrlimit),
  1014. LSM_HOOK_INIT(task_kill, apparmor_task_kill),
  1015. #ifdef CONFIG_AUDIT
  1016. LSM_HOOK_INIT(audit_rule_init, aa_audit_rule_init),
  1017. LSM_HOOK_INIT(audit_rule_known, aa_audit_rule_known),
  1018. LSM_HOOK_INIT(audit_rule_match, aa_audit_rule_match),
  1019. LSM_HOOK_INIT(audit_rule_free, aa_audit_rule_free),
  1020. #endif
  1021. LSM_HOOK_INIT(secid_to_secctx, apparmor_secid_to_secctx),
  1022. LSM_HOOK_INIT(secctx_to_secid, apparmor_secctx_to_secid),
  1023. LSM_HOOK_INIT(release_secctx, apparmor_release_secctx),
  1024. };
  1025. /*
  1026. * AppArmor sysfs module parameters
  1027. */
  1028. static int param_set_aabool(const char *val, const struct kernel_param *kp);
  1029. static int param_get_aabool(char *buffer, const struct kernel_param *kp);
  1030. #define param_check_aabool param_check_bool
  1031. static const struct kernel_param_ops param_ops_aabool = {
  1032. .flags = KERNEL_PARAM_OPS_FL_NOARG,
  1033. .set = param_set_aabool,
  1034. .get = param_get_aabool
  1035. };
  1036. static int param_set_aauint(const char *val, const struct kernel_param *kp);
  1037. static int param_get_aauint(char *buffer, const struct kernel_param *kp);
  1038. #define param_check_aauint param_check_uint
  1039. static const struct kernel_param_ops param_ops_aauint = {
  1040. .set = param_set_aauint,
  1041. .get = param_get_aauint
  1042. };
  1043. static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp);
  1044. static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp);
  1045. #define param_check_aalockpolicy param_check_bool
  1046. static const struct kernel_param_ops param_ops_aalockpolicy = {
  1047. .flags = KERNEL_PARAM_OPS_FL_NOARG,
  1048. .set = param_set_aalockpolicy,
  1049. .get = param_get_aalockpolicy
  1050. };
  1051. static int param_set_audit(const char *val, const struct kernel_param *kp);
  1052. static int param_get_audit(char *buffer, const struct kernel_param *kp);
  1053. static int param_set_mode(const char *val, const struct kernel_param *kp);
  1054. static int param_get_mode(char *buffer, const struct kernel_param *kp);
  1055. /* Flag values, also controllable via /sys/module/apparmor/parameters
  1056. * We define special types as we want to do additional mediation.
  1057. */
  1058. /* AppArmor global enforcement switch - complain, enforce, kill */
  1059. enum profile_mode aa_g_profile_mode = APPARMOR_ENFORCE;
  1060. module_param_call(mode, param_set_mode, param_get_mode,
  1061. &aa_g_profile_mode, S_IRUSR | S_IWUSR);
  1062. /* whether policy verification hashing is enabled */
  1063. bool aa_g_hash_policy = IS_ENABLED(CONFIG_SECURITY_APPARMOR_HASH_DEFAULT);
  1064. #ifdef CONFIG_SECURITY_APPARMOR_HASH
  1065. module_param_named(hash_policy, aa_g_hash_policy, aabool, S_IRUSR | S_IWUSR);
  1066. #endif
  1067. /* Debug mode */
  1068. bool aa_g_debug = IS_ENABLED(CONFIG_SECURITY_APPARMOR_DEBUG_MESSAGES);
  1069. module_param_named(debug, aa_g_debug, aabool, S_IRUSR | S_IWUSR);
  1070. /* Audit mode */
  1071. enum audit_mode aa_g_audit;
  1072. module_param_call(audit, param_set_audit, param_get_audit,
  1073. &aa_g_audit, S_IRUSR | S_IWUSR);
  1074. /* Determines if audit header is included in audited messages. This
  1075. * provides more context if the audit daemon is not running
  1076. */
  1077. bool aa_g_audit_header = true;
  1078. module_param_named(audit_header, aa_g_audit_header, aabool,
  1079. S_IRUSR | S_IWUSR);
  1080. /* lock out loading/removal of policy
  1081. * TODO: add in at boot loading of policy, which is the only way to
  1082. * load policy, if lock_policy is set
  1083. */
  1084. bool aa_g_lock_policy;
  1085. module_param_named(lock_policy, aa_g_lock_policy, aalockpolicy,
  1086. S_IRUSR | S_IWUSR);
  1087. /* Syscall logging mode */
  1088. bool aa_g_logsyscall;
  1089. module_param_named(logsyscall, aa_g_logsyscall, aabool, S_IRUSR | S_IWUSR);
  1090. /* Maximum pathname length before accesses will start getting rejected */
  1091. unsigned int aa_g_path_max = 2 * PATH_MAX;
  1092. module_param_named(path_max, aa_g_path_max, aauint, S_IRUSR);
  1093. /* Determines how paranoid loading of policy is and how much verification
  1094. * on the loaded policy is done.
  1095. * DEPRECATED: read only as strict checking of load is always done now
  1096. * that none root users (user namespaces) can load policy.
  1097. */
  1098. bool aa_g_paranoid_load = true;
  1099. module_param_named(paranoid_load, aa_g_paranoid_load, aabool, S_IRUGO);
  1100. /* Boot time disable flag */
  1101. static bool apparmor_enabled = CONFIG_SECURITY_APPARMOR_BOOTPARAM_VALUE;
  1102. module_param_named(enabled, apparmor_enabled, bool, S_IRUGO);
  1103. static int __init apparmor_enabled_setup(char *str)
  1104. {
  1105. unsigned long enabled;
  1106. int error = kstrtoul(str, 0, &enabled);
  1107. if (!error)
  1108. apparmor_enabled = enabled ? 1 : 0;
  1109. return 1;
  1110. }
  1111. __setup("apparmor=", apparmor_enabled_setup);
  1112. /* set global flag turning off the ability to load policy */
  1113. static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp)
  1114. {
  1115. if (!apparmor_enabled)
  1116. return -EINVAL;
  1117. if (apparmor_initialized && !policy_admin_capable(NULL))
  1118. return -EPERM;
  1119. return param_set_bool(val, kp);
  1120. }
  1121. static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp)
  1122. {
  1123. if (!apparmor_enabled)
  1124. return -EINVAL;
  1125. if (apparmor_initialized && !policy_view_capable(NULL))
  1126. return -EPERM;
  1127. return param_get_bool(buffer, kp);
  1128. }
  1129. static int param_set_aabool(const char *val, const struct kernel_param *kp)
  1130. {
  1131. if (!apparmor_enabled)
  1132. return -EINVAL;
  1133. if (apparmor_initialized && !policy_admin_capable(NULL))
  1134. return -EPERM;
  1135. return param_set_bool(val, kp);
  1136. }
  1137. static int param_get_aabool(char *buffer, const struct kernel_param *kp)
  1138. {
  1139. if (!apparmor_enabled)
  1140. return -EINVAL;
  1141. if (apparmor_initialized && !policy_view_capable(NULL))
  1142. return -EPERM;
  1143. return param_get_bool(buffer, kp);
  1144. }
  1145. static int param_set_aauint(const char *val, const struct kernel_param *kp)
  1146. {
  1147. int error;
  1148. if (!apparmor_enabled)
  1149. return -EINVAL;
  1150. /* file is ro but enforce 2nd line check */
  1151. if (apparmor_initialized)
  1152. return -EPERM;
  1153. error = param_set_uint(val, kp);
  1154. pr_info("AppArmor: buffer size set to %d bytes\n", aa_g_path_max);
  1155. return error;
  1156. }
  1157. static int param_get_aauint(char *buffer, const struct kernel_param *kp)
  1158. {
  1159. if (!apparmor_enabled)
  1160. return -EINVAL;
  1161. if (apparmor_initialized && !policy_view_capable(NULL))
  1162. return -EPERM;
  1163. return param_get_uint(buffer, kp);
  1164. }
  1165. static int param_get_audit(char *buffer, const struct kernel_param *kp)
  1166. {
  1167. if (!apparmor_enabled)
  1168. return -EINVAL;
  1169. if (apparmor_initialized && !policy_view_capable(NULL))
  1170. return -EPERM;
  1171. return sprintf(buffer, "%s", audit_mode_names[aa_g_audit]);
  1172. }
  1173. static int param_set_audit(const char *val, const struct kernel_param *kp)
  1174. {
  1175. int i;
  1176. if (!apparmor_enabled)
  1177. return -EINVAL;
  1178. if (!val)
  1179. return -EINVAL;
  1180. if (apparmor_initialized && !policy_admin_capable(NULL))
  1181. return -EPERM;
  1182. i = match_string(audit_mode_names, AUDIT_MAX_INDEX, val);
  1183. if (i < 0)
  1184. return -EINVAL;
  1185. aa_g_audit = i;
  1186. return 0;
  1187. }
  1188. static int param_get_mode(char *buffer, const struct kernel_param *kp)
  1189. {
  1190. if (!apparmor_enabled)
  1191. return -EINVAL;
  1192. if (apparmor_initialized && !policy_view_capable(NULL))
  1193. return -EPERM;
  1194. return sprintf(buffer, "%s", aa_profile_mode_names[aa_g_profile_mode]);
  1195. }
  1196. static int param_set_mode(const char *val, const struct kernel_param *kp)
  1197. {
  1198. int i;
  1199. if (!apparmor_enabled)
  1200. return -EINVAL;
  1201. if (!val)
  1202. return -EINVAL;
  1203. if (apparmor_initialized && !policy_admin_capable(NULL))
  1204. return -EPERM;
  1205. i = match_string(aa_profile_mode_names, APPARMOR_MODE_NAMES_MAX_INDEX,
  1206. val);
  1207. if (i < 0)
  1208. return -EINVAL;
  1209. aa_g_profile_mode = i;
  1210. return 0;
  1211. }
  1212. /*
  1213. * AppArmor init functions
  1214. */
  1215. /**
  1216. * set_init_ctx - set a task context and profile on the first task.
  1217. *
  1218. * TODO: allow setting an alternate profile than unconfined
  1219. */
  1220. static int __init set_init_ctx(void)
  1221. {
  1222. struct cred *cred = (struct cred *)current->real_cred;
  1223. struct aa_task_ctx *ctx;
  1224. ctx = aa_alloc_task_ctx(GFP_KERNEL);
  1225. if (!ctx)
  1226. return -ENOMEM;
  1227. cred_label(cred) = aa_get_label(ns_unconfined(root_ns));
  1228. task_ctx(current) = ctx;
  1229. return 0;
  1230. }
  1231. static void destroy_buffers(void)
  1232. {
  1233. u32 i, j;
  1234. for_each_possible_cpu(i) {
  1235. for_each_cpu_buffer(j) {
  1236. kfree(per_cpu(aa_buffers, i).buf[j]);
  1237. per_cpu(aa_buffers, i).buf[j] = NULL;
  1238. }
  1239. }
  1240. }
  1241. static int __init alloc_buffers(void)
  1242. {
  1243. u32 i, j;
  1244. for_each_possible_cpu(i) {
  1245. for_each_cpu_buffer(j) {
  1246. char *buffer;
  1247. if (cpu_to_node(i) > num_online_nodes())
  1248. /* fallback to kmalloc for offline nodes */
  1249. buffer = kmalloc(aa_g_path_max, GFP_KERNEL);
  1250. else
  1251. buffer = kmalloc_node(aa_g_path_max, GFP_KERNEL,
  1252. cpu_to_node(i));
  1253. if (!buffer) {
  1254. destroy_buffers();
  1255. return -ENOMEM;
  1256. }
  1257. per_cpu(aa_buffers, i).buf[j] = buffer;
  1258. }
  1259. }
  1260. return 0;
  1261. }
  1262. #ifdef CONFIG_SYSCTL
  1263. static int apparmor_dointvec(struct ctl_table *table, int write,
  1264. void __user *buffer, size_t *lenp, loff_t *ppos)
  1265. {
  1266. if (!policy_admin_capable(NULL))
  1267. return -EPERM;
  1268. if (!apparmor_enabled)
  1269. return -EINVAL;
  1270. return proc_dointvec(table, write, buffer, lenp, ppos);
  1271. }
  1272. static struct ctl_path apparmor_sysctl_path[] = {
  1273. { .procname = "kernel", },
  1274. { }
  1275. };
  1276. static struct ctl_table apparmor_sysctl_table[] = {
  1277. {
  1278. .procname = "unprivileged_userns_apparmor_policy",
  1279. .data = &unprivileged_userns_apparmor_policy,
  1280. .maxlen = sizeof(int),
  1281. .mode = 0600,
  1282. .proc_handler = apparmor_dointvec,
  1283. },
  1284. { }
  1285. };
  1286. static int __init apparmor_init_sysctl(void)
  1287. {
  1288. return register_sysctl_paths(apparmor_sysctl_path,
  1289. apparmor_sysctl_table) ? 0 : -ENOMEM;
  1290. }
  1291. #else
  1292. static inline int apparmor_init_sysctl(void)
  1293. {
  1294. return 0;
  1295. }
  1296. #endif /* CONFIG_SYSCTL */
  1297. static int __init apparmor_init(void)
  1298. {
  1299. int error;
  1300. if (!apparmor_enabled || !security_module_enable("apparmor")) {
  1301. aa_info_message("AppArmor disabled by boot time parameter");
  1302. apparmor_enabled = false;
  1303. return 0;
  1304. }
  1305. aa_secids_init();
  1306. error = aa_setup_dfa_engine();
  1307. if (error) {
  1308. AA_ERROR("Unable to setup dfa engine\n");
  1309. goto alloc_out;
  1310. }
  1311. error = aa_alloc_root_ns();
  1312. if (error) {
  1313. AA_ERROR("Unable to allocate default profile namespace\n");
  1314. goto alloc_out;
  1315. }
  1316. error = apparmor_init_sysctl();
  1317. if (error) {
  1318. AA_ERROR("Unable to register sysctls\n");
  1319. goto alloc_out;
  1320. }
  1321. error = alloc_buffers();
  1322. if (error) {
  1323. AA_ERROR("Unable to allocate work buffers\n");
  1324. goto buffers_out;
  1325. }
  1326. error = set_init_ctx();
  1327. if (error) {
  1328. AA_ERROR("Failed to set context on init task\n");
  1329. aa_free_root_ns();
  1330. goto buffers_out;
  1331. }
  1332. security_add_hooks(apparmor_hooks, ARRAY_SIZE(apparmor_hooks),
  1333. "apparmor");
  1334. /* Report that AppArmor successfully initialized */
  1335. apparmor_initialized = 1;
  1336. if (aa_g_profile_mode == APPARMOR_COMPLAIN)
  1337. aa_info_message("AppArmor initialized: complain mode enabled");
  1338. else if (aa_g_profile_mode == APPARMOR_KILL)
  1339. aa_info_message("AppArmor initialized: kill mode enabled");
  1340. else
  1341. aa_info_message("AppArmor initialized");
  1342. return error;
  1343. buffers_out:
  1344. destroy_buffers();
  1345. alloc_out:
  1346. aa_destroy_aafs();
  1347. aa_teardown_dfa_engine();
  1348. apparmor_enabled = false;
  1349. return error;
  1350. }
  1351. security_initcall(apparmor_init);