auditsc.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446
  1. /* auditsc.c -- System-call auditing support
  2. * Handles all system-call specific auditing features.
  3. *
  4. * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
  5. * Copyright 2005 Hewlett-Packard Development Company, L.P.
  6. * Copyright (C) 2005, 2006 IBM Corporation
  7. * All Rights Reserved.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. * Written by Rickard E. (Rik) Faith <faith@redhat.com>
  24. *
  25. * Many of the ideas implemented here are from Stephen C. Tweedie,
  26. * especially the idea of avoiding a copy by using getname.
  27. *
  28. * The method for actual interception of syscall entry and exit (not in
  29. * this file -- see entry.S) is based on a GPL'd patch written by
  30. * okir@suse.de and Copyright 2003 SuSE Linux AG.
  31. *
  32. * POSIX message queue support added by George Wilson <ltcgcw@us.ibm.com>,
  33. * 2006.
  34. *
  35. * The support of additional filter rules compares (>, <, >=, <=) was
  36. * added by Dustin Kirkland <dustin.kirkland@us.ibm.com>, 2005.
  37. *
  38. * Modified by Amy Griffis <amy.griffis@hp.com> to collect additional
  39. * filesystem information.
  40. *
  41. * Subject and object context labeling support added by <danjones@us.ibm.com>
  42. * and <dustin.kirkland@us.ibm.com> for LSPP certification compliance.
  43. */
  44. #include <linux/init.h>
  45. #include <asm/types.h>
  46. #include <linux/atomic.h>
  47. #include <linux/fs.h>
  48. #include <linux/namei.h>
  49. #include <linux/mm.h>
  50. #include <linux/export.h>
  51. #include <linux/slab.h>
  52. #include <linux/mount.h>
  53. #include <linux/socket.h>
  54. #include <linux/mqueue.h>
  55. #include <linux/audit.h>
  56. #include <linux/personality.h>
  57. #include <linux/time.h>
  58. #include <linux/netlink.h>
  59. #include <linux/compiler.h>
  60. #include <asm/unistd.h>
  61. #include <linux/security.h>
  62. #include <linux/list.h>
  63. #include <linux/tty.h>
  64. #include <linux/binfmts.h>
  65. #include <linux/highmem.h>
  66. #include <linux/syscalls.h>
  67. #include <linux/capability.h>
  68. #include <linux/fs_struct.h>
  69. #include <linux/compat.h>
  70. #include "audit.h"
  71. /* flags stating the success for a syscall */
  72. #define AUDITSC_INVALID 0
  73. #define AUDITSC_SUCCESS 1
  74. #define AUDITSC_FAILURE 2
  75. /* no execve audit message should be longer than this (userspace limits) */
  76. #define MAX_EXECVE_AUDIT_LEN 7500
  77. /* number of audit rules */
  78. int audit_n_rules;
  79. /* determines whether we collect data for signals sent */
  80. int audit_signals;
  81. struct audit_aux_data {
  82. struct audit_aux_data *next;
  83. int type;
  84. };
  85. #define AUDIT_AUX_IPCPERM 0
  86. /* Number of target pids per aux struct. */
  87. #define AUDIT_AUX_PIDS 16
  88. struct audit_aux_data_execve {
  89. struct audit_aux_data d;
  90. int argc;
  91. int envc;
  92. struct mm_struct *mm;
  93. };
  94. struct audit_aux_data_pids {
  95. struct audit_aux_data d;
  96. pid_t target_pid[AUDIT_AUX_PIDS];
  97. kuid_t target_auid[AUDIT_AUX_PIDS];
  98. kuid_t target_uid[AUDIT_AUX_PIDS];
  99. unsigned int target_sessionid[AUDIT_AUX_PIDS];
  100. u32 target_sid[AUDIT_AUX_PIDS];
  101. char target_comm[AUDIT_AUX_PIDS][TASK_COMM_LEN];
  102. int pid_count;
  103. };
  104. struct audit_aux_data_bprm_fcaps {
  105. struct audit_aux_data d;
  106. struct audit_cap_data fcap;
  107. unsigned int fcap_ver;
  108. struct audit_cap_data old_pcap;
  109. struct audit_cap_data new_pcap;
  110. };
  111. struct audit_tree_refs {
  112. struct audit_tree_refs *next;
  113. struct audit_chunk *c[31];
  114. };
  115. static inline int open_arg(int flags, int mask)
  116. {
  117. int n = ACC_MODE(flags);
  118. if (flags & (O_TRUNC | O_CREAT))
  119. n |= AUDIT_PERM_WRITE;
  120. return n & mask;
  121. }
  122. static int audit_match_perm(struct audit_context *ctx, int mask)
  123. {
  124. unsigned n;
  125. if (unlikely(!ctx))
  126. return 0;
  127. n = ctx->major;
  128. switch (audit_classify_syscall(ctx->arch, n)) {
  129. case 0: /* native */
  130. if ((mask & AUDIT_PERM_WRITE) &&
  131. audit_match_class(AUDIT_CLASS_WRITE, n))
  132. return 1;
  133. if ((mask & AUDIT_PERM_READ) &&
  134. audit_match_class(AUDIT_CLASS_READ, n))
  135. return 1;
  136. if ((mask & AUDIT_PERM_ATTR) &&
  137. audit_match_class(AUDIT_CLASS_CHATTR, n))
  138. return 1;
  139. return 0;
  140. case 1: /* 32bit on biarch */
  141. if ((mask & AUDIT_PERM_WRITE) &&
  142. audit_match_class(AUDIT_CLASS_WRITE_32, n))
  143. return 1;
  144. if ((mask & AUDIT_PERM_READ) &&
  145. audit_match_class(AUDIT_CLASS_READ_32, n))
  146. return 1;
  147. if ((mask & AUDIT_PERM_ATTR) &&
  148. audit_match_class(AUDIT_CLASS_CHATTR_32, n))
  149. return 1;
  150. return 0;
  151. case 2: /* open */
  152. return mask & ACC_MODE(ctx->argv[1]);
  153. case 3: /* openat */
  154. return mask & ACC_MODE(ctx->argv[2]);
  155. case 4: /* socketcall */
  156. return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND);
  157. case 5: /* execve */
  158. return mask & AUDIT_PERM_EXEC;
  159. default:
  160. return 0;
  161. }
  162. }
  163. static int audit_match_filetype(struct audit_context *ctx, int val)
  164. {
  165. struct audit_names *n;
  166. umode_t mode = (umode_t)val;
  167. if (unlikely(!ctx))
  168. return 0;
  169. list_for_each_entry(n, &ctx->names_list, list) {
  170. if ((n->ino != -1) &&
  171. ((n->mode & S_IFMT) == mode))
  172. return 1;
  173. }
  174. return 0;
  175. }
  176. /*
  177. * We keep a linked list of fixed-sized (31 pointer) arrays of audit_chunk *;
  178. * ->first_trees points to its beginning, ->trees - to the current end of data.
  179. * ->tree_count is the number of free entries in array pointed to by ->trees.
  180. * Original condition is (NULL, NULL, 0); as soon as it grows we never revert to NULL,
  181. * "empty" becomes (p, p, 31) afterwards. We don't shrink the list (and seriously,
  182. * it's going to remain 1-element for almost any setup) until we free context itself.
  183. * References in it _are_ dropped - at the same time we free/drop aux stuff.
  184. */
  185. #ifdef CONFIG_AUDIT_TREE
  186. static void audit_set_auditable(struct audit_context *ctx)
  187. {
  188. if (!ctx->prio) {
  189. ctx->prio = 1;
  190. ctx->current_state = AUDIT_RECORD_CONTEXT;
  191. }
  192. }
  193. static int put_tree_ref(struct audit_context *ctx, struct audit_chunk *chunk)
  194. {
  195. struct audit_tree_refs *p = ctx->trees;
  196. int left = ctx->tree_count;
  197. if (likely(left)) {
  198. p->c[--left] = chunk;
  199. ctx->tree_count = left;
  200. return 1;
  201. }
  202. if (!p)
  203. return 0;
  204. p = p->next;
  205. if (p) {
  206. p->c[30] = chunk;
  207. ctx->trees = p;
  208. ctx->tree_count = 30;
  209. return 1;
  210. }
  211. return 0;
  212. }
  213. static int grow_tree_refs(struct audit_context *ctx)
  214. {
  215. struct audit_tree_refs *p = ctx->trees;
  216. ctx->trees = kzalloc(sizeof(struct audit_tree_refs), GFP_KERNEL);
  217. if (!ctx->trees) {
  218. ctx->trees = p;
  219. return 0;
  220. }
  221. if (p)
  222. p->next = ctx->trees;
  223. else
  224. ctx->first_trees = ctx->trees;
  225. ctx->tree_count = 31;
  226. return 1;
  227. }
  228. #endif
  229. static void unroll_tree_refs(struct audit_context *ctx,
  230. struct audit_tree_refs *p, int count)
  231. {
  232. #ifdef CONFIG_AUDIT_TREE
  233. struct audit_tree_refs *q;
  234. int n;
  235. if (!p) {
  236. /* we started with empty chain */
  237. p = ctx->first_trees;
  238. count = 31;
  239. /* if the very first allocation has failed, nothing to do */
  240. if (!p)
  241. return;
  242. }
  243. n = count;
  244. for (q = p; q != ctx->trees; q = q->next, n = 31) {
  245. while (n--) {
  246. audit_put_chunk(q->c[n]);
  247. q->c[n] = NULL;
  248. }
  249. }
  250. while (n-- > ctx->tree_count) {
  251. audit_put_chunk(q->c[n]);
  252. q->c[n] = NULL;
  253. }
  254. ctx->trees = p;
  255. ctx->tree_count = count;
  256. #endif
  257. }
  258. static void free_tree_refs(struct audit_context *ctx)
  259. {
  260. struct audit_tree_refs *p, *q;
  261. for (p = ctx->first_trees; p; p = q) {
  262. q = p->next;
  263. kfree(p);
  264. }
  265. }
  266. static int match_tree_refs(struct audit_context *ctx, struct audit_tree *tree)
  267. {
  268. #ifdef CONFIG_AUDIT_TREE
  269. struct audit_tree_refs *p;
  270. int n;
  271. if (!tree)
  272. return 0;
  273. /* full ones */
  274. for (p = ctx->first_trees; p != ctx->trees; p = p->next) {
  275. for (n = 0; n < 31; n++)
  276. if (audit_tree_match(p->c[n], tree))
  277. return 1;
  278. }
  279. /* partial */
  280. if (p) {
  281. for (n = ctx->tree_count; n < 31; n++)
  282. if (audit_tree_match(p->c[n], tree))
  283. return 1;
  284. }
  285. #endif
  286. return 0;
  287. }
  288. static int audit_compare_uid(kuid_t uid,
  289. struct audit_names *name,
  290. struct audit_field *f,
  291. struct audit_context *ctx)
  292. {
  293. struct audit_names *n;
  294. int rc;
  295. if (name) {
  296. rc = audit_uid_comparator(uid, f->op, name->uid);
  297. if (rc)
  298. return rc;
  299. }
  300. if (ctx) {
  301. list_for_each_entry(n, &ctx->names_list, list) {
  302. rc = audit_uid_comparator(uid, f->op, n->uid);
  303. if (rc)
  304. return rc;
  305. }
  306. }
  307. return 0;
  308. }
  309. static int audit_compare_gid(kgid_t gid,
  310. struct audit_names *name,
  311. struct audit_field *f,
  312. struct audit_context *ctx)
  313. {
  314. struct audit_names *n;
  315. int rc;
  316. if (name) {
  317. rc = audit_gid_comparator(gid, f->op, name->gid);
  318. if (rc)
  319. return rc;
  320. }
  321. if (ctx) {
  322. list_for_each_entry(n, &ctx->names_list, list) {
  323. rc = audit_gid_comparator(gid, f->op, n->gid);
  324. if (rc)
  325. return rc;
  326. }
  327. }
  328. return 0;
  329. }
  330. static int audit_field_compare(struct task_struct *tsk,
  331. const struct cred *cred,
  332. struct audit_field *f,
  333. struct audit_context *ctx,
  334. struct audit_names *name)
  335. {
  336. switch (f->val) {
  337. /* process to file object comparisons */
  338. case AUDIT_COMPARE_UID_TO_OBJ_UID:
  339. return audit_compare_uid(cred->uid, name, f, ctx);
  340. case AUDIT_COMPARE_GID_TO_OBJ_GID:
  341. return audit_compare_gid(cred->gid, name, f, ctx);
  342. case AUDIT_COMPARE_EUID_TO_OBJ_UID:
  343. return audit_compare_uid(cred->euid, name, f, ctx);
  344. case AUDIT_COMPARE_EGID_TO_OBJ_GID:
  345. return audit_compare_gid(cred->egid, name, f, ctx);
  346. case AUDIT_COMPARE_AUID_TO_OBJ_UID:
  347. return audit_compare_uid(tsk->loginuid, name, f, ctx);
  348. case AUDIT_COMPARE_SUID_TO_OBJ_UID:
  349. return audit_compare_uid(cred->suid, name, f, ctx);
  350. case AUDIT_COMPARE_SGID_TO_OBJ_GID:
  351. return audit_compare_gid(cred->sgid, name, f, ctx);
  352. case AUDIT_COMPARE_FSUID_TO_OBJ_UID:
  353. return audit_compare_uid(cred->fsuid, name, f, ctx);
  354. case AUDIT_COMPARE_FSGID_TO_OBJ_GID:
  355. return audit_compare_gid(cred->fsgid, name, f, ctx);
  356. /* uid comparisons */
  357. case AUDIT_COMPARE_UID_TO_AUID:
  358. return audit_uid_comparator(cred->uid, f->op, tsk->loginuid);
  359. case AUDIT_COMPARE_UID_TO_EUID:
  360. return audit_uid_comparator(cred->uid, f->op, cred->euid);
  361. case AUDIT_COMPARE_UID_TO_SUID:
  362. return audit_uid_comparator(cred->uid, f->op, cred->suid);
  363. case AUDIT_COMPARE_UID_TO_FSUID:
  364. return audit_uid_comparator(cred->uid, f->op, cred->fsuid);
  365. /* auid comparisons */
  366. case AUDIT_COMPARE_AUID_TO_EUID:
  367. return audit_uid_comparator(tsk->loginuid, f->op, cred->euid);
  368. case AUDIT_COMPARE_AUID_TO_SUID:
  369. return audit_uid_comparator(tsk->loginuid, f->op, cred->suid);
  370. case AUDIT_COMPARE_AUID_TO_FSUID:
  371. return audit_uid_comparator(tsk->loginuid, f->op, cred->fsuid);
  372. /* euid comparisons */
  373. case AUDIT_COMPARE_EUID_TO_SUID:
  374. return audit_uid_comparator(cred->euid, f->op, cred->suid);
  375. case AUDIT_COMPARE_EUID_TO_FSUID:
  376. return audit_uid_comparator(cred->euid, f->op, cred->fsuid);
  377. /* suid comparisons */
  378. case AUDIT_COMPARE_SUID_TO_FSUID:
  379. return audit_uid_comparator(cred->suid, f->op, cred->fsuid);
  380. /* gid comparisons */
  381. case AUDIT_COMPARE_GID_TO_EGID:
  382. return audit_gid_comparator(cred->gid, f->op, cred->egid);
  383. case AUDIT_COMPARE_GID_TO_SGID:
  384. return audit_gid_comparator(cred->gid, f->op, cred->sgid);
  385. case AUDIT_COMPARE_GID_TO_FSGID:
  386. return audit_gid_comparator(cred->gid, f->op, cred->fsgid);
  387. /* egid comparisons */
  388. case AUDIT_COMPARE_EGID_TO_SGID:
  389. return audit_gid_comparator(cred->egid, f->op, cred->sgid);
  390. case AUDIT_COMPARE_EGID_TO_FSGID:
  391. return audit_gid_comparator(cred->egid, f->op, cred->fsgid);
  392. /* sgid comparison */
  393. case AUDIT_COMPARE_SGID_TO_FSGID:
  394. return audit_gid_comparator(cred->sgid, f->op, cred->fsgid);
  395. default:
  396. WARN(1, "Missing AUDIT_COMPARE define. Report as a bug\n");
  397. return 0;
  398. }
  399. return 0;
  400. }
  401. /* Determine if any context name data matches a rule's watch data */
  402. /* Compare a task_struct with an audit_rule. Return 1 on match, 0
  403. * otherwise.
  404. *
  405. * If task_creation is true, this is an explicit indication that we are
  406. * filtering a task rule at task creation time. This and tsk == current are
  407. * the only situations where tsk->cred may be accessed without an rcu read lock.
  408. */
  409. static int audit_filter_rules(struct task_struct *tsk,
  410. struct audit_krule *rule,
  411. struct audit_context *ctx,
  412. struct audit_names *name,
  413. enum audit_state *state,
  414. bool task_creation)
  415. {
  416. const struct cred *cred;
  417. int i, need_sid = 1;
  418. u32 sid;
  419. cred = rcu_dereference_check(tsk->cred, tsk == current || task_creation);
  420. for (i = 0; i < rule->field_count; i++) {
  421. struct audit_field *f = &rule->fields[i];
  422. struct audit_names *n;
  423. int result = 0;
  424. switch (f->type) {
  425. case AUDIT_PID:
  426. result = audit_comparator(tsk->pid, f->op, f->val);
  427. break;
  428. case AUDIT_PPID:
  429. if (ctx) {
  430. if (!ctx->ppid)
  431. ctx->ppid = sys_getppid();
  432. result = audit_comparator(ctx->ppid, f->op, f->val);
  433. }
  434. break;
  435. case AUDIT_UID:
  436. result = audit_uid_comparator(cred->uid, f->op, f->uid);
  437. break;
  438. case AUDIT_EUID:
  439. result = audit_uid_comparator(cred->euid, f->op, f->uid);
  440. break;
  441. case AUDIT_SUID:
  442. result = audit_uid_comparator(cred->suid, f->op, f->uid);
  443. break;
  444. case AUDIT_FSUID:
  445. result = audit_uid_comparator(cred->fsuid, f->op, f->uid);
  446. break;
  447. case AUDIT_GID:
  448. result = audit_gid_comparator(cred->gid, f->op, f->gid);
  449. if (f->op == Audit_equal) {
  450. if (!result)
  451. result = in_group_p(f->gid);
  452. } else if (f->op == Audit_not_equal) {
  453. if (result)
  454. result = !in_group_p(f->gid);
  455. }
  456. break;
  457. case AUDIT_EGID:
  458. result = audit_gid_comparator(cred->egid, f->op, f->gid);
  459. if (f->op == Audit_equal) {
  460. if (!result)
  461. result = in_egroup_p(f->gid);
  462. } else if (f->op == Audit_not_equal) {
  463. if (result)
  464. result = !in_egroup_p(f->gid);
  465. }
  466. break;
  467. case AUDIT_SGID:
  468. result = audit_gid_comparator(cred->sgid, f->op, f->gid);
  469. break;
  470. case AUDIT_FSGID:
  471. result = audit_gid_comparator(cred->fsgid, f->op, f->gid);
  472. break;
  473. case AUDIT_PERS:
  474. result = audit_comparator(tsk->personality, f->op, f->val);
  475. break;
  476. case AUDIT_ARCH:
  477. if (ctx)
  478. result = audit_comparator(ctx->arch, f->op, f->val);
  479. break;
  480. case AUDIT_EXIT:
  481. if (ctx && ctx->return_valid)
  482. result = audit_comparator(ctx->return_code, f->op, f->val);
  483. break;
  484. case AUDIT_SUCCESS:
  485. if (ctx && ctx->return_valid) {
  486. if (f->val)
  487. result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
  488. else
  489. result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
  490. }
  491. break;
  492. case AUDIT_DEVMAJOR:
  493. if (name) {
  494. if (audit_comparator(MAJOR(name->dev), f->op, f->val) ||
  495. audit_comparator(MAJOR(name->rdev), f->op, f->val))
  496. ++result;
  497. } else if (ctx) {
  498. list_for_each_entry(n, &ctx->names_list, list) {
  499. if (audit_comparator(MAJOR(n->dev), f->op, f->val) ||
  500. audit_comparator(MAJOR(n->rdev), f->op, f->val)) {
  501. ++result;
  502. break;
  503. }
  504. }
  505. }
  506. break;
  507. case AUDIT_DEVMINOR:
  508. if (name) {
  509. if (audit_comparator(MINOR(name->dev), f->op, f->val) ||
  510. audit_comparator(MINOR(name->rdev), f->op, f->val))
  511. ++result;
  512. } else if (ctx) {
  513. list_for_each_entry(n, &ctx->names_list, list) {
  514. if (audit_comparator(MINOR(n->dev), f->op, f->val) ||
  515. audit_comparator(MINOR(n->rdev), f->op, f->val)) {
  516. ++result;
  517. break;
  518. }
  519. }
  520. }
  521. break;
  522. case AUDIT_INODE:
  523. if (name)
  524. result = audit_comparator(name->ino, f->op, f->val);
  525. else if (ctx) {
  526. list_for_each_entry(n, &ctx->names_list, list) {
  527. if (audit_comparator(n->ino, f->op, f->val)) {
  528. ++result;
  529. break;
  530. }
  531. }
  532. }
  533. break;
  534. case AUDIT_OBJ_UID:
  535. if (name) {
  536. result = audit_uid_comparator(name->uid, f->op, f->uid);
  537. } else if (ctx) {
  538. list_for_each_entry(n, &ctx->names_list, list) {
  539. if (audit_uid_comparator(n->uid, f->op, f->uid)) {
  540. ++result;
  541. break;
  542. }
  543. }
  544. }
  545. break;
  546. case AUDIT_OBJ_GID:
  547. if (name) {
  548. result = audit_gid_comparator(name->gid, f->op, f->gid);
  549. } else if (ctx) {
  550. list_for_each_entry(n, &ctx->names_list, list) {
  551. if (audit_gid_comparator(n->gid, f->op, f->gid)) {
  552. ++result;
  553. break;
  554. }
  555. }
  556. }
  557. break;
  558. case AUDIT_WATCH:
  559. if (name)
  560. result = audit_watch_compare(rule->watch, name->ino, name->dev);
  561. break;
  562. case AUDIT_DIR:
  563. if (ctx)
  564. result = match_tree_refs(ctx, rule->tree);
  565. break;
  566. case AUDIT_LOGINUID:
  567. result = 0;
  568. if (ctx)
  569. result = audit_uid_comparator(tsk->loginuid, f->op, f->uid);
  570. break;
  571. case AUDIT_LOGINUID_SET:
  572. result = audit_comparator(audit_loginuid_set(tsk), f->op, f->val);
  573. break;
  574. case AUDIT_SUBJ_USER:
  575. case AUDIT_SUBJ_ROLE:
  576. case AUDIT_SUBJ_TYPE:
  577. case AUDIT_SUBJ_SEN:
  578. case AUDIT_SUBJ_CLR:
  579. /* NOTE: this may return negative values indicating
  580. a temporary error. We simply treat this as a
  581. match for now to avoid losing information that
  582. may be wanted. An error message will also be
  583. logged upon error */
  584. if (f->lsm_rule) {
  585. if (need_sid) {
  586. security_task_getsecid(tsk, &sid);
  587. need_sid = 0;
  588. }
  589. result = security_audit_rule_match(sid, f->type,
  590. f->op,
  591. f->lsm_rule,
  592. ctx);
  593. }
  594. break;
  595. case AUDIT_OBJ_USER:
  596. case AUDIT_OBJ_ROLE:
  597. case AUDIT_OBJ_TYPE:
  598. case AUDIT_OBJ_LEV_LOW:
  599. case AUDIT_OBJ_LEV_HIGH:
  600. /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
  601. also applies here */
  602. if (f->lsm_rule) {
  603. /* Find files that match */
  604. if (name) {
  605. result = security_audit_rule_match(
  606. name->osid, f->type, f->op,
  607. f->lsm_rule, ctx);
  608. } else if (ctx) {
  609. list_for_each_entry(n, &ctx->names_list, list) {
  610. if (security_audit_rule_match(n->osid, f->type,
  611. f->op, f->lsm_rule,
  612. ctx)) {
  613. ++result;
  614. break;
  615. }
  616. }
  617. }
  618. /* Find ipc objects that match */
  619. if (!ctx || ctx->type != AUDIT_IPC)
  620. break;
  621. if (security_audit_rule_match(ctx->ipc.osid,
  622. f->type, f->op,
  623. f->lsm_rule, ctx))
  624. ++result;
  625. }
  626. break;
  627. case AUDIT_ARG0:
  628. case AUDIT_ARG1:
  629. case AUDIT_ARG2:
  630. case AUDIT_ARG3:
  631. if (ctx)
  632. result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
  633. break;
  634. case AUDIT_FILTERKEY:
  635. /* ignore this field for filtering */
  636. result = 1;
  637. break;
  638. case AUDIT_PERM:
  639. result = audit_match_perm(ctx, f->val);
  640. break;
  641. case AUDIT_FILETYPE:
  642. result = audit_match_filetype(ctx, f->val);
  643. break;
  644. case AUDIT_FIELD_COMPARE:
  645. result = audit_field_compare(tsk, cred, f, ctx, name);
  646. break;
  647. }
  648. if (!result)
  649. return 0;
  650. }
  651. if (ctx) {
  652. if (rule->prio <= ctx->prio)
  653. return 0;
  654. if (rule->filterkey) {
  655. kfree(ctx->filterkey);
  656. ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
  657. }
  658. ctx->prio = rule->prio;
  659. }
  660. switch (rule->action) {
  661. case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
  662. case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
  663. }
  664. return 1;
  665. }
  666. /* At process creation time, we can determine if system-call auditing is
  667. * completely disabled for this task. Since we only have the task
  668. * structure at this point, we can only check uid and gid.
  669. */
  670. static enum audit_state audit_filter_task(struct task_struct *tsk, char **key)
  671. {
  672. struct audit_entry *e;
  673. enum audit_state state;
  674. rcu_read_lock();
  675. list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
  676. if (audit_filter_rules(tsk, &e->rule, NULL, NULL,
  677. &state, true)) {
  678. if (state == AUDIT_RECORD_CONTEXT)
  679. *key = kstrdup(e->rule.filterkey, GFP_ATOMIC);
  680. rcu_read_unlock();
  681. return state;
  682. }
  683. }
  684. rcu_read_unlock();
  685. return AUDIT_BUILD_CONTEXT;
  686. }
  687. /* At syscall entry and exit time, this filter is called if the
  688. * audit_state is not low enough that auditing cannot take place, but is
  689. * also not high enough that we already know we have to write an audit
  690. * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
  691. */
  692. static enum audit_state audit_filter_syscall(struct task_struct *tsk,
  693. struct audit_context *ctx,
  694. struct list_head *list)
  695. {
  696. struct audit_entry *e;
  697. enum audit_state state;
  698. if (audit_pid && tsk->tgid == audit_pid)
  699. return AUDIT_DISABLED;
  700. rcu_read_lock();
  701. if (!list_empty(list)) {
  702. int word = AUDIT_WORD(ctx->major);
  703. int bit = AUDIT_BIT(ctx->major);
  704. list_for_each_entry_rcu(e, list, list) {
  705. if ((e->rule.mask[word] & bit) == bit &&
  706. audit_filter_rules(tsk, &e->rule, ctx, NULL,
  707. &state, false)) {
  708. rcu_read_unlock();
  709. ctx->current_state = state;
  710. return state;
  711. }
  712. }
  713. }
  714. rcu_read_unlock();
  715. return AUDIT_BUILD_CONTEXT;
  716. }
  717. /*
  718. * Given an audit_name check the inode hash table to see if they match.
  719. * Called holding the rcu read lock to protect the use of audit_inode_hash
  720. */
  721. static int audit_filter_inode_name(struct task_struct *tsk,
  722. struct audit_names *n,
  723. struct audit_context *ctx) {
  724. int word, bit;
  725. int h = audit_hash_ino((u32)n->ino);
  726. struct list_head *list = &audit_inode_hash[h];
  727. struct audit_entry *e;
  728. enum audit_state state;
  729. word = AUDIT_WORD(ctx->major);
  730. bit = AUDIT_BIT(ctx->major);
  731. if (list_empty(list))
  732. return 0;
  733. list_for_each_entry_rcu(e, list, list) {
  734. if ((e->rule.mask[word] & bit) == bit &&
  735. audit_filter_rules(tsk, &e->rule, ctx, n, &state, false)) {
  736. ctx->current_state = state;
  737. return 1;
  738. }
  739. }
  740. return 0;
  741. }
  742. /* At syscall exit time, this filter is called if any audit_names have been
  743. * collected during syscall processing. We only check rules in sublists at hash
  744. * buckets applicable to the inode numbers in audit_names.
  745. * Regarding audit_state, same rules apply as for audit_filter_syscall().
  746. */
  747. void audit_filter_inodes(struct task_struct *tsk, struct audit_context *ctx)
  748. {
  749. struct audit_names *n;
  750. if (audit_pid && tsk->tgid == audit_pid)
  751. return;
  752. rcu_read_lock();
  753. list_for_each_entry(n, &ctx->names_list, list) {
  754. if (audit_filter_inode_name(tsk, n, ctx))
  755. break;
  756. }
  757. rcu_read_unlock();
  758. }
  759. static inline struct audit_context *audit_get_context(struct task_struct *tsk,
  760. int return_valid,
  761. long return_code)
  762. {
  763. struct audit_context *context = tsk->audit_context;
  764. if (!context)
  765. return NULL;
  766. context->return_valid = return_valid;
  767. /*
  768. * we need to fix up the return code in the audit logs if the actual
  769. * return codes are later going to be fixed up by the arch specific
  770. * signal handlers
  771. *
  772. * This is actually a test for:
  773. * (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) ||
  774. * (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK)
  775. *
  776. * but is faster than a bunch of ||
  777. */
  778. if (unlikely(return_code <= -ERESTARTSYS) &&
  779. (return_code >= -ERESTART_RESTARTBLOCK) &&
  780. (return_code != -ENOIOCTLCMD))
  781. context->return_code = -EINTR;
  782. else
  783. context->return_code = return_code;
  784. if (context->in_syscall && !context->dummy) {
  785. audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
  786. audit_filter_inodes(tsk, context);
  787. }
  788. tsk->audit_context = NULL;
  789. return context;
  790. }
  791. static inline void audit_free_names(struct audit_context *context)
  792. {
  793. struct audit_names *n, *next;
  794. #if AUDIT_DEBUG == 2
  795. if (context->put_count + context->ino_count != context->name_count) {
  796. int i = 0;
  797. printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
  798. " name_count=%d put_count=%d"
  799. " ino_count=%d [NOT freeing]\n",
  800. __FILE__, __LINE__,
  801. context->serial, context->major, context->in_syscall,
  802. context->name_count, context->put_count,
  803. context->ino_count);
  804. list_for_each_entry(n, &context->names_list, list) {
  805. printk(KERN_ERR "names[%d] = %p = %s\n", i++,
  806. n->name, n->name->name ?: "(null)");
  807. }
  808. dump_stack();
  809. return;
  810. }
  811. #endif
  812. #if AUDIT_DEBUG
  813. context->put_count = 0;
  814. context->ino_count = 0;
  815. #endif
  816. list_for_each_entry_safe(n, next, &context->names_list, list) {
  817. list_del(&n->list);
  818. if (n->name && n->name_put)
  819. final_putname(n->name);
  820. if (n->should_free)
  821. kfree(n);
  822. }
  823. context->name_count = 0;
  824. path_put(&context->pwd);
  825. context->pwd.dentry = NULL;
  826. context->pwd.mnt = NULL;
  827. }
  828. static inline void audit_free_aux(struct audit_context *context)
  829. {
  830. struct audit_aux_data *aux;
  831. while ((aux = context->aux)) {
  832. context->aux = aux->next;
  833. kfree(aux);
  834. }
  835. while ((aux = context->aux_pids)) {
  836. context->aux_pids = aux->next;
  837. kfree(aux);
  838. }
  839. }
  840. static inline struct audit_context *audit_alloc_context(enum audit_state state)
  841. {
  842. struct audit_context *context;
  843. context = kzalloc(sizeof(*context), GFP_KERNEL);
  844. if (!context)
  845. return NULL;
  846. context->state = state;
  847. context->prio = state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
  848. INIT_LIST_HEAD(&context->killed_trees);
  849. INIT_LIST_HEAD(&context->names_list);
  850. return context;
  851. }
  852. /**
  853. * audit_alloc - allocate an audit context block for a task
  854. * @tsk: task
  855. *
  856. * Filter on the task information and allocate a per-task audit context
  857. * if necessary. Doing so turns on system call auditing for the
  858. * specified task. This is called from copy_process, so no lock is
  859. * needed.
  860. */
  861. int audit_alloc(struct task_struct *tsk)
  862. {
  863. struct audit_context *context;
  864. enum audit_state state;
  865. char *key = NULL;
  866. if (likely(!audit_ever_enabled))
  867. return 0; /* Return if not auditing. */
  868. state = audit_filter_task(tsk, &key);
  869. if (state == AUDIT_DISABLED) {
  870. clear_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
  871. return 0;
  872. }
  873. if (!(context = audit_alloc_context(state))) {
  874. kfree(key);
  875. audit_log_lost("out of memory in audit_alloc");
  876. return -ENOMEM;
  877. }
  878. context->filterkey = key;
  879. tsk->audit_context = context;
  880. set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
  881. return 0;
  882. }
  883. static inline void audit_free_context(struct audit_context *context)
  884. {
  885. audit_free_names(context);
  886. unroll_tree_refs(context, NULL, 0);
  887. free_tree_refs(context);
  888. audit_free_aux(context);
  889. kfree(context->filterkey);
  890. kfree(context->sockaddr);
  891. kfree(context);
  892. }
  893. static int audit_log_pid_context(struct audit_context *context, pid_t pid,
  894. kuid_t auid, kuid_t uid, unsigned int sessionid,
  895. u32 sid, char *comm)
  896. {
  897. struct audit_buffer *ab;
  898. char *ctx = NULL;
  899. u32 len;
  900. int rc = 0;
  901. ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
  902. if (!ab)
  903. return rc;
  904. audit_log_format(ab, "opid=%d oauid=%d ouid=%d oses=%d", pid,
  905. from_kuid(&init_user_ns, auid),
  906. from_kuid(&init_user_ns, uid), sessionid);
  907. if (sid) {
  908. if (security_secid_to_secctx(sid, &ctx, &len)) {
  909. audit_log_format(ab, " obj=(none)");
  910. rc = 1;
  911. } else {
  912. audit_log_format(ab, " obj=%s", ctx);
  913. security_release_secctx(ctx, len);
  914. }
  915. }
  916. audit_log_format(ab, " ocomm=");
  917. audit_log_untrustedstring(ab, comm);
  918. audit_log_end(ab);
  919. return rc;
  920. }
  921. /*
  922. * to_send and len_sent accounting are very loose estimates. We aren't
  923. * really worried about a hard cap to MAX_EXECVE_AUDIT_LEN so much as being
  924. * within about 500 bytes (next page boundary)
  925. *
  926. * why snprintf? an int is up to 12 digits long. if we just assumed when
  927. * logging that a[%d]= was going to be 16 characters long we would be wasting
  928. * space in every audit message. In one 7500 byte message we can log up to
  929. * about 1000 min size arguments. That comes down to about 50% waste of space
  930. * if we didn't do the snprintf to find out how long arg_num_len was.
  931. */
  932. static int audit_log_single_execve_arg(struct audit_context *context,
  933. struct audit_buffer **ab,
  934. int arg_num,
  935. size_t *len_sent,
  936. const char __user *p,
  937. char *buf)
  938. {
  939. char arg_num_len_buf[12];
  940. const char __user *tmp_p = p;
  941. /* how many digits are in arg_num? 5 is the length of ' a=""' */
  942. size_t arg_num_len = snprintf(arg_num_len_buf, 12, "%d", arg_num) + 5;
  943. size_t len, len_left, to_send;
  944. size_t max_execve_audit_len = MAX_EXECVE_AUDIT_LEN;
  945. unsigned int i, has_cntl = 0, too_long = 0;
  946. int ret;
  947. /* strnlen_user includes the null we don't want to send */
  948. len_left = len = strnlen_user(p, MAX_ARG_STRLEN) - 1;
  949. /*
  950. * We just created this mm, if we can't find the strings
  951. * we just copied into it something is _very_ wrong. Similar
  952. * for strings that are too long, we should not have created
  953. * any.
  954. */
  955. if (unlikely((len == -1) || len > MAX_ARG_STRLEN - 1)) {
  956. WARN_ON(1);
  957. send_sig(SIGKILL, current, 0);
  958. return -1;
  959. }
  960. /* walk the whole argument looking for non-ascii chars */
  961. do {
  962. if (len_left > MAX_EXECVE_AUDIT_LEN)
  963. to_send = MAX_EXECVE_AUDIT_LEN;
  964. else
  965. to_send = len_left;
  966. ret = copy_from_user(buf, tmp_p, to_send);
  967. /*
  968. * There is no reason for this copy to be short. We just
  969. * copied them here, and the mm hasn't been exposed to user-
  970. * space yet.
  971. */
  972. if (ret) {
  973. WARN_ON(1);
  974. send_sig(SIGKILL, current, 0);
  975. return -1;
  976. }
  977. buf[to_send] = '\0';
  978. has_cntl = audit_string_contains_control(buf, to_send);
  979. if (has_cntl) {
  980. /*
  981. * hex messages get logged as 2 bytes, so we can only
  982. * send half as much in each message
  983. */
  984. max_execve_audit_len = MAX_EXECVE_AUDIT_LEN / 2;
  985. break;
  986. }
  987. len_left -= to_send;
  988. tmp_p += to_send;
  989. } while (len_left > 0);
  990. len_left = len;
  991. if (len > max_execve_audit_len)
  992. too_long = 1;
  993. /* rewalk the argument actually logging the message */
  994. for (i = 0; len_left > 0; i++) {
  995. int room_left;
  996. if (len_left > max_execve_audit_len)
  997. to_send = max_execve_audit_len;
  998. else
  999. to_send = len_left;
  1000. /* do we have space left to send this argument in this ab? */
  1001. room_left = MAX_EXECVE_AUDIT_LEN - arg_num_len - *len_sent;
  1002. if (has_cntl)
  1003. room_left -= (to_send * 2);
  1004. else
  1005. room_left -= to_send;
  1006. if (room_left < 0) {
  1007. *len_sent = 0;
  1008. audit_log_end(*ab);
  1009. *ab = audit_log_start(context, GFP_KERNEL, AUDIT_EXECVE);
  1010. if (!*ab)
  1011. return 0;
  1012. }
  1013. /*
  1014. * first record needs to say how long the original string was
  1015. * so we can be sure nothing was lost.
  1016. */
  1017. if ((i == 0) && (too_long))
  1018. audit_log_format(*ab, " a%d_len=%zu", arg_num,
  1019. has_cntl ? 2*len : len);
  1020. /*
  1021. * normally arguments are small enough to fit and we already
  1022. * filled buf above when we checked for control characters
  1023. * so don't bother with another copy_from_user
  1024. */
  1025. if (len >= max_execve_audit_len)
  1026. ret = copy_from_user(buf, p, to_send);
  1027. else
  1028. ret = 0;
  1029. if (ret) {
  1030. WARN_ON(1);
  1031. send_sig(SIGKILL, current, 0);
  1032. return -1;
  1033. }
  1034. buf[to_send] = '\0';
  1035. /* actually log it */
  1036. audit_log_format(*ab, " a%d", arg_num);
  1037. if (too_long)
  1038. audit_log_format(*ab, "[%d]", i);
  1039. audit_log_format(*ab, "=");
  1040. if (has_cntl)
  1041. audit_log_n_hex(*ab, buf, to_send);
  1042. else
  1043. audit_log_string(*ab, buf);
  1044. p += to_send;
  1045. len_left -= to_send;
  1046. *len_sent += arg_num_len;
  1047. if (has_cntl)
  1048. *len_sent += to_send * 2;
  1049. else
  1050. *len_sent += to_send;
  1051. }
  1052. /* include the null we didn't log */
  1053. return len + 1;
  1054. }
  1055. static void audit_log_execve_info(struct audit_context *context,
  1056. struct audit_buffer **ab,
  1057. struct audit_aux_data_execve *axi)
  1058. {
  1059. int i, len;
  1060. size_t len_sent = 0;
  1061. const char __user *p;
  1062. char *buf;
  1063. if (axi->mm != current->mm)
  1064. return; /* execve failed, no additional info */
  1065. p = (const char __user *)axi->mm->arg_start;
  1066. audit_log_format(*ab, "argc=%d", axi->argc);
  1067. /*
  1068. * we need some kernel buffer to hold the userspace args. Just
  1069. * allocate one big one rather than allocating one of the right size
  1070. * for every single argument inside audit_log_single_execve_arg()
  1071. * should be <8k allocation so should be pretty safe.
  1072. */
  1073. buf = kmalloc(MAX_EXECVE_AUDIT_LEN + 1, GFP_KERNEL);
  1074. if (!buf) {
  1075. audit_panic("out of memory for argv string\n");
  1076. return;
  1077. }
  1078. for (i = 0; i < axi->argc; i++) {
  1079. len = audit_log_single_execve_arg(context, ab, i,
  1080. &len_sent, p, buf);
  1081. if (len <= 0)
  1082. break;
  1083. p += len;
  1084. }
  1085. kfree(buf);
  1086. }
  1087. static void show_special(struct audit_context *context, int *call_panic)
  1088. {
  1089. struct audit_buffer *ab;
  1090. int i;
  1091. ab = audit_log_start(context, GFP_KERNEL, context->type);
  1092. if (!ab)
  1093. return;
  1094. switch (context->type) {
  1095. case AUDIT_SOCKETCALL: {
  1096. int nargs = context->socketcall.nargs;
  1097. audit_log_format(ab, "nargs=%d", nargs);
  1098. for (i = 0; i < nargs; i++)
  1099. audit_log_format(ab, " a%d=%lx", i,
  1100. context->socketcall.args[i]);
  1101. break; }
  1102. case AUDIT_IPC: {
  1103. u32 osid = context->ipc.osid;
  1104. audit_log_format(ab, "ouid=%u ogid=%u mode=%#ho",
  1105. from_kuid(&init_user_ns, context->ipc.uid),
  1106. from_kgid(&init_user_ns, context->ipc.gid),
  1107. context->ipc.mode);
  1108. if (osid) {
  1109. char *ctx = NULL;
  1110. u32 len;
  1111. if (security_secid_to_secctx(osid, &ctx, &len)) {
  1112. audit_log_format(ab, " osid=%u", osid);
  1113. *call_panic = 1;
  1114. } else {
  1115. audit_log_format(ab, " obj=%s", ctx);
  1116. security_release_secctx(ctx, len);
  1117. }
  1118. }
  1119. if (context->ipc.has_perm) {
  1120. audit_log_end(ab);
  1121. ab = audit_log_start(context, GFP_KERNEL,
  1122. AUDIT_IPC_SET_PERM);
  1123. if (unlikely(!ab))
  1124. return;
  1125. audit_log_format(ab,
  1126. "qbytes=%lx ouid=%u ogid=%u mode=%#ho",
  1127. context->ipc.qbytes,
  1128. context->ipc.perm_uid,
  1129. context->ipc.perm_gid,
  1130. context->ipc.perm_mode);
  1131. }
  1132. break; }
  1133. case AUDIT_MQ_OPEN: {
  1134. audit_log_format(ab,
  1135. "oflag=0x%x mode=%#ho mq_flags=0x%lx mq_maxmsg=%ld "
  1136. "mq_msgsize=%ld mq_curmsgs=%ld",
  1137. context->mq_open.oflag, context->mq_open.mode,
  1138. context->mq_open.attr.mq_flags,
  1139. context->mq_open.attr.mq_maxmsg,
  1140. context->mq_open.attr.mq_msgsize,
  1141. context->mq_open.attr.mq_curmsgs);
  1142. break; }
  1143. case AUDIT_MQ_SENDRECV: {
  1144. audit_log_format(ab,
  1145. "mqdes=%d msg_len=%zd msg_prio=%u "
  1146. "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
  1147. context->mq_sendrecv.mqdes,
  1148. context->mq_sendrecv.msg_len,
  1149. context->mq_sendrecv.msg_prio,
  1150. context->mq_sendrecv.abs_timeout.tv_sec,
  1151. context->mq_sendrecv.abs_timeout.tv_nsec);
  1152. break; }
  1153. case AUDIT_MQ_NOTIFY: {
  1154. audit_log_format(ab, "mqdes=%d sigev_signo=%d",
  1155. context->mq_notify.mqdes,
  1156. context->mq_notify.sigev_signo);
  1157. break; }
  1158. case AUDIT_MQ_GETSETATTR: {
  1159. struct mq_attr *attr = &context->mq_getsetattr.mqstat;
  1160. audit_log_format(ab,
  1161. "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
  1162. "mq_curmsgs=%ld ",
  1163. context->mq_getsetattr.mqdes,
  1164. attr->mq_flags, attr->mq_maxmsg,
  1165. attr->mq_msgsize, attr->mq_curmsgs);
  1166. break; }
  1167. case AUDIT_CAPSET: {
  1168. audit_log_format(ab, "pid=%d", context->capset.pid);
  1169. audit_log_cap(ab, "cap_pi", &context->capset.cap.inheritable);
  1170. audit_log_cap(ab, "cap_pp", &context->capset.cap.permitted);
  1171. audit_log_cap(ab, "cap_pe", &context->capset.cap.effective);
  1172. break; }
  1173. case AUDIT_MMAP: {
  1174. audit_log_format(ab, "fd=%d flags=0x%x", context->mmap.fd,
  1175. context->mmap.flags);
  1176. break; }
  1177. }
  1178. audit_log_end(ab);
  1179. }
  1180. static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
  1181. {
  1182. int i, call_panic = 0;
  1183. struct audit_buffer *ab;
  1184. struct audit_aux_data *aux;
  1185. struct audit_names *n;
  1186. /* tsk == current */
  1187. context->personality = tsk->personality;
  1188. ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
  1189. if (!ab)
  1190. return; /* audit_panic has been called */
  1191. audit_log_format(ab, "arch=%x syscall=%d",
  1192. context->arch, context->major);
  1193. if (context->personality != PER_LINUX)
  1194. audit_log_format(ab, " per=%lx", context->personality);
  1195. if (context->return_valid)
  1196. audit_log_format(ab, " success=%s exit=%ld",
  1197. (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
  1198. context->return_code);
  1199. audit_log_format(ab,
  1200. " a0=%lx a1=%lx a2=%lx a3=%lx items=%d",
  1201. context->argv[0],
  1202. context->argv[1],
  1203. context->argv[2],
  1204. context->argv[3],
  1205. context->name_count);
  1206. audit_log_task_info(ab, tsk);
  1207. audit_log_key(ab, context->filterkey);
  1208. audit_log_end(ab);
  1209. for (aux = context->aux; aux; aux = aux->next) {
  1210. ab = audit_log_start(context, GFP_KERNEL, aux->type);
  1211. if (!ab)
  1212. continue; /* audit_panic has been called */
  1213. switch (aux->type) {
  1214. case AUDIT_EXECVE: {
  1215. struct audit_aux_data_execve *axi = (void *)aux;
  1216. audit_log_execve_info(context, &ab, axi);
  1217. break; }
  1218. case AUDIT_BPRM_FCAPS: {
  1219. struct audit_aux_data_bprm_fcaps *axs = (void *)aux;
  1220. audit_log_format(ab, "fver=%x", axs->fcap_ver);
  1221. audit_log_cap(ab, "fp", &axs->fcap.permitted);
  1222. audit_log_cap(ab, "fi", &axs->fcap.inheritable);
  1223. audit_log_format(ab, " fe=%d", axs->fcap.fE);
  1224. audit_log_cap(ab, "old_pp", &axs->old_pcap.permitted);
  1225. audit_log_cap(ab, "old_pi", &axs->old_pcap.inheritable);
  1226. audit_log_cap(ab, "old_pe", &axs->old_pcap.effective);
  1227. audit_log_cap(ab, "new_pp", &axs->new_pcap.permitted);
  1228. audit_log_cap(ab, "new_pi", &axs->new_pcap.inheritable);
  1229. audit_log_cap(ab, "new_pe", &axs->new_pcap.effective);
  1230. break; }
  1231. }
  1232. audit_log_end(ab);
  1233. }
  1234. if (context->type)
  1235. show_special(context, &call_panic);
  1236. if (context->fds[0] >= 0) {
  1237. ab = audit_log_start(context, GFP_KERNEL, AUDIT_FD_PAIR);
  1238. if (ab) {
  1239. audit_log_format(ab, "fd0=%d fd1=%d",
  1240. context->fds[0], context->fds[1]);
  1241. audit_log_end(ab);
  1242. }
  1243. }
  1244. if (context->sockaddr_len) {
  1245. ab = audit_log_start(context, GFP_KERNEL, AUDIT_SOCKADDR);
  1246. if (ab) {
  1247. audit_log_format(ab, "saddr=");
  1248. audit_log_n_hex(ab, (void *)context->sockaddr,
  1249. context->sockaddr_len);
  1250. audit_log_end(ab);
  1251. }
  1252. }
  1253. for (aux = context->aux_pids; aux; aux = aux->next) {
  1254. struct audit_aux_data_pids *axs = (void *)aux;
  1255. for (i = 0; i < axs->pid_count; i++)
  1256. if (audit_log_pid_context(context, axs->target_pid[i],
  1257. axs->target_auid[i],
  1258. axs->target_uid[i],
  1259. axs->target_sessionid[i],
  1260. axs->target_sid[i],
  1261. axs->target_comm[i]))
  1262. call_panic = 1;
  1263. }
  1264. if (context->target_pid &&
  1265. audit_log_pid_context(context, context->target_pid,
  1266. context->target_auid, context->target_uid,
  1267. context->target_sessionid,
  1268. context->target_sid, context->target_comm))
  1269. call_panic = 1;
  1270. if (context->pwd.dentry && context->pwd.mnt) {
  1271. ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
  1272. if (ab) {
  1273. audit_log_d_path(ab, " cwd=", &context->pwd);
  1274. audit_log_end(ab);
  1275. }
  1276. }
  1277. i = 0;
  1278. list_for_each_entry(n, &context->names_list, list) {
  1279. if (n->hidden)
  1280. continue;
  1281. audit_log_name(context, n, NULL, i++, &call_panic);
  1282. }
  1283. /* Send end of event record to help user space know we are finished */
  1284. ab = audit_log_start(context, GFP_KERNEL, AUDIT_EOE);
  1285. if (ab)
  1286. audit_log_end(ab);
  1287. if (call_panic)
  1288. audit_panic("error converting sid to string");
  1289. }
  1290. /**
  1291. * audit_free - free a per-task audit context
  1292. * @tsk: task whose audit context block to free
  1293. *
  1294. * Called from copy_process and do_exit
  1295. */
  1296. void __audit_free(struct task_struct *tsk)
  1297. {
  1298. struct audit_context *context;
  1299. context = audit_get_context(tsk, 0, 0);
  1300. if (!context)
  1301. return;
  1302. /* Check for system calls that do not go through the exit
  1303. * function (e.g., exit_group), then free context block.
  1304. * We use GFP_ATOMIC here because we might be doing this
  1305. * in the context of the idle thread */
  1306. /* that can happen only if we are called from do_exit() */
  1307. if (context->in_syscall && context->current_state == AUDIT_RECORD_CONTEXT)
  1308. audit_log_exit(context, tsk);
  1309. if (!list_empty(&context->killed_trees))
  1310. audit_kill_trees(&context->killed_trees);
  1311. audit_free_context(context);
  1312. }
  1313. /**
  1314. * audit_syscall_entry - fill in an audit record at syscall entry
  1315. * @arch: architecture type
  1316. * @major: major syscall type (function)
  1317. * @a1: additional syscall register 1
  1318. * @a2: additional syscall register 2
  1319. * @a3: additional syscall register 3
  1320. * @a4: additional syscall register 4
  1321. *
  1322. * Fill in audit context at syscall entry. This only happens if the
  1323. * audit context was created when the task was created and the state or
  1324. * filters demand the audit context be built. If the state from the
  1325. * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
  1326. * then the record will be written at syscall exit time (otherwise, it
  1327. * will only be written if another part of the kernel requests that it
  1328. * be written).
  1329. */
  1330. void __audit_syscall_entry(int arch, int major,
  1331. unsigned long a1, unsigned long a2,
  1332. unsigned long a3, unsigned long a4)
  1333. {
  1334. struct task_struct *tsk = current;
  1335. struct audit_context *context = tsk->audit_context;
  1336. enum audit_state state;
  1337. if (!context)
  1338. return;
  1339. BUG_ON(context->in_syscall || context->name_count);
  1340. if (!audit_enabled)
  1341. return;
  1342. context->arch = arch;
  1343. context->major = major;
  1344. context->argv[0] = a1;
  1345. context->argv[1] = a2;
  1346. context->argv[2] = a3;
  1347. context->argv[3] = a4;
  1348. state = context->state;
  1349. context->dummy = !audit_n_rules;
  1350. if (!context->dummy && state == AUDIT_BUILD_CONTEXT) {
  1351. context->prio = 0;
  1352. state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
  1353. }
  1354. if (state == AUDIT_DISABLED)
  1355. return;
  1356. context->serial = 0;
  1357. context->ctime = CURRENT_TIME;
  1358. context->in_syscall = 1;
  1359. context->current_state = state;
  1360. context->ppid = 0;
  1361. }
  1362. /**
  1363. * audit_syscall_exit - deallocate audit context after a system call
  1364. * @success: success value of the syscall
  1365. * @return_code: return value of the syscall
  1366. *
  1367. * Tear down after system call. If the audit context has been marked as
  1368. * auditable (either because of the AUDIT_RECORD_CONTEXT state from
  1369. * filtering, or because some other part of the kernel wrote an audit
  1370. * message), then write out the syscall information. In call cases,
  1371. * free the names stored from getname().
  1372. */
  1373. void __audit_syscall_exit(int success, long return_code)
  1374. {
  1375. struct task_struct *tsk = current;
  1376. struct audit_context *context;
  1377. if (success)
  1378. success = AUDITSC_SUCCESS;
  1379. else
  1380. success = AUDITSC_FAILURE;
  1381. context = audit_get_context(tsk, success, return_code);
  1382. if (!context)
  1383. return;
  1384. if (context->in_syscall && context->current_state == AUDIT_RECORD_CONTEXT)
  1385. audit_log_exit(context, tsk);
  1386. context->in_syscall = 0;
  1387. context->prio = context->state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
  1388. if (!list_empty(&context->killed_trees))
  1389. audit_kill_trees(&context->killed_trees);
  1390. audit_free_names(context);
  1391. unroll_tree_refs(context, NULL, 0);
  1392. audit_free_aux(context);
  1393. context->aux = NULL;
  1394. context->aux_pids = NULL;
  1395. context->target_pid = 0;
  1396. context->target_sid = 0;
  1397. context->sockaddr_len = 0;
  1398. context->type = 0;
  1399. context->fds[0] = -1;
  1400. if (context->state != AUDIT_RECORD_CONTEXT) {
  1401. kfree(context->filterkey);
  1402. context->filterkey = NULL;
  1403. }
  1404. tsk->audit_context = context;
  1405. }
  1406. static inline void handle_one(const struct inode *inode)
  1407. {
  1408. #ifdef CONFIG_AUDIT_TREE
  1409. struct audit_context *context;
  1410. struct audit_tree_refs *p;
  1411. struct audit_chunk *chunk;
  1412. int count;
  1413. if (likely(hlist_empty(&inode->i_fsnotify_marks)))
  1414. return;
  1415. context = current->audit_context;
  1416. p = context->trees;
  1417. count = context->tree_count;
  1418. rcu_read_lock();
  1419. chunk = audit_tree_lookup(inode);
  1420. rcu_read_unlock();
  1421. if (!chunk)
  1422. return;
  1423. if (likely(put_tree_ref(context, chunk)))
  1424. return;
  1425. if (unlikely(!grow_tree_refs(context))) {
  1426. printk(KERN_WARNING "out of memory, audit has lost a tree reference\n");
  1427. audit_set_auditable(context);
  1428. audit_put_chunk(chunk);
  1429. unroll_tree_refs(context, p, count);
  1430. return;
  1431. }
  1432. put_tree_ref(context, chunk);
  1433. #endif
  1434. }
  1435. static void handle_path(const struct dentry *dentry)
  1436. {
  1437. #ifdef CONFIG_AUDIT_TREE
  1438. struct audit_context *context;
  1439. struct audit_tree_refs *p;
  1440. const struct dentry *d, *parent;
  1441. struct audit_chunk *drop;
  1442. unsigned long seq;
  1443. int count;
  1444. context = current->audit_context;
  1445. p = context->trees;
  1446. count = context->tree_count;
  1447. retry:
  1448. drop = NULL;
  1449. d = dentry;
  1450. rcu_read_lock();
  1451. seq = read_seqbegin(&rename_lock);
  1452. for(;;) {
  1453. struct inode *inode = d->d_inode;
  1454. if (inode && unlikely(!hlist_empty(&inode->i_fsnotify_marks))) {
  1455. struct audit_chunk *chunk;
  1456. chunk = audit_tree_lookup(inode);
  1457. if (chunk) {
  1458. if (unlikely(!put_tree_ref(context, chunk))) {
  1459. drop = chunk;
  1460. break;
  1461. }
  1462. }
  1463. }
  1464. parent = d->d_parent;
  1465. if (parent == d)
  1466. break;
  1467. d = parent;
  1468. }
  1469. if (unlikely(read_seqretry(&rename_lock, seq) || drop)) { /* in this order */
  1470. rcu_read_unlock();
  1471. if (!drop) {
  1472. /* just a race with rename */
  1473. unroll_tree_refs(context, p, count);
  1474. goto retry;
  1475. }
  1476. audit_put_chunk(drop);
  1477. if (grow_tree_refs(context)) {
  1478. /* OK, got more space */
  1479. unroll_tree_refs(context, p, count);
  1480. goto retry;
  1481. }
  1482. /* too bad */
  1483. printk(KERN_WARNING
  1484. "out of memory, audit has lost a tree reference\n");
  1485. unroll_tree_refs(context, p, count);
  1486. audit_set_auditable(context);
  1487. return;
  1488. }
  1489. rcu_read_unlock();
  1490. #endif
  1491. }
  1492. static struct audit_names *audit_alloc_name(struct audit_context *context,
  1493. unsigned char type)
  1494. {
  1495. struct audit_names *aname;
  1496. if (context->name_count < AUDIT_NAMES) {
  1497. aname = &context->preallocated_names[context->name_count];
  1498. memset(aname, 0, sizeof(*aname));
  1499. } else {
  1500. aname = kzalloc(sizeof(*aname), GFP_NOFS);
  1501. if (!aname)
  1502. return NULL;
  1503. aname->should_free = true;
  1504. }
  1505. aname->ino = (unsigned long)-1;
  1506. aname->type = type;
  1507. list_add_tail(&aname->list, &context->names_list);
  1508. context->name_count++;
  1509. #if AUDIT_DEBUG
  1510. context->ino_count++;
  1511. #endif
  1512. return aname;
  1513. }
  1514. /**
  1515. * audit_reusename - fill out filename with info from existing entry
  1516. * @uptr: userland ptr to pathname
  1517. *
  1518. * Search the audit_names list for the current audit context. If there is an
  1519. * existing entry with a matching "uptr" then return the filename
  1520. * associated with that audit_name. If not, return NULL.
  1521. */
  1522. struct filename *
  1523. __audit_reusename(const __user char *uptr)
  1524. {
  1525. struct audit_context *context = current->audit_context;
  1526. struct audit_names *n;
  1527. list_for_each_entry(n, &context->names_list, list) {
  1528. if (!n->name)
  1529. continue;
  1530. if (n->name->uptr == uptr)
  1531. return n->name;
  1532. }
  1533. return NULL;
  1534. }
  1535. /**
  1536. * audit_getname - add a name to the list
  1537. * @name: name to add
  1538. *
  1539. * Add a name to the list of audit names for this context.
  1540. * Called from fs/namei.c:getname().
  1541. */
  1542. void __audit_getname(struct filename *name)
  1543. {
  1544. struct audit_context *context = current->audit_context;
  1545. struct audit_names *n;
  1546. if (!context->in_syscall) {
  1547. #if AUDIT_DEBUG == 2
  1548. printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
  1549. __FILE__, __LINE__, context->serial, name);
  1550. dump_stack();
  1551. #endif
  1552. return;
  1553. }
  1554. #if AUDIT_DEBUG
  1555. /* The filename _must_ have a populated ->name */
  1556. BUG_ON(!name->name);
  1557. #endif
  1558. n = audit_alloc_name(context, AUDIT_TYPE_UNKNOWN);
  1559. if (!n)
  1560. return;
  1561. n->name = name;
  1562. n->name_len = AUDIT_NAME_FULL;
  1563. n->name_put = true;
  1564. name->aname = n;
  1565. if (!context->pwd.dentry)
  1566. get_fs_pwd(current->fs, &context->pwd);
  1567. }
  1568. /* audit_putname - intercept a putname request
  1569. * @name: name to intercept and delay for putname
  1570. *
  1571. * If we have stored the name from getname in the audit context,
  1572. * then we delay the putname until syscall exit.
  1573. * Called from include/linux/fs.h:putname().
  1574. */
  1575. void audit_putname(struct filename *name)
  1576. {
  1577. struct audit_context *context = current->audit_context;
  1578. BUG_ON(!context);
  1579. if (!context->in_syscall) {
  1580. #if AUDIT_DEBUG == 2
  1581. printk(KERN_ERR "%s:%d(:%d): final_putname(%p)\n",
  1582. __FILE__, __LINE__, context->serial, name);
  1583. if (context->name_count) {
  1584. struct audit_names *n;
  1585. int i = 0;
  1586. list_for_each_entry(n, &context->names_list, list)
  1587. printk(KERN_ERR "name[%d] = %p = %s\n", i++,
  1588. n->name, n->name->name ?: "(null)");
  1589. }
  1590. #endif
  1591. final_putname(name);
  1592. }
  1593. #if AUDIT_DEBUG
  1594. else {
  1595. ++context->put_count;
  1596. if (context->put_count > context->name_count) {
  1597. printk(KERN_ERR "%s:%d(:%d): major=%d"
  1598. " in_syscall=%d putname(%p) name_count=%d"
  1599. " put_count=%d\n",
  1600. __FILE__, __LINE__,
  1601. context->serial, context->major,
  1602. context->in_syscall, name->name,
  1603. context->name_count, context->put_count);
  1604. dump_stack();
  1605. }
  1606. }
  1607. #endif
  1608. }
  1609. /**
  1610. * __audit_inode - store the inode and device from a lookup
  1611. * @name: name being audited
  1612. * @dentry: dentry being audited
  1613. * @flags: attributes for this particular entry
  1614. */
  1615. void __audit_inode(struct filename *name, const struct dentry *dentry,
  1616. unsigned int flags)
  1617. {
  1618. struct audit_context *context = current->audit_context;
  1619. const struct inode *inode = dentry->d_inode;
  1620. struct audit_names *n;
  1621. bool parent = flags & AUDIT_INODE_PARENT;
  1622. if (!context->in_syscall)
  1623. return;
  1624. if (!name)
  1625. goto out_alloc;
  1626. #if AUDIT_DEBUG
  1627. /* The struct filename _must_ have a populated ->name */
  1628. BUG_ON(!name->name);
  1629. #endif
  1630. /*
  1631. * If we have a pointer to an audit_names entry already, then we can
  1632. * just use it directly if the type is correct.
  1633. */
  1634. n = name->aname;
  1635. if (n) {
  1636. if (parent) {
  1637. if (n->type == AUDIT_TYPE_PARENT ||
  1638. n->type == AUDIT_TYPE_UNKNOWN)
  1639. goto out;
  1640. } else {
  1641. if (n->type != AUDIT_TYPE_PARENT)
  1642. goto out;
  1643. }
  1644. }
  1645. list_for_each_entry_reverse(n, &context->names_list, list) {
  1646. /* does the name pointer match? */
  1647. if (!n->name || n->name->name != name->name)
  1648. continue;
  1649. /* match the correct record type */
  1650. if (parent) {
  1651. if (n->type == AUDIT_TYPE_PARENT ||
  1652. n->type == AUDIT_TYPE_UNKNOWN)
  1653. goto out;
  1654. } else {
  1655. if (n->type != AUDIT_TYPE_PARENT)
  1656. goto out;
  1657. }
  1658. }
  1659. out_alloc:
  1660. /* unable to find the name from a previous getname(). Allocate a new
  1661. * anonymous entry.
  1662. */
  1663. n = audit_alloc_name(context, AUDIT_TYPE_NORMAL);
  1664. if (!n)
  1665. return;
  1666. out:
  1667. if (parent) {
  1668. n->name_len = n->name ? parent_len(n->name->name) : AUDIT_NAME_FULL;
  1669. n->type = AUDIT_TYPE_PARENT;
  1670. if (flags & AUDIT_INODE_HIDDEN)
  1671. n->hidden = true;
  1672. } else {
  1673. n->name_len = AUDIT_NAME_FULL;
  1674. n->type = AUDIT_TYPE_NORMAL;
  1675. }
  1676. handle_path(dentry);
  1677. audit_copy_inode(n, dentry, inode);
  1678. }
  1679. /**
  1680. * __audit_inode_child - collect inode info for created/removed objects
  1681. * @parent: inode of dentry parent
  1682. * @dentry: dentry being audited
  1683. * @type: AUDIT_TYPE_* value that we're looking for
  1684. *
  1685. * For syscalls that create or remove filesystem objects, audit_inode
  1686. * can only collect information for the filesystem object's parent.
  1687. * This call updates the audit context with the child's information.
  1688. * Syscalls that create a new filesystem object must be hooked after
  1689. * the object is created. Syscalls that remove a filesystem object
  1690. * must be hooked prior, in order to capture the target inode during
  1691. * unsuccessful attempts.
  1692. */
  1693. void __audit_inode_child(const struct inode *parent,
  1694. const struct dentry *dentry,
  1695. const unsigned char type)
  1696. {
  1697. struct audit_context *context = current->audit_context;
  1698. const struct inode *inode = dentry->d_inode;
  1699. const char *dname = dentry->d_name.name;
  1700. struct audit_names *n, *found_parent = NULL, *found_child = NULL;
  1701. if (!context->in_syscall)
  1702. return;
  1703. if (inode)
  1704. handle_one(inode);
  1705. /* look for a parent entry first */
  1706. list_for_each_entry(n, &context->names_list, list) {
  1707. if (!n->name || n->type != AUDIT_TYPE_PARENT)
  1708. continue;
  1709. if (n->ino == parent->i_ino &&
  1710. !audit_compare_dname_path(dname, n->name->name, n->name_len)) {
  1711. found_parent = n;
  1712. break;
  1713. }
  1714. }
  1715. /* is there a matching child entry? */
  1716. list_for_each_entry(n, &context->names_list, list) {
  1717. /* can only match entries that have a name */
  1718. if (!n->name || n->type != type)
  1719. continue;
  1720. /* if we found a parent, make sure this one is a child of it */
  1721. if (found_parent && (n->name != found_parent->name))
  1722. continue;
  1723. if (!strcmp(dname, n->name->name) ||
  1724. !audit_compare_dname_path(dname, n->name->name,
  1725. found_parent ?
  1726. found_parent->name_len :
  1727. AUDIT_NAME_FULL)) {
  1728. found_child = n;
  1729. break;
  1730. }
  1731. }
  1732. if (!found_parent) {
  1733. /* create a new, "anonymous" parent record */
  1734. n = audit_alloc_name(context, AUDIT_TYPE_PARENT);
  1735. if (!n)
  1736. return;
  1737. audit_copy_inode(n, NULL, parent);
  1738. }
  1739. if (!found_child) {
  1740. found_child = audit_alloc_name(context, type);
  1741. if (!found_child)
  1742. return;
  1743. /* Re-use the name belonging to the slot for a matching parent
  1744. * directory. All names for this context are relinquished in
  1745. * audit_free_names() */
  1746. if (found_parent) {
  1747. found_child->name = found_parent->name;
  1748. found_child->name_len = AUDIT_NAME_FULL;
  1749. /* don't call __putname() */
  1750. found_child->name_put = false;
  1751. }
  1752. }
  1753. if (inode)
  1754. audit_copy_inode(found_child, dentry, inode);
  1755. else
  1756. found_child->ino = (unsigned long)-1;
  1757. }
  1758. EXPORT_SYMBOL_GPL(__audit_inode_child);
  1759. /**
  1760. * auditsc_get_stamp - get local copies of audit_context values
  1761. * @ctx: audit_context for the task
  1762. * @t: timespec to store time recorded in the audit_context
  1763. * @serial: serial value that is recorded in the audit_context
  1764. *
  1765. * Also sets the context as auditable.
  1766. */
  1767. int auditsc_get_stamp(struct audit_context *ctx,
  1768. struct timespec *t, unsigned int *serial)
  1769. {
  1770. if (!ctx->in_syscall)
  1771. return 0;
  1772. if (!ctx->serial)
  1773. ctx->serial = audit_serial();
  1774. t->tv_sec = ctx->ctime.tv_sec;
  1775. t->tv_nsec = ctx->ctime.tv_nsec;
  1776. *serial = ctx->serial;
  1777. if (!ctx->prio) {
  1778. ctx->prio = 1;
  1779. ctx->current_state = AUDIT_RECORD_CONTEXT;
  1780. }
  1781. return 1;
  1782. }
  1783. /* global counter which is incremented every time something logs in */
  1784. static atomic_t session_id = ATOMIC_INIT(0);
  1785. static int audit_set_loginuid_perm(kuid_t loginuid)
  1786. {
  1787. /* if we are unset, we don't need privs */
  1788. if (!audit_loginuid_set(current))
  1789. return 0;
  1790. /* if AUDIT_FEATURE_LOGINUID_IMMUTABLE means never ever allow a change*/
  1791. if (is_audit_feature_set(AUDIT_FEATURE_LOGINUID_IMMUTABLE))
  1792. return -EPERM;
  1793. /* it is set, you need permission */
  1794. if (!capable(CAP_AUDIT_CONTROL))
  1795. return -EPERM;
  1796. /* reject if this is not an unset and we don't allow that */
  1797. if (is_audit_feature_set(AUDIT_FEATURE_ONLY_UNSET_LOGINUID) && uid_valid(loginuid))
  1798. return -EPERM;
  1799. return 0;
  1800. }
  1801. static void audit_log_set_loginuid(kuid_t koldloginuid, kuid_t kloginuid,
  1802. unsigned int oldsessionid, unsigned int sessionid,
  1803. int rc)
  1804. {
  1805. struct audit_buffer *ab;
  1806. uid_t uid, ologinuid, nloginuid;
  1807. uid = from_kuid(&init_user_ns, task_uid(current));
  1808. ologinuid = from_kuid(&init_user_ns, koldloginuid);
  1809. nloginuid = from_kuid(&init_user_ns, kloginuid),
  1810. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
  1811. if (!ab)
  1812. return;
  1813. audit_log_format(ab, "pid=%d uid=%u old auid=%u new auid=%u old "
  1814. "ses=%u new ses=%u res=%d", current->pid, uid, ologinuid,
  1815. nloginuid, oldsessionid, sessionid, !rc);
  1816. audit_log_end(ab);
  1817. }
  1818. /**
  1819. * audit_set_loginuid - set current task's audit_context loginuid
  1820. * @loginuid: loginuid value
  1821. *
  1822. * Returns 0.
  1823. *
  1824. * Called (set) from fs/proc/base.c::proc_loginuid_write().
  1825. */
  1826. int audit_set_loginuid(kuid_t loginuid)
  1827. {
  1828. struct task_struct *task = current;
  1829. unsigned int sessionid = -1;
  1830. kuid_t oldloginuid, oldsessionid;
  1831. int rc;
  1832. oldloginuid = audit_get_loginuid(current);
  1833. oldsessionid = audit_get_sessionid(current);
  1834. rc = audit_set_loginuid_perm(loginuid);
  1835. if (rc)
  1836. goto out;
  1837. /* are we setting or clearing? */
  1838. if (uid_valid(loginuid))
  1839. sessionid = atomic_inc_return(&session_id);
  1840. task->sessionid = sessionid;
  1841. task->loginuid = loginuid;
  1842. out:
  1843. audit_log_set_loginuid(oldloginuid, loginuid, oldsessionid, sessionid, rc);
  1844. return rc;
  1845. }
  1846. /**
  1847. * __audit_mq_open - record audit data for a POSIX MQ open
  1848. * @oflag: open flag
  1849. * @mode: mode bits
  1850. * @attr: queue attributes
  1851. *
  1852. */
  1853. void __audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr)
  1854. {
  1855. struct audit_context *context = current->audit_context;
  1856. if (attr)
  1857. memcpy(&context->mq_open.attr, attr, sizeof(struct mq_attr));
  1858. else
  1859. memset(&context->mq_open.attr, 0, sizeof(struct mq_attr));
  1860. context->mq_open.oflag = oflag;
  1861. context->mq_open.mode = mode;
  1862. context->type = AUDIT_MQ_OPEN;
  1863. }
  1864. /**
  1865. * __audit_mq_sendrecv - record audit data for a POSIX MQ timed send/receive
  1866. * @mqdes: MQ descriptor
  1867. * @msg_len: Message length
  1868. * @msg_prio: Message priority
  1869. * @abs_timeout: Message timeout in absolute time
  1870. *
  1871. */
  1872. void __audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
  1873. const struct timespec *abs_timeout)
  1874. {
  1875. struct audit_context *context = current->audit_context;
  1876. struct timespec *p = &context->mq_sendrecv.abs_timeout;
  1877. if (abs_timeout)
  1878. memcpy(p, abs_timeout, sizeof(struct timespec));
  1879. else
  1880. memset(p, 0, sizeof(struct timespec));
  1881. context->mq_sendrecv.mqdes = mqdes;
  1882. context->mq_sendrecv.msg_len = msg_len;
  1883. context->mq_sendrecv.msg_prio = msg_prio;
  1884. context->type = AUDIT_MQ_SENDRECV;
  1885. }
  1886. /**
  1887. * __audit_mq_notify - record audit data for a POSIX MQ notify
  1888. * @mqdes: MQ descriptor
  1889. * @notification: Notification event
  1890. *
  1891. */
  1892. void __audit_mq_notify(mqd_t mqdes, const struct sigevent *notification)
  1893. {
  1894. struct audit_context *context = current->audit_context;
  1895. if (notification)
  1896. context->mq_notify.sigev_signo = notification->sigev_signo;
  1897. else
  1898. context->mq_notify.sigev_signo = 0;
  1899. context->mq_notify.mqdes = mqdes;
  1900. context->type = AUDIT_MQ_NOTIFY;
  1901. }
  1902. /**
  1903. * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
  1904. * @mqdes: MQ descriptor
  1905. * @mqstat: MQ flags
  1906. *
  1907. */
  1908. void __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
  1909. {
  1910. struct audit_context *context = current->audit_context;
  1911. context->mq_getsetattr.mqdes = mqdes;
  1912. context->mq_getsetattr.mqstat = *mqstat;
  1913. context->type = AUDIT_MQ_GETSETATTR;
  1914. }
  1915. /**
  1916. * audit_ipc_obj - record audit data for ipc object
  1917. * @ipcp: ipc permissions
  1918. *
  1919. */
  1920. void __audit_ipc_obj(struct kern_ipc_perm *ipcp)
  1921. {
  1922. struct audit_context *context = current->audit_context;
  1923. context->ipc.uid = ipcp->uid;
  1924. context->ipc.gid = ipcp->gid;
  1925. context->ipc.mode = ipcp->mode;
  1926. context->ipc.has_perm = 0;
  1927. security_ipc_getsecid(ipcp, &context->ipc.osid);
  1928. context->type = AUDIT_IPC;
  1929. }
  1930. /**
  1931. * audit_ipc_set_perm - record audit data for new ipc permissions
  1932. * @qbytes: msgq bytes
  1933. * @uid: msgq user id
  1934. * @gid: msgq group id
  1935. * @mode: msgq mode (permissions)
  1936. *
  1937. * Called only after audit_ipc_obj().
  1938. */
  1939. void __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, umode_t mode)
  1940. {
  1941. struct audit_context *context = current->audit_context;
  1942. context->ipc.qbytes = qbytes;
  1943. context->ipc.perm_uid = uid;
  1944. context->ipc.perm_gid = gid;
  1945. context->ipc.perm_mode = mode;
  1946. context->ipc.has_perm = 1;
  1947. }
  1948. int __audit_bprm(struct linux_binprm *bprm)
  1949. {
  1950. struct audit_aux_data_execve *ax;
  1951. struct audit_context *context = current->audit_context;
  1952. ax = kmalloc(sizeof(*ax), GFP_KERNEL);
  1953. if (!ax)
  1954. return -ENOMEM;
  1955. ax->argc = bprm->argc;
  1956. ax->envc = bprm->envc;
  1957. ax->mm = bprm->mm;
  1958. ax->d.type = AUDIT_EXECVE;
  1959. ax->d.next = context->aux;
  1960. context->aux = (void *)ax;
  1961. return 0;
  1962. }
  1963. /**
  1964. * audit_socketcall - record audit data for sys_socketcall
  1965. * @nargs: number of args, which should not be more than AUDITSC_ARGS.
  1966. * @args: args array
  1967. *
  1968. */
  1969. int __audit_socketcall(int nargs, unsigned long *args)
  1970. {
  1971. struct audit_context *context = current->audit_context;
  1972. if (nargs <= 0 || nargs > AUDITSC_ARGS || !args)
  1973. return -EINVAL;
  1974. context->type = AUDIT_SOCKETCALL;
  1975. context->socketcall.nargs = nargs;
  1976. memcpy(context->socketcall.args, args, nargs * sizeof(unsigned long));
  1977. return 0;
  1978. }
  1979. /**
  1980. * __audit_fd_pair - record audit data for pipe and socketpair
  1981. * @fd1: the first file descriptor
  1982. * @fd2: the second file descriptor
  1983. *
  1984. */
  1985. void __audit_fd_pair(int fd1, int fd2)
  1986. {
  1987. struct audit_context *context = current->audit_context;
  1988. context->fds[0] = fd1;
  1989. context->fds[1] = fd2;
  1990. }
  1991. /**
  1992. * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
  1993. * @len: data length in user space
  1994. * @a: data address in kernel space
  1995. *
  1996. * Returns 0 for success or NULL context or < 0 on error.
  1997. */
  1998. int __audit_sockaddr(int len, void *a)
  1999. {
  2000. struct audit_context *context = current->audit_context;
  2001. if (!context->sockaddr) {
  2002. void *p = kmalloc(sizeof(struct sockaddr_storage), GFP_KERNEL);
  2003. if (!p)
  2004. return -ENOMEM;
  2005. context->sockaddr = p;
  2006. }
  2007. context->sockaddr_len = len;
  2008. memcpy(context->sockaddr, a, len);
  2009. return 0;
  2010. }
  2011. void __audit_ptrace(struct task_struct *t)
  2012. {
  2013. struct audit_context *context = current->audit_context;
  2014. context->target_pid = t->pid;
  2015. context->target_auid = audit_get_loginuid(t);
  2016. context->target_uid = task_uid(t);
  2017. context->target_sessionid = audit_get_sessionid(t);
  2018. security_task_getsecid(t, &context->target_sid);
  2019. memcpy(context->target_comm, t->comm, TASK_COMM_LEN);
  2020. }
  2021. /**
  2022. * audit_signal_info - record signal info for shutting down audit subsystem
  2023. * @sig: signal value
  2024. * @t: task being signaled
  2025. *
  2026. * If the audit subsystem is being terminated, record the task (pid)
  2027. * and uid that is doing that.
  2028. */
  2029. int __audit_signal_info(int sig, struct task_struct *t)
  2030. {
  2031. struct audit_aux_data_pids *axp;
  2032. struct task_struct *tsk = current;
  2033. struct audit_context *ctx = tsk->audit_context;
  2034. kuid_t uid = current_uid(), t_uid = task_uid(t);
  2035. if (audit_pid && t->tgid == audit_pid) {
  2036. if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1 || sig == SIGUSR2) {
  2037. audit_sig_pid = tsk->pid;
  2038. if (uid_valid(tsk->loginuid))
  2039. audit_sig_uid = tsk->loginuid;
  2040. else
  2041. audit_sig_uid = uid;
  2042. security_task_getsecid(tsk, &audit_sig_sid);
  2043. }
  2044. if (!audit_signals || audit_dummy_context())
  2045. return 0;
  2046. }
  2047. /* optimize the common case by putting first signal recipient directly
  2048. * in audit_context */
  2049. if (!ctx->target_pid) {
  2050. ctx->target_pid = t->tgid;
  2051. ctx->target_auid = audit_get_loginuid(t);
  2052. ctx->target_uid = t_uid;
  2053. ctx->target_sessionid = audit_get_sessionid(t);
  2054. security_task_getsecid(t, &ctx->target_sid);
  2055. memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN);
  2056. return 0;
  2057. }
  2058. axp = (void *)ctx->aux_pids;
  2059. if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
  2060. axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
  2061. if (!axp)
  2062. return -ENOMEM;
  2063. axp->d.type = AUDIT_OBJ_PID;
  2064. axp->d.next = ctx->aux_pids;
  2065. ctx->aux_pids = (void *)axp;
  2066. }
  2067. BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS);
  2068. axp->target_pid[axp->pid_count] = t->tgid;
  2069. axp->target_auid[axp->pid_count] = audit_get_loginuid(t);
  2070. axp->target_uid[axp->pid_count] = t_uid;
  2071. axp->target_sessionid[axp->pid_count] = audit_get_sessionid(t);
  2072. security_task_getsecid(t, &axp->target_sid[axp->pid_count]);
  2073. memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN);
  2074. axp->pid_count++;
  2075. return 0;
  2076. }
  2077. /**
  2078. * __audit_log_bprm_fcaps - store information about a loading bprm and relevant fcaps
  2079. * @bprm: pointer to the bprm being processed
  2080. * @new: the proposed new credentials
  2081. * @old: the old credentials
  2082. *
  2083. * Simply check if the proc already has the caps given by the file and if not
  2084. * store the priv escalation info for later auditing at the end of the syscall
  2085. *
  2086. * -Eric
  2087. */
  2088. int __audit_log_bprm_fcaps(struct linux_binprm *bprm,
  2089. const struct cred *new, const struct cred *old)
  2090. {
  2091. struct audit_aux_data_bprm_fcaps *ax;
  2092. struct audit_context *context = current->audit_context;
  2093. struct cpu_vfs_cap_data vcaps;
  2094. struct dentry *dentry;
  2095. ax = kmalloc(sizeof(*ax), GFP_KERNEL);
  2096. if (!ax)
  2097. return -ENOMEM;
  2098. ax->d.type = AUDIT_BPRM_FCAPS;
  2099. ax->d.next = context->aux;
  2100. context->aux = (void *)ax;
  2101. dentry = dget(bprm->file->f_dentry);
  2102. get_vfs_caps_from_disk(dentry, &vcaps);
  2103. dput(dentry);
  2104. ax->fcap.permitted = vcaps.permitted;
  2105. ax->fcap.inheritable = vcaps.inheritable;
  2106. ax->fcap.fE = !!(vcaps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
  2107. ax->fcap_ver = (vcaps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
  2108. ax->old_pcap.permitted = old->cap_permitted;
  2109. ax->old_pcap.inheritable = old->cap_inheritable;
  2110. ax->old_pcap.effective = old->cap_effective;
  2111. ax->new_pcap.permitted = new->cap_permitted;
  2112. ax->new_pcap.inheritable = new->cap_inheritable;
  2113. ax->new_pcap.effective = new->cap_effective;
  2114. return 0;
  2115. }
  2116. /**
  2117. * __audit_log_capset - store information about the arguments to the capset syscall
  2118. * @pid: target pid of the capset call
  2119. * @new: the new credentials
  2120. * @old: the old (current) credentials
  2121. *
  2122. * Record the aguments userspace sent to sys_capset for later printing by the
  2123. * audit system if applicable
  2124. */
  2125. void __audit_log_capset(pid_t pid,
  2126. const struct cred *new, const struct cred *old)
  2127. {
  2128. struct audit_context *context = current->audit_context;
  2129. context->capset.pid = pid;
  2130. context->capset.cap.effective = new->cap_effective;
  2131. context->capset.cap.inheritable = new->cap_effective;
  2132. context->capset.cap.permitted = new->cap_permitted;
  2133. context->type = AUDIT_CAPSET;
  2134. }
  2135. void __audit_mmap_fd(int fd, int flags)
  2136. {
  2137. struct audit_context *context = current->audit_context;
  2138. context->mmap.fd = fd;
  2139. context->mmap.flags = flags;
  2140. context->type = AUDIT_MMAP;
  2141. }
  2142. static void audit_log_task(struct audit_buffer *ab)
  2143. {
  2144. kuid_t auid, uid;
  2145. kgid_t gid;
  2146. unsigned int sessionid;
  2147. auid = audit_get_loginuid(current);
  2148. sessionid = audit_get_sessionid(current);
  2149. current_uid_gid(&uid, &gid);
  2150. audit_log_format(ab, "auid=%u uid=%u gid=%u ses=%u",
  2151. from_kuid(&init_user_ns, auid),
  2152. from_kuid(&init_user_ns, uid),
  2153. from_kgid(&init_user_ns, gid),
  2154. sessionid);
  2155. audit_log_task_context(ab);
  2156. audit_log_format(ab, " pid=%d comm=", current->pid);
  2157. audit_log_untrustedstring(ab, current->comm);
  2158. }
  2159. static void audit_log_abend(struct audit_buffer *ab, char *reason, long signr)
  2160. {
  2161. audit_log_task(ab);
  2162. audit_log_format(ab, " reason=");
  2163. audit_log_string(ab, reason);
  2164. audit_log_format(ab, " sig=%ld", signr);
  2165. }
  2166. /**
  2167. * audit_core_dumps - record information about processes that end abnormally
  2168. * @signr: signal value
  2169. *
  2170. * If a process ends with a core dump, something fishy is going on and we
  2171. * should record the event for investigation.
  2172. */
  2173. void audit_core_dumps(long signr)
  2174. {
  2175. struct audit_buffer *ab;
  2176. if (!audit_enabled)
  2177. return;
  2178. if (signr == SIGQUIT) /* don't care for those */
  2179. return;
  2180. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND);
  2181. if (unlikely(!ab))
  2182. return;
  2183. audit_log_abend(ab, "memory violation", signr);
  2184. audit_log_end(ab);
  2185. }
  2186. void __audit_seccomp(unsigned long syscall, long signr, int code)
  2187. {
  2188. struct audit_buffer *ab;
  2189. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_SECCOMP);
  2190. if (unlikely(!ab))
  2191. return;
  2192. audit_log_task(ab);
  2193. audit_log_format(ab, " sig=%ld", signr);
  2194. audit_log_format(ab, " syscall=%ld", syscall);
  2195. audit_log_format(ab, " compat=%d", is_compat_task());
  2196. audit_log_format(ab, " ip=0x%lx", KSTK_EIP(current));
  2197. audit_log_format(ab, " code=0x%x", code);
  2198. audit_log_end(ab);
  2199. }
  2200. struct list_head *audit_killed_trees(void)
  2201. {
  2202. struct audit_context *ctx = current->audit_context;
  2203. if (likely(!ctx || !ctx->in_syscall))
  2204. return NULL;
  2205. return &ctx->killed_trees;
  2206. }