auditsc.c 67 KB

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