auditsc.c 66 KB

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