auditsc.c 67 KB

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