auditsc.c 73 KB

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