auditsc.c 67 KB

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