auditsc.c 67 KB

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