lsm_audit.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * common LSM auditing functions
  3. *
  4. * Based on code written for SELinux by :
  5. * Stephen Smalley, <sds@epoch.ncsc.mil>
  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 defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  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. ip6 = ipv6_hdr(skb);
  109. if (ip6 == NULL)
  110. return -EINVAL;
  111. ipv6_addr_copy(&ad->u.net.v6info.saddr, &ip6->saddr);
  112. ipv6_addr_copy(&ad->u.net.v6info.daddr, &ip6->daddr);
  113. ret = 0;
  114. /* IPv6 can have several extension header before the Transport header
  115. * skip them */
  116. offset = skb_network_offset(skb);
  117. offset += sizeof(*ip6);
  118. nexthdr = ip6->nexthdr;
  119. offset = ipv6_skip_exthdr(skb, offset, &nexthdr);
  120. if (offset < 0)
  121. return 0;
  122. if (proto)
  123. *proto = nexthdr;
  124. switch (nexthdr) {
  125. case IPPROTO_TCP: {
  126. struct tcphdr _tcph, *th;
  127. th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
  128. if (th == NULL)
  129. break;
  130. ad->u.net.sport = th->source;
  131. ad->u.net.dport = th->dest;
  132. break;
  133. }
  134. case IPPROTO_UDP: {
  135. struct udphdr _udph, *uh;
  136. uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
  137. if (uh == NULL)
  138. break;
  139. ad->u.net.sport = uh->source;
  140. ad->u.net.dport = uh->dest;
  141. break;
  142. }
  143. case IPPROTO_DCCP: {
  144. struct dccp_hdr _dccph, *dh;
  145. dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
  146. if (dh == NULL)
  147. break;
  148. ad->u.net.sport = dh->dccph_sport;
  149. ad->u.net.dport = dh->dccph_dport;
  150. break;
  151. }
  152. case IPPROTO_SCTP: {
  153. struct sctphdr _sctph, *sh;
  154. sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
  155. if (sh == NULL)
  156. break;
  157. ad->u.net.sport = sh->source;
  158. ad->u.net.dport = sh->dest;
  159. break;
  160. }
  161. default:
  162. ret = -EINVAL;
  163. }
  164. return ret;
  165. }
  166. #endif
  167. static inline void print_ipv6_addr(struct audit_buffer *ab,
  168. struct in6_addr *addr, __be16 port,
  169. char *name1, char *name2)
  170. {
  171. if (!ipv6_addr_any(addr))
  172. audit_log_format(ab, " %s=%pI6c", name1, addr);
  173. if (port)
  174. audit_log_format(ab, " %s=%d", name2, ntohs(port));
  175. }
  176. static inline void print_ipv4_addr(struct audit_buffer *ab, __be32 addr,
  177. __be16 port, char *name1, char *name2)
  178. {
  179. if (addr)
  180. audit_log_format(ab, " %s=%pI4", name1, &addr);
  181. if (port)
  182. audit_log_format(ab, " %s=%d", name2, ntohs(port));
  183. }
  184. /**
  185. * dump_common_audit_data - helper to dump common audit data
  186. * @a : common audit data
  187. *
  188. */
  189. static void dump_common_audit_data(struct audit_buffer *ab,
  190. struct common_audit_data *a)
  191. {
  192. struct task_struct *tsk = current;
  193. if (a->tsk)
  194. tsk = a->tsk;
  195. if (tsk && tsk->pid) {
  196. audit_log_format(ab, " pid=%d comm=", tsk->pid);
  197. audit_log_untrustedstring(ab, tsk->comm);
  198. }
  199. switch (a->type) {
  200. case LSM_AUDIT_DATA_NONE:
  201. return;
  202. case LSM_AUDIT_DATA_IPC:
  203. audit_log_format(ab, " key=%d ", a->u.ipc_id);
  204. break;
  205. case LSM_AUDIT_DATA_CAP:
  206. audit_log_format(ab, " capability=%d ", a->u.cap);
  207. break;
  208. case LSM_AUDIT_DATA_PATH: {
  209. struct inode *inode;
  210. audit_log_d_path(ab, "path=", &a->u.path);
  211. inode = a->u.path.dentry->d_inode;
  212. if (inode)
  213. audit_log_format(ab, " dev=%s ino=%lu",
  214. inode->i_sb->s_id,
  215. inode->i_ino);
  216. break;
  217. }
  218. case LSM_AUDIT_DATA_DENTRY: {
  219. struct inode *inode;
  220. audit_log_format(ab, " name=");
  221. audit_log_untrustedstring(ab, a->u.dentry->d_name.name);
  222. inode = a->u.dentry->d_inode;
  223. if (inode)
  224. audit_log_format(ab, " dev=%s ino=%lu",
  225. inode->i_sb->s_id,
  226. inode->i_ino);
  227. break;
  228. }
  229. case LSM_AUDIT_DATA_INODE: {
  230. struct dentry *dentry;
  231. struct inode *inode;
  232. inode = a->u.inode;
  233. dentry = d_find_alias(inode);
  234. if (dentry) {
  235. audit_log_format(ab, " name=");
  236. audit_log_untrustedstring(ab,
  237. dentry->d_name.name);
  238. dput(dentry);
  239. }
  240. audit_log_format(ab, " dev=%s ino=%lu", inode->i_sb->s_id,
  241. inode->i_ino);
  242. break;
  243. }
  244. case LSM_AUDIT_DATA_TASK:
  245. tsk = a->u.tsk;
  246. if (tsk && tsk->pid) {
  247. audit_log_format(ab, " pid=%d comm=", tsk->pid);
  248. audit_log_untrustedstring(ab, tsk->comm);
  249. }
  250. break;
  251. case LSM_AUDIT_DATA_NET:
  252. if (a->u.net.sk) {
  253. struct sock *sk = a->u.net.sk;
  254. struct unix_sock *u;
  255. int len = 0;
  256. char *p = NULL;
  257. switch (sk->sk_family) {
  258. case AF_INET: {
  259. struct inet_sock *inet = inet_sk(sk);
  260. print_ipv4_addr(ab, inet->inet_rcv_saddr,
  261. inet->inet_sport,
  262. "laddr", "lport");
  263. print_ipv4_addr(ab, inet->inet_daddr,
  264. inet->inet_dport,
  265. "faddr", "fport");
  266. break;
  267. }
  268. case AF_INET6: {
  269. struct inet_sock *inet = inet_sk(sk);
  270. struct ipv6_pinfo *inet6 = inet6_sk(sk);
  271. print_ipv6_addr(ab, &inet6->rcv_saddr,
  272. inet->inet_sport,
  273. "laddr", "lport");
  274. print_ipv6_addr(ab, &inet6->daddr,
  275. inet->inet_dport,
  276. "faddr", "fport");
  277. break;
  278. }
  279. case AF_UNIX:
  280. u = unix_sk(sk);
  281. if (u->dentry) {
  282. struct path path = {
  283. .dentry = u->dentry,
  284. .mnt = u->mnt
  285. };
  286. audit_log_d_path(ab, "path=", &path);
  287. break;
  288. }
  289. if (!u->addr)
  290. break;
  291. len = u->addr->len-sizeof(short);
  292. p = &u->addr->name->sun_path[0];
  293. audit_log_format(ab, " path=");
  294. if (*p)
  295. audit_log_untrustedstring(ab, p);
  296. else
  297. audit_log_n_hex(ab, p, len);
  298. break;
  299. }
  300. }
  301. switch (a->u.net.family) {
  302. case AF_INET:
  303. print_ipv4_addr(ab, a->u.net.v4info.saddr,
  304. a->u.net.sport,
  305. "saddr", "src");
  306. print_ipv4_addr(ab, a->u.net.v4info.daddr,
  307. a->u.net.dport,
  308. "daddr", "dest");
  309. break;
  310. case AF_INET6:
  311. print_ipv6_addr(ab, &a->u.net.v6info.saddr,
  312. a->u.net.sport,
  313. "saddr", "src");
  314. print_ipv6_addr(ab, &a->u.net.v6info.daddr,
  315. a->u.net.dport,
  316. "daddr", "dest");
  317. break;
  318. }
  319. if (a->u.net.netif > 0) {
  320. struct net_device *dev;
  321. /* NOTE: we always use init's namespace */
  322. dev = dev_get_by_index(&init_net, a->u.net.netif);
  323. if (dev) {
  324. audit_log_format(ab, " netif=%s", dev->name);
  325. dev_put(dev);
  326. }
  327. }
  328. break;
  329. #ifdef CONFIG_KEYS
  330. case LSM_AUDIT_DATA_KEY:
  331. audit_log_format(ab, " key_serial=%u", a->u.key_struct.key);
  332. if (a->u.key_struct.key_desc) {
  333. audit_log_format(ab, " key_desc=");
  334. audit_log_untrustedstring(ab, a->u.key_struct.key_desc);
  335. }
  336. break;
  337. #endif
  338. case LSM_AUDIT_DATA_KMOD:
  339. audit_log_format(ab, " kmod=");
  340. audit_log_untrustedstring(ab, a->u.kmod_name);
  341. break;
  342. } /* switch (a->type) */
  343. }
  344. /**
  345. * common_lsm_audit - generic LSM auditing function
  346. * @a: auxiliary audit data
  347. *
  348. * setup the audit buffer for common security information
  349. * uses callback to print LSM specific information
  350. */
  351. void common_lsm_audit(struct common_audit_data *a)
  352. {
  353. struct audit_buffer *ab;
  354. if (a == NULL)
  355. return;
  356. /* we use GFP_ATOMIC so we won't sleep */
  357. ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_AVC);
  358. if (ab == NULL)
  359. return;
  360. if (a->lsm_pre_audit)
  361. a->lsm_pre_audit(ab, a);
  362. dump_common_audit_data(ab, a);
  363. if (a->lsm_post_audit)
  364. a->lsm_post_audit(ab, a);
  365. audit_log_end(ab);
  366. }