auditsc.c 68 KB

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