auditsc.c 66 KB

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