auditsc.c 62 KB

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