auditsc.c 64 KB

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