Skip to content

Commit 131ad62

Browse files
Mr Dash Fourkaber
authored andcommitted
netfilter: add SELinux context support to AUDIT target
In this revision the conversion of secid to SELinux context and adding it to the audit log is moved from xt_AUDIT.c to audit.c with the aid of a separate helper function - audit_log_secctx - which does both the conversion and logging of SELinux context, thus also preventing internal secid number being leaked to userspace. If conversion is not successful an error is raised. With the introduction of this helper function the work done in xt_AUDIT.c is much more simplified. It also opens the possibility of this helper function being used by other modules (including auditd itself), if desired. With this addition, typical (raw auditd) output after applying the patch would be: type=NETFILTER_PKT msg=audit(1305852240.082:31012): action=0 hook=1 len=52 inif=? outif=eth0 saddr=10.1.1.7 daddr=10.1.2.1 ipid=16312 proto=6 sport=56150 dport=22 obj=system_u:object_r:ssh_client_packet_t:s0 type=NETFILTER_PKT msg=audit(1306772064.079:56): action=0 hook=3 len=48 inif=eth0 outif=? smac=00:05:5d:7c:27:0b dmac=00:02:b3:0a:7f:81 macproto=0x0800 saddr=10.1.2.1 daddr=10.1.1.7 ipid=462 proto=6 sport=22 dport=3561 obj=system_u:object_r:ssh_server_packet_t:s0 Acked-by: Eric Paris <eparis@redhat.com> Signed-off-by: Mr Dash Four <mr.dash.four@googlemail.com> Signed-off-by: Patrick McHardy <kaber@trash.net>
1 parent 15b4d93 commit 131ad62

3 files changed

Lines changed: 41 additions & 0 deletions

File tree

‎include/linux/audit.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,12 @@ extern void audit_log_d_path(struct audit_buffer *ab,
613613
extern void audit_log_key(struct audit_buffer *ab,
614614
char *key);
615615
extern void audit_log_lost(const char *message);
616+
#ifdef CONFIG_SECURITY
617+
extern void audit_log_secctx(struct audit_buffer *ab, u32 secid);
618+
#else
619+
#define audit_log_secctx(b,s) do { ; } while (0)
620+
#endif
621+
616622
extern int audit_update_lsm_rules(void);
617623

618624
/* Private API (for audit.c only) */
@@ -635,6 +641,7 @@ extern int audit_enabled;
635641
#define audit_log_untrustedstring(a,s) do { ; } while (0)
636642
#define audit_log_d_path(b, p, d) do { ; } while (0)
637643
#define audit_log_key(b, k) do { ; } while (0)
644+
#define audit_log_secctx(b,s) do { ; } while (0)
638645
#define audit_enabled 0
639646
#endif
640647
#endif

‎kernel/audit.c‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@
5555
#include <net/sock.h>
5656
#include <net/netlink.h>
5757
#include <linux/skbuff.h>
58+
#ifdef CONFIG_SECURITY
59+
#include <linux/security.h>
60+
#endif
5861
#include <linux/netlink.h>
5962
#include <linux/freezer.h>
6063
#include <linux/tty.h>
@@ -1502,6 +1505,32 @@ void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type,
15021505
}
15031506
}
15041507

1508+
#ifdef CONFIG_SECURITY
1509+
/**
1510+
* audit_log_secctx - Converts and logs SELinux context
1511+
* @ab: audit_buffer
1512+
* @secid: security number
1513+
*
1514+
* This is a helper function that calls security_secid_to_secctx to convert
1515+
* secid to secctx and then adds the (converted) SELinux context to the audit
1516+
* log by calling audit_log_format, thus also preventing leak of internal secid
1517+
* to userspace. If secid cannot be converted audit_panic is called.
1518+
*/
1519+
void audit_log_secctx(struct audit_buffer *ab, u32 secid)
1520+
{
1521+
u32 len;
1522+
char *secctx;
1523+
1524+
if (security_secid_to_secctx(secid, &secctx, &len)) {
1525+
audit_panic("Cannot convert secid to context");
1526+
} else {
1527+
audit_log_format(ab, " obj=%s", secctx);
1528+
security_release_secctx(secctx, len);
1529+
}
1530+
}
1531+
EXPORT_SYMBOL(audit_log_secctx);
1532+
#endif
1533+
15051534
EXPORT_SYMBOL(audit_log_start);
15061535
EXPORT_SYMBOL(audit_log_end);
15071536
EXPORT_SYMBOL(audit_log_format);

‎net/netfilter/xt_AUDIT.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ audit_tg(struct sk_buff *skb, const struct xt_action_param *par)
163163
break;
164164
}
165165

166+
#ifdef CONFIG_NETWORK_SECMARK
167+
if (skb->secmark)
168+
audit_log_secctx(ab, skb->secmark);
169+
#endif
170+
166171
audit_log_end(ab);
167172

168173
errout:

0 commit comments

Comments
 (0)