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/binfmts.h>
  64. #include <linux/highmem.h>
  65. #include <linux/syscalls.h>
  66. #include <linux/inotify.h>
  67. #include "audit.h"
  68. extern struct list_head audit_filter_list[];
  69. extern int audit_ever_enabled;
  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 path pwd;
  187. struct audit_context *previous; /* For nested syscalls */
  188. struct audit_aux_data *aux;
  189. struct audit_aux_data *aux_pids;
  190. /* Save things to print about task_struct */
  191. pid_t pid, ppid;
  192. uid_t uid, euid, suid, fsuid;
  193. gid_t gid, egid, sgid, fsgid;
  194. unsigned long personality;
  195. int arch;
  196. pid_t target_pid;
  197. uid_t target_auid;
  198. uid_t target_uid;
  199. unsigned int target_sessionid;
  200. u32 target_sid;
  201. char target_comm[TASK_COMM_LEN];
  202. struct audit_tree_refs *trees, *first_trees;
  203. int tree_count;
  204. #if AUDIT_DEBUG
  205. int put_count;
  206. int ino_count;
  207. #endif
  208. };
  209. #define ACC_MODE(x) ("\004\002\006\006"[(x)&O_ACCMODE])
  210. static inline int open_arg(int flags, int mask)
  211. {
  212. int n = ACC_MODE(flags);
  213. if (flags & (O_TRUNC | O_CREAT))
  214. n |= AUDIT_PERM_WRITE;
  215. return n & mask;
  216. }
  217. static int audit_match_perm(struct audit_context *ctx, int mask)
  218. {
  219. unsigned n = ctx->major;
  220. switch (audit_classify_syscall(ctx->arch, n)) {
  221. case 0: /* native */
  222. if ((mask & AUDIT_PERM_WRITE) &&
  223. audit_match_class(AUDIT_CLASS_WRITE, n))
  224. return 1;
  225. if ((mask & AUDIT_PERM_READ) &&
  226. audit_match_class(AUDIT_CLASS_READ, n))
  227. return 1;
  228. if ((mask & AUDIT_PERM_ATTR) &&
  229. audit_match_class(AUDIT_CLASS_CHATTR, n))
  230. return 1;
  231. return 0;
  232. case 1: /* 32bit on biarch */
  233. if ((mask & AUDIT_PERM_WRITE) &&
  234. audit_match_class(AUDIT_CLASS_WRITE_32, n))
  235. return 1;
  236. if ((mask & AUDIT_PERM_READ) &&
  237. audit_match_class(AUDIT_CLASS_READ_32, n))
  238. return 1;
  239. if ((mask & AUDIT_PERM_ATTR) &&
  240. audit_match_class(AUDIT_CLASS_CHATTR_32, n))
  241. return 1;
  242. return 0;
  243. case 2: /* open */
  244. return mask & ACC_MODE(ctx->argv[1]);
  245. case 3: /* openat */
  246. return mask & ACC_MODE(ctx->argv[2]);
  247. case 4: /* socketcall */
  248. return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND);
  249. case 5: /* execve */
  250. return mask & AUDIT_PERM_EXEC;
  251. default:
  252. return 0;
  253. }
  254. }
  255. /*
  256. * We keep a linked list of fixed-sized (31 pointer) arrays of audit_chunk *;
  257. * ->first_trees points to its beginning, ->trees - to the current end of data.
  258. * ->tree_count is the number of free entries in array pointed to by ->trees.
  259. * Original condition is (NULL, NULL, 0); as soon as it grows we never revert to NULL,
  260. * "empty" becomes (p, p, 31) afterwards. We don't shrink the list (and seriously,
  261. * it's going to remain 1-element for almost any setup) until we free context itself.
  262. * References in it _are_ dropped - at the same time we free/drop aux stuff.
  263. */
  264. #ifdef CONFIG_AUDIT_TREE
  265. static int put_tree_ref(struct audit_context *ctx, struct audit_chunk *chunk)
  266. {
  267. struct audit_tree_refs *p = ctx->trees;
  268. int left = ctx->tree_count;
  269. if (likely(left)) {
  270. p->c[--left] = chunk;
  271. ctx->tree_count = left;
  272. return 1;
  273. }
  274. if (!p)
  275. return 0;
  276. p = p->next;
  277. if (p) {
  278. p->c[30] = chunk;
  279. ctx->trees = p;
  280. ctx->tree_count = 30;
  281. return 1;
  282. }
  283. return 0;
  284. }
  285. static int grow_tree_refs(struct audit_context *ctx)
  286. {
  287. struct audit_tree_refs *p = ctx->trees;
  288. ctx->trees = kzalloc(sizeof(struct audit_tree_refs), GFP_KERNEL);
  289. if (!ctx->trees) {
  290. ctx->trees = p;
  291. return 0;
  292. }
  293. if (p)
  294. p->next = ctx->trees;
  295. else
  296. ctx->first_trees = ctx->trees;
  297. ctx->tree_count = 31;
  298. return 1;
  299. }
  300. #endif
  301. static void unroll_tree_refs(struct audit_context *ctx,
  302. struct audit_tree_refs *p, int count)
  303. {
  304. #ifdef CONFIG_AUDIT_TREE
  305. struct audit_tree_refs *q;
  306. int n;
  307. if (!p) {
  308. /* we started with empty chain */
  309. p = ctx->first_trees;
  310. count = 31;
  311. /* if the very first allocation has failed, nothing to do */
  312. if (!p)
  313. return;
  314. }
  315. n = count;
  316. for (q = p; q != ctx->trees; q = q->next, n = 31) {
  317. while (n--) {
  318. audit_put_chunk(q->c[n]);
  319. q->c[n] = NULL;
  320. }
  321. }
  322. while (n-- > ctx->tree_count) {
  323. audit_put_chunk(q->c[n]);
  324. q->c[n] = NULL;
  325. }
  326. ctx->trees = p;
  327. ctx->tree_count = count;
  328. #endif
  329. }
  330. static void free_tree_refs(struct audit_context *ctx)
  331. {
  332. struct audit_tree_refs *p, *q;
  333. for (p = ctx->first_trees; p; p = q) {
  334. q = p->next;
  335. kfree(p);
  336. }
  337. }
  338. static int match_tree_refs(struct audit_context *ctx, struct audit_tree *tree)
  339. {
  340. #ifdef CONFIG_AUDIT_TREE
  341. struct audit_tree_refs *p;
  342. int n;
  343. if (!tree)
  344. return 0;
  345. /* full ones */
  346. for (p = ctx->first_trees; p != ctx->trees; p = p->next) {
  347. for (n = 0; n < 31; n++)
  348. if (audit_tree_match(p->c[n], tree))
  349. return 1;
  350. }
  351. /* partial */
  352. if (p) {
  353. for (n = ctx->tree_count; n < 31; n++)
  354. if (audit_tree_match(p->c[n], tree))
  355. return 1;
  356. }
  357. #endif
  358. return 0;
  359. }
  360. /* Determine if any context name data matches a rule's watch data */
  361. /* Compare a task_struct with an audit_rule. Return 1 on match, 0
  362. * otherwise. */
  363. static int audit_filter_rules(struct task_struct *tsk,
  364. struct audit_krule *rule,
  365. struct audit_context *ctx,
  366. struct audit_names *name,
  367. enum audit_state *state)
  368. {
  369. int i, j, need_sid = 1;
  370. u32 sid;
  371. for (i = 0; i < rule->field_count; i++) {
  372. struct audit_field *f = &rule->fields[i];
  373. int result = 0;
  374. switch (f->type) {
  375. case AUDIT_PID:
  376. result = audit_comparator(tsk->pid, f->op, f->val);
  377. break;
  378. case AUDIT_PPID:
  379. if (ctx) {
  380. if (!ctx->ppid)
  381. ctx->ppid = sys_getppid();
  382. result = audit_comparator(ctx->ppid, f->op, f->val);
  383. }
  384. break;
  385. case AUDIT_UID:
  386. result = audit_comparator(tsk->uid, f->op, f->val);
  387. break;
  388. case AUDIT_EUID:
  389. result = audit_comparator(tsk->euid, f->op, f->val);
  390. break;
  391. case AUDIT_SUID:
  392. result = audit_comparator(tsk->suid, f->op, f->val);
  393. break;
  394. case AUDIT_FSUID:
  395. result = audit_comparator(tsk->fsuid, f->op, f->val);
  396. break;
  397. case AUDIT_GID:
  398. result = audit_comparator(tsk->gid, f->op, f->val);
  399. break;
  400. case AUDIT_EGID:
  401. result = audit_comparator(tsk->egid, f->op, f->val);
  402. break;
  403. case AUDIT_SGID:
  404. result = audit_comparator(tsk->sgid, f->op, f->val);
  405. break;
  406. case AUDIT_FSGID:
  407. result = audit_comparator(tsk->fsgid, f->op, f->val);
  408. break;
  409. case AUDIT_PERS:
  410. result = audit_comparator(tsk->personality, f->op, f->val);
  411. break;
  412. case AUDIT_ARCH:
  413. if (ctx)
  414. result = audit_comparator(ctx->arch, f->op, f->val);
  415. break;
  416. case AUDIT_EXIT:
  417. if (ctx && ctx->return_valid)
  418. result = audit_comparator(ctx->return_code, f->op, f->val);
  419. break;
  420. case AUDIT_SUCCESS:
  421. if (ctx && ctx->return_valid) {
  422. if (f->val)
  423. result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
  424. else
  425. result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
  426. }
  427. break;
  428. case AUDIT_DEVMAJOR:
  429. if (name)
  430. result = audit_comparator(MAJOR(name->dev),
  431. f->op, f->val);
  432. else if (ctx) {
  433. for (j = 0; j < ctx->name_count; j++) {
  434. if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
  435. ++result;
  436. break;
  437. }
  438. }
  439. }
  440. break;
  441. case AUDIT_DEVMINOR:
  442. if (name)
  443. result = audit_comparator(MINOR(name->dev),
  444. f->op, f->val);
  445. else if (ctx) {
  446. for (j = 0; j < ctx->name_count; j++) {
  447. if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
  448. ++result;
  449. break;
  450. }
  451. }
  452. }
  453. break;
  454. case AUDIT_INODE:
  455. if (name)
  456. result = (name->ino == f->val);
  457. else if (ctx) {
  458. for (j = 0; j < ctx->name_count; j++) {
  459. if (audit_comparator(ctx->names[j].ino, f->op, f->val)) {
  460. ++result;
  461. break;
  462. }
  463. }
  464. }
  465. break;
  466. case AUDIT_WATCH:
  467. if (name && rule->watch->ino != (unsigned long)-1)
  468. result = (name->dev == rule->watch->dev &&
  469. name->ino == rule->watch->ino);
  470. break;
  471. case AUDIT_DIR:
  472. if (ctx)
  473. result = match_tree_refs(ctx, rule->tree);
  474. break;
  475. case AUDIT_LOGINUID:
  476. result = 0;
  477. if (ctx)
  478. result = audit_comparator(tsk->loginuid, f->op, f->val);
  479. break;
  480. case AUDIT_SUBJ_USER:
  481. case AUDIT_SUBJ_ROLE:
  482. case AUDIT_SUBJ_TYPE:
  483. case AUDIT_SUBJ_SEN:
  484. case AUDIT_SUBJ_CLR:
  485. /* NOTE: this may return negative values indicating
  486. a temporary error. We simply treat this as a
  487. match for now to avoid losing information that
  488. may be wanted. An error message will also be
  489. logged upon error */
  490. if (f->lsm_rule) {
  491. if (need_sid) {
  492. security_task_getsecid(tsk, &sid);
  493. need_sid = 0;
  494. }
  495. result = security_audit_rule_match(sid, f->type,
  496. f->op,
  497. f->lsm_rule,
  498. ctx);
  499. }
  500. break;
  501. case AUDIT_OBJ_USER:
  502. case AUDIT_OBJ_ROLE:
  503. case AUDIT_OBJ_TYPE:
  504. case AUDIT_OBJ_LEV_LOW:
  505. case AUDIT_OBJ_LEV_HIGH:
  506. /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
  507. also applies here */
  508. if (f->lsm_rule) {
  509. /* Find files that match */
  510. if (name) {
  511. result = security_audit_rule_match(
  512. name->osid, f->type, f->op,
  513. f->lsm_rule, ctx);
  514. } else if (ctx) {
  515. for (j = 0; j < ctx->name_count; j++) {
  516. if (security_audit_rule_match(
  517. ctx->names[j].osid,
  518. f->type, f->op,
  519. f->lsm_rule, ctx)) {
  520. ++result;
  521. break;
  522. }
  523. }
  524. }
  525. /* Find ipc objects that match */
  526. if (ctx) {
  527. struct audit_aux_data *aux;
  528. for (aux = ctx->aux; aux;
  529. aux = aux->next) {
  530. if (aux->type == AUDIT_IPC) {
  531. struct audit_aux_data_ipcctl *axi = (void *)aux;
  532. if (security_audit_rule_match(axi->osid, f->type, f->op, f->lsm_rule, ctx)) {
  533. ++result;
  534. break;
  535. }
  536. }
  537. }
  538. }
  539. }
  540. break;
  541. case AUDIT_ARG0:
  542. case AUDIT_ARG1:
  543. case AUDIT_ARG2:
  544. case AUDIT_ARG3:
  545. if (ctx)
  546. result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
  547. break;
  548. case AUDIT_FILTERKEY:
  549. /* ignore this field for filtering */
  550. result = 1;
  551. break;
  552. case AUDIT_PERM:
  553. result = audit_match_perm(ctx, f->val);
  554. break;
  555. }
  556. if (!result)
  557. return 0;
  558. }
  559. if (rule->filterkey)
  560. ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
  561. switch (rule->action) {
  562. case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
  563. case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
  564. }
  565. return 1;
  566. }
  567. /* At process creation time, we can determine if system-call auditing is
  568. * completely disabled for this task. Since we only have the task
  569. * structure at this point, we can only check uid and gid.
  570. */
  571. static enum audit_state audit_filter_task(struct task_struct *tsk)
  572. {
  573. struct audit_entry *e;
  574. enum audit_state state;
  575. rcu_read_lock();
  576. list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
  577. if (audit_filter_rules(tsk, &e->rule, NULL, NULL, &state)) {
  578. rcu_read_unlock();
  579. return state;
  580. }
  581. }
  582. rcu_read_unlock();
  583. return AUDIT_BUILD_CONTEXT;
  584. }
  585. /* At syscall entry and exit time, this filter is called if the
  586. * audit_state is not low enough that auditing cannot take place, but is
  587. * also not high enough that we already know we have to write an audit
  588. * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
  589. */
  590. static enum audit_state audit_filter_syscall(struct task_struct *tsk,
  591. struct audit_context *ctx,
  592. struct list_head *list)
  593. {
  594. struct audit_entry *e;
  595. enum audit_state state;
  596. if (audit_pid && tsk->tgid == audit_pid)
  597. return AUDIT_DISABLED;
  598. rcu_read_lock();
  599. if (!list_empty(list)) {
  600. int word = AUDIT_WORD(ctx->major);
  601. int bit = AUDIT_BIT(ctx->major);
  602. list_for_each_entry_rcu(e, list, list) {
  603. if ((e->rule.mask[word] & bit) == bit &&
  604. audit_filter_rules(tsk, &e->rule, ctx, NULL,
  605. &state)) {
  606. rcu_read_unlock();
  607. return state;
  608. }
  609. }
  610. }
  611. rcu_read_unlock();
  612. return AUDIT_BUILD_CONTEXT;
  613. }
  614. /* At syscall exit time, this filter is called if any audit_names[] have been
  615. * collected during syscall processing. We only check rules in sublists at hash
  616. * buckets applicable to the inode numbers in audit_names[].
  617. * Regarding audit_state, same rules apply as for audit_filter_syscall().
  618. */
  619. enum audit_state audit_filter_inodes(struct task_struct *tsk,
  620. struct audit_context *ctx)
  621. {
  622. int i;
  623. struct audit_entry *e;
  624. enum audit_state state;
  625. if (audit_pid && tsk->tgid == audit_pid)
  626. return AUDIT_DISABLED;
  627. rcu_read_lock();
  628. for (i = 0; i < ctx->name_count; i++) {
  629. int word = AUDIT_WORD(ctx->major);
  630. int bit = AUDIT_BIT(ctx->major);
  631. struct audit_names *n = &ctx->names[i];
  632. int h = audit_hash_ino((u32)n->ino);
  633. struct list_head *list = &audit_inode_hash[h];
  634. if (list_empty(list))
  635. continue;
  636. list_for_each_entry_rcu(e, list, list) {
  637. if ((e->rule.mask[word] & bit) == bit &&
  638. audit_filter_rules(tsk, &e->rule, ctx, n, &state)) {
  639. rcu_read_unlock();
  640. return state;
  641. }
  642. }
  643. }
  644. rcu_read_unlock();
  645. return AUDIT_BUILD_CONTEXT;
  646. }
  647. void audit_set_auditable(struct audit_context *ctx)
  648. {
  649. ctx->auditable = 1;
  650. }
  651. static inline struct audit_context *audit_get_context(struct task_struct *tsk,
  652. int return_valid,
  653. int return_code)
  654. {
  655. struct audit_context *context = tsk->audit_context;
  656. if (likely(!context))
  657. return NULL;
  658. context->return_valid = return_valid;
  659. /*
  660. * we need to fix up the return code in the audit logs if the actual
  661. * return codes are later going to be fixed up by the arch specific
  662. * signal handlers
  663. *
  664. * This is actually a test for:
  665. * (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) ||
  666. * (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK)
  667. *
  668. * but is faster than a bunch of ||
  669. */
  670. if (unlikely(return_code <= -ERESTARTSYS) &&
  671. (return_code >= -ERESTART_RESTARTBLOCK) &&
  672. (return_code != -ENOIOCTLCMD))
  673. context->return_code = -EINTR;
  674. else
  675. context->return_code = return_code;
  676. if (context->in_syscall && !context->dummy && !context->auditable) {
  677. enum audit_state state;
  678. state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
  679. if (state == AUDIT_RECORD_CONTEXT) {
  680. context->auditable = 1;
  681. goto get_context;
  682. }
  683. state = audit_filter_inodes(tsk, context);
  684. if (state == AUDIT_RECORD_CONTEXT)
  685. context->auditable = 1;
  686. }
  687. get_context:
  688. tsk->audit_context = NULL;
  689. return context;
  690. }
  691. static inline void audit_free_names(struct audit_context *context)
  692. {
  693. int i;
  694. #if AUDIT_DEBUG == 2
  695. if (context->auditable
  696. ||context->put_count + context->ino_count != context->name_count) {
  697. printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
  698. " name_count=%d put_count=%d"
  699. " ino_count=%d [NOT freeing]\n",
  700. __FILE__, __LINE__,
  701. context->serial, context->major, context->in_syscall,
  702. context->name_count, context->put_count,
  703. context->ino_count);
  704. for (i = 0; i < context->name_count; i++) {
  705. printk(KERN_ERR "names[%d] = %p = %s\n", i,
  706. context->names[i].name,
  707. context->names[i].name ?: "(null)");
  708. }
  709. dump_stack();
  710. return;
  711. }
  712. #endif
  713. #if AUDIT_DEBUG
  714. context->put_count = 0;
  715. context->ino_count = 0;
  716. #endif
  717. for (i = 0; i < context->name_count; i++) {
  718. if (context->names[i].name && context->names[i].name_put)
  719. __putname(context->names[i].name);
  720. }
  721. context->name_count = 0;
  722. path_put(&context->pwd);
  723. context->pwd.dentry = NULL;
  724. context->pwd.mnt = NULL;
  725. }
  726. static inline void audit_free_aux(struct audit_context *context)
  727. {
  728. struct audit_aux_data *aux;
  729. while ((aux = context->aux)) {
  730. context->aux = aux->next;
  731. kfree(aux);
  732. }
  733. while ((aux = context->aux_pids)) {
  734. context->aux_pids = aux->next;
  735. kfree(aux);
  736. }
  737. }
  738. static inline void audit_zero_context(struct audit_context *context,
  739. enum audit_state state)
  740. {
  741. memset(context, 0, sizeof(*context));
  742. context->state = state;
  743. }
  744. static inline struct audit_context *audit_alloc_context(enum audit_state state)
  745. {
  746. struct audit_context *context;
  747. if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
  748. return NULL;
  749. audit_zero_context(context, state);
  750. return context;
  751. }
  752. /**
  753. * audit_alloc - allocate an audit context block for a task
  754. * @tsk: task
  755. *
  756. * Filter on the task information and allocate a per-task audit context
  757. * if necessary. Doing so turns on system call auditing for the
  758. * specified task. This is called from copy_process, so no lock is
  759. * needed.
  760. */
  761. int audit_alloc(struct task_struct *tsk)
  762. {
  763. struct audit_context *context;
  764. enum audit_state state;
  765. if (likely(!audit_ever_enabled))
  766. return 0; /* Return if not auditing. */
  767. state = audit_filter_task(tsk);
  768. if (likely(state == AUDIT_DISABLED))
  769. return 0;
  770. if (!(context = audit_alloc_context(state))) {
  771. audit_log_lost("out of memory in audit_alloc");
  772. return -ENOMEM;
  773. }
  774. tsk->audit_context = context;
  775. set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
  776. return 0;
  777. }
  778. static inline void audit_free_context(struct audit_context *context)
  779. {
  780. struct audit_context *previous;
  781. int count = 0;
  782. do {
  783. previous = context->previous;
  784. if (previous || (count && count < 10)) {
  785. ++count;
  786. printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
  787. " freeing multiple contexts (%d)\n",
  788. context->serial, context->major,
  789. context->name_count, count);
  790. }
  791. audit_free_names(context);
  792. unroll_tree_refs(context, NULL, 0);
  793. free_tree_refs(context);
  794. audit_free_aux(context);
  795. kfree(context->filterkey);
  796. kfree(context);
  797. context = previous;
  798. } while (context);
  799. if (count >= 10)
  800. printk(KERN_ERR "audit: freed %d contexts\n", count);
  801. }
  802. void audit_log_task_context(struct audit_buffer *ab)
  803. {
  804. char *ctx = NULL;
  805. unsigned len;
  806. int error;
  807. u32 sid;
  808. security_task_getsecid(current, &sid);
  809. if (!sid)
  810. return;
  811. error = security_secid_to_secctx(sid, &ctx, &len);
  812. if (error) {
  813. if (error != -EINVAL)
  814. goto error_path;
  815. return;
  816. }
  817. audit_log_format(ab, " subj=%s", ctx);
  818. security_release_secctx(ctx, len);
  819. return;
  820. error_path:
  821. audit_panic("error in audit_log_task_context");
  822. return;
  823. }
  824. EXPORT_SYMBOL(audit_log_task_context);
  825. static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
  826. {
  827. char name[sizeof(tsk->comm)];
  828. struct mm_struct *mm = tsk->mm;
  829. struct vm_area_struct *vma;
  830. /* tsk == current */
  831. get_task_comm(name, tsk);
  832. audit_log_format(ab, " comm=");
  833. audit_log_untrustedstring(ab, name);
  834. if (mm) {
  835. down_read(&mm->mmap_sem);
  836. vma = mm->mmap;
  837. while (vma) {
  838. if ((vma->vm_flags & VM_EXECUTABLE) &&
  839. vma->vm_file) {
  840. audit_log_d_path(ab, "exe=",
  841. &vma->vm_file->f_path);
  842. break;
  843. }
  844. vma = vma->vm_next;
  845. }
  846. up_read(&mm->mmap_sem);
  847. }
  848. audit_log_task_context(ab);
  849. }
  850. static int audit_log_pid_context(struct audit_context *context, pid_t pid,
  851. uid_t auid, uid_t uid, unsigned int sessionid,
  852. u32 sid, char *comm)
  853. {
  854. struct audit_buffer *ab;
  855. char *ctx = NULL;
  856. u32 len;
  857. int rc = 0;
  858. ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
  859. if (!ab)
  860. return rc;
  861. audit_log_format(ab, "opid=%d oauid=%d ouid=%d oses=%d", pid, auid,
  862. uid, sessionid);
  863. if (security_secid_to_secctx(sid, &ctx, &len)) {
  864. audit_log_format(ab, " obj=(none)");
  865. rc = 1;
  866. } else {
  867. audit_log_format(ab, " obj=%s", ctx);
  868. security_release_secctx(ctx, len);
  869. }
  870. audit_log_format(ab, " ocomm=");
  871. audit_log_untrustedstring(ab, comm);
  872. audit_log_end(ab);
  873. return rc;
  874. }
  875. /*
  876. * to_send and len_sent accounting are very loose estimates. We aren't
  877. * really worried about a hard cap to MAX_EXECVE_AUDIT_LEN so much as being
  878. * within about 500 bytes (next page boundry)
  879. *
  880. * why snprintf? an int is up to 12 digits long. if we just assumed when
  881. * logging that a[%d]= was going to be 16 characters long we would be wasting
  882. * space in every audit message. In one 7500 byte message we can log up to
  883. * about 1000 min size arguments. That comes down to about 50% waste of space
  884. * if we didn't do the snprintf to find out how long arg_num_len was.
  885. */
  886. static int audit_log_single_execve_arg(struct audit_context *context,
  887. struct audit_buffer **ab,
  888. int arg_num,
  889. size_t *len_sent,
  890. const char __user *p,
  891. char *buf)
  892. {
  893. char arg_num_len_buf[12];
  894. const char __user *tmp_p = p;
  895. /* how many digits are in arg_num? 3 is the length of a=\n */
  896. size_t arg_num_len = snprintf(arg_num_len_buf, 12, "%d", arg_num) + 3;
  897. size_t len, len_left, to_send;
  898. size_t max_execve_audit_len = MAX_EXECVE_AUDIT_LEN;
  899. unsigned int i, has_cntl = 0, too_long = 0;
  900. int ret;
  901. /* strnlen_user includes the null we don't want to send */
  902. len_left = len = strnlen_user(p, MAX_ARG_STRLEN) - 1;
  903. /*
  904. * We just created this mm, if we can't find the strings
  905. * we just copied into it something is _very_ wrong. Similar
  906. * for strings that are too long, we should not have created
  907. * any.
  908. */
  909. if (unlikely((len == -1) || len > MAX_ARG_STRLEN - 1)) {
  910. WARN_ON(1);
  911. send_sig(SIGKILL, current, 0);
  912. return -1;
  913. }
  914. /* walk the whole argument looking for non-ascii chars */
  915. do {
  916. if (len_left > MAX_EXECVE_AUDIT_LEN)
  917. to_send = MAX_EXECVE_AUDIT_LEN;
  918. else
  919. to_send = len_left;
  920. ret = copy_from_user(buf, tmp_p, to_send);
  921. /*
  922. * There is no reason for this copy to be short. We just
  923. * copied them here, and the mm hasn't been exposed to user-
  924. * space yet.
  925. */
  926. if (ret) {
  927. WARN_ON(1);
  928. send_sig(SIGKILL, current, 0);
  929. return -1;
  930. }
  931. buf[to_send] = '\0';
  932. has_cntl = audit_string_contains_control(buf, to_send);
  933. if (has_cntl) {
  934. /*
  935. * hex messages get logged as 2 bytes, so we can only
  936. * send half as much in each message
  937. */
  938. max_execve_audit_len = MAX_EXECVE_AUDIT_LEN / 2;
  939. break;
  940. }
  941. len_left -= to_send;
  942. tmp_p += to_send;
  943. } while (len_left > 0);
  944. len_left = len;
  945. if (len > max_execve_audit_len)
  946. too_long = 1;
  947. /* rewalk the argument actually logging the message */
  948. for (i = 0; len_left > 0; i++) {
  949. int room_left;
  950. if (len_left > max_execve_audit_len)
  951. to_send = max_execve_audit_len;
  952. else
  953. to_send = len_left;
  954. /* do we have space left to send this argument in this ab? */
  955. room_left = MAX_EXECVE_AUDIT_LEN - arg_num_len - *len_sent;
  956. if (has_cntl)
  957. room_left -= (to_send * 2);
  958. else
  959. room_left -= to_send;
  960. if (room_left < 0) {
  961. *len_sent = 0;
  962. audit_log_end(*ab);
  963. *ab = audit_log_start(context, GFP_KERNEL, AUDIT_EXECVE);
  964. if (!*ab)
  965. return 0;
  966. }
  967. /*
  968. * first record needs to say how long the original string was
  969. * so we can be sure nothing was lost.
  970. */
  971. if ((i == 0) && (too_long))
  972. audit_log_format(*ab, "a%d_len=%zu ", arg_num,
  973. has_cntl ? 2*len : len);
  974. /*
  975. * normally arguments are small enough to fit and we already
  976. * filled buf above when we checked for control characters
  977. * so don't bother with another copy_from_user
  978. */
  979. if (len >= max_execve_audit_len)
  980. ret = copy_from_user(buf, p, to_send);
  981. else
  982. ret = 0;
  983. if (ret) {
  984. WARN_ON(1);
  985. send_sig(SIGKILL, current, 0);
  986. return -1;
  987. }
  988. buf[to_send] = '\0';
  989. /* actually log it */
  990. audit_log_format(*ab, "a%d", arg_num);
  991. if (too_long)
  992. audit_log_format(*ab, "[%d]", i);
  993. audit_log_format(*ab, "=");
  994. if (has_cntl)
  995. audit_log_hex(*ab, buf, to_send);
  996. else
  997. audit_log_format(*ab, "\"%s\"", buf);
  998. audit_log_format(*ab, "\n");
  999. p += to_send;
  1000. len_left -= to_send;
  1001. *len_sent += arg_num_len;
  1002. if (has_cntl)
  1003. *len_sent += to_send * 2;
  1004. else
  1005. *len_sent += to_send;
  1006. }
  1007. /* include the null we didn't log */
  1008. return len + 1;
  1009. }
  1010. static void audit_log_execve_info(struct audit_context *context,
  1011. struct audit_buffer **ab,
  1012. struct audit_aux_data_execve *axi)
  1013. {
  1014. int i;
  1015. size_t len, len_sent = 0;
  1016. const char __user *p;
  1017. char *buf;
  1018. if (axi->mm != current->mm)
  1019. return; /* execve failed, no additional info */
  1020. p = (const char __user *)axi->mm->arg_start;
  1021. audit_log_format(*ab, "argc=%d ", axi->argc);
  1022. /*
  1023. * we need some kernel buffer to hold the userspace args. Just
  1024. * allocate one big one rather than allocating one of the right size
  1025. * for every single argument inside audit_log_single_execve_arg()
  1026. * should be <8k allocation so should be pretty safe.
  1027. */
  1028. buf = kmalloc(MAX_EXECVE_AUDIT_LEN + 1, GFP_KERNEL);
  1029. if (!buf) {
  1030. audit_panic("out of memory for argv string\n");
  1031. return;
  1032. }
  1033. for (i = 0; i < axi->argc; i++) {
  1034. len = audit_log_single_execve_arg(context, ab, i,
  1035. &len_sent, p, buf);
  1036. if (len <= 0)
  1037. break;
  1038. p += len;
  1039. }
  1040. kfree(buf);
  1041. }
  1042. static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
  1043. {
  1044. int i, call_panic = 0;
  1045. struct audit_buffer *ab;
  1046. struct audit_aux_data *aux;
  1047. const char *tty;
  1048. /* tsk == current */
  1049. context->pid = tsk->pid;
  1050. if (!context->ppid)
  1051. context->ppid = sys_getppid();
  1052. context->uid = tsk->uid;
  1053. context->gid = tsk->gid;
  1054. context->euid = tsk->euid;
  1055. context->suid = tsk->suid;
  1056. context->fsuid = tsk->fsuid;
  1057. context->egid = tsk->egid;
  1058. context->sgid = tsk->sgid;
  1059. context->fsgid = tsk->fsgid;
  1060. context->personality = tsk->personality;
  1061. ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
  1062. if (!ab)
  1063. return; /* audit_panic has been called */
  1064. audit_log_format(ab, "arch=%x syscall=%d",
  1065. context->arch, context->major);
  1066. if (context->personality != PER_LINUX)
  1067. audit_log_format(ab, " per=%lx", context->personality);
  1068. if (context->return_valid)
  1069. audit_log_format(ab, " success=%s exit=%ld",
  1070. (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
  1071. context->return_code);
  1072. mutex_lock(&tty_mutex);
  1073. read_lock(&tasklist_lock);
  1074. if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
  1075. tty = tsk->signal->tty->name;
  1076. else
  1077. tty = "(none)";
  1078. read_unlock(&tasklist_lock);
  1079. audit_log_format(ab,
  1080. " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
  1081. " ppid=%d pid=%d auid=%u uid=%u gid=%u"
  1082. " euid=%u suid=%u fsuid=%u"
  1083. " egid=%u sgid=%u fsgid=%u tty=%s ses=%u",
  1084. context->argv[0],
  1085. context->argv[1],
  1086. context->argv[2],
  1087. context->argv[3],
  1088. context->name_count,
  1089. context->ppid,
  1090. context->pid,
  1091. tsk->loginuid,
  1092. context->uid,
  1093. context->gid,
  1094. context->euid, context->suid, context->fsuid,
  1095. context->egid, context->sgid, context->fsgid, tty,
  1096. tsk->sessionid);
  1097. mutex_unlock(&tty_mutex);
  1098. audit_log_task_info(ab, tsk);
  1099. if (context->filterkey) {
  1100. audit_log_format(ab, " key=");
  1101. audit_log_untrustedstring(ab, context->filterkey);
  1102. } else
  1103. audit_log_format(ab, " key=(null)");
  1104. audit_log_end(ab);
  1105. for (aux = context->aux; aux; aux = aux->next) {
  1106. ab = audit_log_start(context, GFP_KERNEL, aux->type);
  1107. if (!ab)
  1108. continue; /* audit_panic has been called */
  1109. switch (aux->type) {
  1110. case AUDIT_MQ_OPEN: {
  1111. struct audit_aux_data_mq_open *axi = (void *)aux;
  1112. audit_log_format(ab,
  1113. "oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld "
  1114. "mq_msgsize=%ld mq_curmsgs=%ld",
  1115. axi->oflag, axi->mode, axi->attr.mq_flags,
  1116. axi->attr.mq_maxmsg, axi->attr.mq_msgsize,
  1117. axi->attr.mq_curmsgs);
  1118. break; }
  1119. case AUDIT_MQ_SENDRECV: {
  1120. struct audit_aux_data_mq_sendrecv *axi = (void *)aux;
  1121. audit_log_format(ab,
  1122. "mqdes=%d msg_len=%zd msg_prio=%u "
  1123. "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
  1124. axi->mqdes, axi->msg_len, axi->msg_prio,
  1125. axi->abs_timeout.tv_sec, axi->abs_timeout.tv_nsec);
  1126. break; }
  1127. case AUDIT_MQ_NOTIFY: {
  1128. struct audit_aux_data_mq_notify *axi = (void *)aux;
  1129. audit_log_format(ab,
  1130. "mqdes=%d sigev_signo=%d",
  1131. axi->mqdes,
  1132. axi->notification.sigev_signo);
  1133. break; }
  1134. case AUDIT_MQ_GETSETATTR: {
  1135. struct audit_aux_data_mq_getsetattr *axi = (void *)aux;
  1136. audit_log_format(ab,
  1137. "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
  1138. "mq_curmsgs=%ld ",
  1139. axi->mqdes,
  1140. axi->mqstat.mq_flags, axi->mqstat.mq_maxmsg,
  1141. axi->mqstat.mq_msgsize, axi->mqstat.mq_curmsgs);
  1142. break; }
  1143. case AUDIT_IPC: {
  1144. struct audit_aux_data_ipcctl *axi = (void *)aux;
  1145. audit_log_format(ab,
  1146. "ouid=%u ogid=%u mode=%#o",
  1147. axi->uid, axi->gid, axi->mode);
  1148. if (axi->osid != 0) {
  1149. char *ctx = NULL;
  1150. u32 len;
  1151. if (security_secid_to_secctx(
  1152. axi->osid, &ctx, &len)) {
  1153. audit_log_format(ab, " osid=%u",
  1154. axi->osid);
  1155. call_panic = 1;
  1156. } else {
  1157. audit_log_format(ab, " obj=%s", ctx);
  1158. security_release_secctx(ctx, len);
  1159. }
  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.dentry && context->pwd.mnt) {
  1210. ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
  1211. if (ab) {
  1212. audit_log_d_path(ab, "cwd=", &context->pwd);
  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. break;
  1234. default:
  1235. /* log the name's directory component */
  1236. audit_log_format(ab, " name=");
  1237. audit_log_n_untrustedstring(ab, n->name_len,
  1238. n->name);
  1239. }
  1240. } else
  1241. audit_log_format(ab, " name=(null)");
  1242. if (n->ino != (unsigned long)-1) {
  1243. audit_log_format(ab, " inode=%lu"
  1244. " dev=%02x:%02x mode=%#o"
  1245. " ouid=%u ogid=%u rdev=%02x:%02x",
  1246. n->ino,
  1247. MAJOR(n->dev),
  1248. MINOR(n->dev),
  1249. n->mode,
  1250. n->uid,
  1251. n->gid,
  1252. MAJOR(n->rdev),
  1253. MINOR(n->rdev));
  1254. }
  1255. if (n->osid != 0) {
  1256. char *ctx = NULL;
  1257. u32 len;
  1258. if (security_secid_to_secctx(
  1259. n->osid, &ctx, &len)) {
  1260. audit_log_format(ab, " osid=%u", n->osid);
  1261. call_panic = 2;
  1262. } else {
  1263. audit_log_format(ab, " obj=%s", ctx);
  1264. security_release_secctx(ctx, len);
  1265. }
  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.dentry) {
  1534. read_lock(&current->fs->lock);
  1535. context->pwd = current->fs->pwd;
  1536. path_get(&current->fs->pwd);
  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. security_inode_getsecid(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. security_ipc_getsecid(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. security_task_getsecid(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. security_task_getsecid(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. security_task_getsecid(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. security_task_getsecid(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. security_task_getsecid(current, &sid);
  2174. if (sid) {
  2175. char *ctx = NULL;
  2176. u32 len;
  2177. if (security_secid_to_secctx(sid, &ctx, &len))
  2178. audit_log_format(ab, " ssid=%u", sid);
  2179. else {
  2180. audit_log_format(ab, " subj=%s", ctx);
  2181. security_release_secctx(ctx, len);
  2182. }
  2183. }
  2184. audit_log_format(ab, " pid=%d comm=", current->pid);
  2185. audit_log_untrustedstring(ab, current->comm);
  2186. audit_log_format(ab, " sig=%ld", signr);
  2187. audit_log_end(ab);
  2188. }