auditsc.c 65 KB

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