auditsc.c 66 KB

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