lsm_audit.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. * common LSM auditing functions
  3. *
  4. * Based on code written for SELinux by :
  5. * Stephen Smalley, <sds@tycho.nsa.gov>
  6. * James Morris <jmorris@redhat.com>
  7. * Author : Etienne Basset, <etienne.basset@ensta.org>
  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 version 2,
  11. * as published by the Free Software Foundation.
  12. */
  13. #include <linux/types.h>
  14. #include <linux/stddef.h>
  15. #include <linux/kernel.h>
  16. #include <linux/gfp.h>
  17. #include <linux/fs.h>
  18. #include <linux/init.h>
  19. #include <net/sock.h>
  20. #include <linux/un.h>
  21. #include <net/af_unix.h>
  22. #include <linux/audit.h>
  23. #include <linux/ipv6.h>
  24. #include <linux/ip.h>
  25. #include <net/ip.h>
  26. #include <net/ipv6.h>
  27. #include <linux/tcp.h>
  28. #include <linux/udp.h>
  29. #include <linux/dccp.h>
  30. #include <linux/sctp.h>
  31. #include <linux/lsm_audit.h>
  32. /**
  33. * ipv4_skb_to_auditdata : fill auditdata from skb
  34. * @skb : the skb
  35. * @ad : the audit data to fill
  36. * @proto : the layer 4 protocol
  37. *
  38. * return 0 on success
  39. */
  40. int ipv4_skb_to_auditdata(struct sk_buff *skb,
  41. struct common_audit_data *ad, u8 *proto)
  42. {
  43. int ret = 0;
  44. struct iphdr *ih;
  45. ih = ip_hdr(skb);
  46. if (ih == NULL)
  47. return -EINVAL;
  48. ad->u.net->v4info.saddr = ih->saddr;
  49. ad->u.net->v4info.daddr = ih->daddr;
  50. if (proto)
  51. *proto = ih->protocol;
  52. /* non initial fragment */
  53. if (ntohs(ih->frag_off) & IP_OFFSET)
  54. return 0;
  55. switch (ih->protocol) {
  56. case IPPROTO_TCP: {
  57. struct tcphdr *th = tcp_hdr(skb);
  58. if (th == NULL)
  59. break;
  60. ad->u.net->sport = th->source;
  61. ad->u.net->dport = th->dest;
  62. break;
  63. }
  64. case IPPROTO_UDP: {
  65. struct udphdr *uh = udp_hdr(skb);
  66. if (uh == NULL)
  67. break;
  68. ad->u.net->sport = uh->source;
  69. ad->u.net->dport = uh->dest;
  70. break;
  71. }
  72. case IPPROTO_DCCP: {
  73. struct dccp_hdr *dh = dccp_hdr(skb);
  74. if (dh == NULL)
  75. break;
  76. ad->u.net->sport = dh->dccph_sport;
  77. ad->u.net->dport = dh->dccph_dport;
  78. break;
  79. }
  80. case IPPROTO_SCTP: {
  81. struct sctphdr *sh = sctp_hdr(skb);
  82. if (sh == NULL)
  83. break;
  84. ad->u.net->sport = sh->source;
  85. ad->u.net->dport = sh->dest;
  86. break;
  87. }
  88. default:
  89. ret = -EINVAL;
  90. }
  91. return ret;
  92. }
  93. #if IS_ENABLED(CONFIG_IPV6)
  94. /**
  95. * ipv6_skb_to_auditdata : fill auditdata from skb
  96. * @skb : the skb
  97. * @ad : the audit data to fill
  98. * @proto : the layer 4 protocol
  99. *
  100. * return 0 on success
  101. */
  102. int ipv6_skb_to_auditdata(struct sk_buff *skb,
  103. struct common_audit_data *ad, u8 *proto)
  104. {
  105. int offset, ret = 0;
  106. struct ipv6hdr *ip6;
  107. u8 nexthdr;
  108. __be16 frag_off;
  109. ip6 = ipv6_hdr(skb);
  110. if (ip6 == NULL)
  111. return -EINVAL;
  112. ad->u.net->v6info.saddr = ip6->saddr;
  113. ad->u.net->v6info.daddr = ip6->daddr;
  114. ret = 0;
  115. /* IPv6 can have several extension header before the Transport header
  116. * skip them */
  117. offset = skb_network_offset(skb);
  118. offset += sizeof(*ip6);
  119. nexthdr = ip6->nexthdr;
  120. offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
  121. if (offset < 0)
  122. return 0;
  123. if (proto)
  124. *proto = nexthdr;
  125. switch (nexthdr) {
  126. case IPPROTO_TCP: {
  127. struct tcphdr _tcph, *th;
  128. th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
  129. if (th == NULL)
  130. break;
  131. ad->u.net->sport = th->source;
  132. ad->u.net->dport = th->dest;
  133. break;
  134. }
  135. case IPPROTO_UDP: {
  136. struct udphdr _udph, *uh;
  137. uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
  138. if (uh == NULL)
  139. break;
  140. ad->u.net->sport = uh->source;
  141. ad->u.net->dport = uh->dest;
  142. break;
  143. }
  144. case IPPROTO_DCCP: {
  145. struct dccp_hdr _dccph, *dh;
  146. dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
  147. if (dh == NULL)
  148. break;
  149. ad->u.net->sport = dh->dccph_sport;
  150. ad->u.net->dport = dh->dccph_dport;
  151. break;
  152. }
  153. case IPPROTO_SCTP: {
  154. struct sctphdr _sctph, *sh;
  155. sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
  156. if (sh == NULL)
  157. break;
  158. ad->u.net->sport = sh->source;
  159. ad->u.net->dport = sh->dest;
  160. break;
  161. }
  162. default:
  163. ret = -EINVAL;
  164. }
  165. return ret;
  166. }
  167. #endif
  168. static inline void print_ipv6_addr(struct audit_buffer *ab,
  169. struct in6_addr *addr, __be16 port,
  170. char *name1, char *name2)
  171. {
  172. if (!ipv6_addr_any(addr))
  173. audit_log_format(ab, " %s=%pI6c", name1, addr);
  174. if (port)
  175. audit_log_format(ab, " %s=%d", name2, ntohs(port));
  176. }
  177. static inline void print_ipv4_addr(struct audit_buffer *ab, __be32 addr,
  178. __be16 port, char *name1, char *name2)
  179. {
  180. if (addr)
  181. audit_log_format(ab, " %s=%pI4", name1, &addr);
  182. if (port)
  183. audit_log_format(ab, " %s=%d", name2, ntohs(port));
  184. }
  185. /**
  186. * dump_common_audit_data - helper to dump common audit data
  187. * @a : common audit data
  188. *
  189. */
  190. static void dump_common_audit_data(struct audit_buffer *ab,
  191. struct common_audit_data *a)
  192. {
  193. char comm[sizeof(current->comm)];
  194. /*
  195. * To keep stack sizes in check force programers to notice if they
  196. * start making this union too large! See struct lsm_network_audit
  197. * as an example of how to deal with large data.
  198. */
  199. BUILD_BUG_ON(sizeof(a->u) > sizeof(void *)*2);
  200. audit_log_format(ab, " pid=%d comm=", task_tgid_nr(current));
  201. audit_log_untrustedstring(ab, memcpy(comm, current->comm, sizeof(comm)));
  202. switch (a->type) {
  203. case LSM_AUDIT_DATA_NONE:
  204. return;
  205. case LSM_AUDIT_DATA_IPC:
  206. audit_log_format(ab, " key=%d ", a->u.ipc_id);
  207. break;
  208. case LSM_AUDIT_DATA_CAP:
  209. audit_log_format(ab, " capability=%d ", a->u.cap);
  210. break;
  211. case LSM_AUDIT_DATA_PATH: {
  212. struct inode *inode;
  213. audit_log_d_path(ab, " path=", &a->u.path);
  214. inode = d_backing_inode(a->u.path.dentry);
  215. if (inode) {
  216. audit_log_format(ab, " dev=");
  217. audit_log_untrustedstring(ab, inode->i_sb->s_id);
  218. audit_log_format(ab, " ino=%lu", inode->i_ino);
  219. }
  220. break;
  221. }
  222. case LSM_AUDIT_DATA_FILE: {
  223. struct inode *inode;
  224. audit_log_d_path(ab, " path=", &a->u.file->f_path);
  225. inode = file_inode(a->u.file);
  226. if (inode) {
  227. audit_log_format(ab, " dev=");
  228. audit_log_untrustedstring(ab, inode->i_sb->s_id);
  229. audit_log_format(ab, " ino=%lu", inode->i_ino);
  230. }
  231. break;
  232. }
  233. case LSM_AUDIT_DATA_IOCTL_OP: {
  234. struct inode *inode;
  235. audit_log_d_path(ab, " path=", &a->u.op->path);
  236. inode = a->u.op->path.dentry->d_inode;
  237. if (inode) {
  238. audit_log_format(ab, " dev=");
  239. audit_log_untrustedstring(ab, inode->i_sb->s_id);
  240. audit_log_format(ab, " ino=%lu", inode->i_ino);
  241. }
  242. audit_log_format(ab, " ioctlcmd=0x%hx", a->u.op->cmd);
  243. break;
  244. }
  245. case LSM_AUDIT_DATA_DENTRY: {
  246. struct inode *inode;
  247. audit_log_format(ab, " name=");
  248. spin_lock(&a->u.dentry->d_lock);
  249. audit_log_untrustedstring(ab, a->u.dentry->d_name.name);
  250. spin_unlock(&a->u.dentry->d_lock);
  251. inode = d_backing_inode(a->u.dentry);
  252. if (inode) {
  253. audit_log_format(ab, " dev=");
  254. audit_log_untrustedstring(ab, inode->i_sb->s_id);
  255. audit_log_format(ab, " ino=%lu", inode->i_ino);
  256. }
  257. break;
  258. }
  259. case LSM_AUDIT_DATA_INODE: {
  260. struct dentry *dentry;
  261. struct inode *inode;
  262. inode = a->u.inode;
  263. dentry = d_find_alias(inode);
  264. if (dentry) {
  265. audit_log_format(ab, " name=");
  266. spin_lock(&dentry->d_lock);
  267. audit_log_untrustedstring(ab, dentry->d_name.name);
  268. spin_unlock(&dentry->d_lock);
  269. dput(dentry);
  270. }
  271. audit_log_format(ab, " dev=");
  272. audit_log_untrustedstring(ab, inode->i_sb->s_id);
  273. audit_log_format(ab, " ino=%lu", inode->i_ino);
  274. break;
  275. }
  276. case LSM_AUDIT_DATA_TASK: {
  277. struct task_struct *tsk = a->u.tsk;
  278. if (tsk) {
  279. pid_t pid = task_tgid_nr(tsk);
  280. if (pid) {
  281. char comm[sizeof(tsk->comm)];
  282. audit_log_format(ab, " opid=%d ocomm=", pid);
  283. audit_log_untrustedstring(ab,
  284. memcpy(comm, tsk->comm, sizeof(comm)));
  285. }
  286. }
  287. break;
  288. }
  289. case LSM_AUDIT_DATA_NET:
  290. if (a->u.net->sk) {
  291. struct sock *sk = a->u.net->sk;
  292. struct unix_sock *u;
  293. struct unix_address *addr;
  294. int len = 0;
  295. char *p = NULL;
  296. switch (sk->sk_family) {
  297. case AF_INET: {
  298. struct inet_sock *inet = inet_sk(sk);
  299. print_ipv4_addr(ab, inet->inet_rcv_saddr,
  300. inet->inet_sport,
  301. "laddr", "lport");
  302. print_ipv4_addr(ab, inet->inet_daddr,
  303. inet->inet_dport,
  304. "faddr", "fport");
  305. break;
  306. }
  307. #if IS_ENABLED(CONFIG_IPV6)
  308. case AF_INET6: {
  309. struct inet_sock *inet = inet_sk(sk);
  310. print_ipv6_addr(ab, &sk->sk_v6_rcv_saddr,
  311. inet->inet_sport,
  312. "laddr", "lport");
  313. print_ipv6_addr(ab, &sk->sk_v6_daddr,
  314. inet->inet_dport,
  315. "faddr", "fport");
  316. break;
  317. }
  318. #endif
  319. case AF_UNIX:
  320. u = unix_sk(sk);
  321. addr = smp_load_acquire(&u->addr);
  322. if (!addr)
  323. break;
  324. if (u->path.dentry) {
  325. audit_log_d_path(ab, " path=", &u->path);
  326. break;
  327. }
  328. len = addr->len-sizeof(short);
  329. p = &addr->name->sun_path[0];
  330. audit_log_format(ab, " path=");
  331. if (*p)
  332. audit_log_untrustedstring(ab, p);
  333. else
  334. audit_log_n_hex(ab, p, len);
  335. break;
  336. }
  337. }
  338. switch (a->u.net->family) {
  339. case AF_INET:
  340. print_ipv4_addr(ab, a->u.net->v4info.saddr,
  341. a->u.net->sport,
  342. "saddr", "src");
  343. print_ipv4_addr(ab, a->u.net->v4info.daddr,
  344. a->u.net->dport,
  345. "daddr", "dest");
  346. break;
  347. case AF_INET6:
  348. print_ipv6_addr(ab, &a->u.net->v6info.saddr,
  349. a->u.net->sport,
  350. "saddr", "src");
  351. print_ipv6_addr(ab, &a->u.net->v6info.daddr,
  352. a->u.net->dport,
  353. "daddr", "dest");
  354. break;
  355. }
  356. if (a->u.net->netif > 0) {
  357. struct net_device *dev;
  358. /* NOTE: we always use init's namespace */
  359. dev = dev_get_by_index(&init_net, a->u.net->netif);
  360. if (dev) {
  361. audit_log_format(ab, " netif=%s", dev->name);
  362. dev_put(dev);
  363. }
  364. }
  365. break;
  366. #ifdef CONFIG_KEYS
  367. case LSM_AUDIT_DATA_KEY:
  368. audit_log_format(ab, " key_serial=%u", a->u.key_struct.key);
  369. if (a->u.key_struct.key_desc) {
  370. audit_log_format(ab, " key_desc=");
  371. audit_log_untrustedstring(ab, a->u.key_struct.key_desc);
  372. }
  373. break;
  374. #endif
  375. case LSM_AUDIT_DATA_KMOD:
  376. audit_log_format(ab, " kmod=");
  377. audit_log_untrustedstring(ab, a->u.kmod_name);
  378. break;
  379. case LSM_AUDIT_DATA_IBPKEY: {
  380. struct in6_addr sbn_pfx;
  381. memset(&sbn_pfx.s6_addr, 0,
  382. sizeof(sbn_pfx.s6_addr));
  383. memcpy(&sbn_pfx.s6_addr, &a->u.ibpkey->subnet_prefix,
  384. sizeof(a->u.ibpkey->subnet_prefix));
  385. audit_log_format(ab, " pkey=0x%x subnet_prefix=%pI6c",
  386. a->u.ibpkey->pkey, &sbn_pfx);
  387. break;
  388. }
  389. case LSM_AUDIT_DATA_IBENDPORT:
  390. audit_log_format(ab, " device=%s port_num=%u",
  391. a->u.ibendport->dev_name,
  392. a->u.ibendport->port);
  393. break;
  394. } /* switch (a->type) */
  395. }
  396. /**
  397. * common_lsm_audit - generic LSM auditing function
  398. * @a: auxiliary audit data
  399. * @pre_audit: lsm-specific pre-audit callback
  400. * @post_audit: lsm-specific post-audit callback
  401. *
  402. * setup the audit buffer for common security information
  403. * uses callback to print LSM specific information
  404. */
  405. void common_lsm_audit(struct common_audit_data *a,
  406. void (*pre_audit)(struct audit_buffer *, void *),
  407. void (*post_audit)(struct audit_buffer *, void *))
  408. {
  409. struct audit_buffer *ab;
  410. if (a == NULL)
  411. return;
  412. /* we use GFP_ATOMIC so we won't sleep */
  413. ab = audit_log_start(current->audit_context, GFP_ATOMIC | __GFP_NOWARN,
  414. AUDIT_AVC);
  415. if (ab == NULL)
  416. return;
  417. if (pre_audit)
  418. pre_audit(ab, a);
  419. dump_common_audit_data(ab, a);
  420. if (post_audit)
  421. post_audit(ab, a);
  422. audit_log_end(ab);
  423. }