auditsc.c 66 KB

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