auditsc.c 68 KB

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