auditsc.c 74 KB

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