auditsc.c 64 KB

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