src: add security context information
authorRoman Kubiak <r.kubiak@samsung.com>
Tue, 16 Jun 2015 16:14:47 +0000 (18:14 +0200)
committerr.kubiak <r.kubiak@samsung.com>
Mon, 16 Nov 2015 13:12:07 +0000 (14:12 +0100)
This commit adds security context information structures
and functions.

This will allow userspace to find the security context of each
packet (if it exists) and make decisions based on that.
It should work for SELinux and SMACK.

Signed-off-by: Roman Kubiak <r.kubiak@samsung.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
include/libnetfilter_queue/libnetfilter_queue.h
include/libnetfilter_queue/linux_nfnetlink_queue.h
include/linux/netfilter/nfnetlink_queue.h
src/libnetfilter_queue.c
src/nlmsg.c
utils/nfqnl_test.c

index bde720962b1cc6ad3d24bff3e616ad2cb6615dd8..2e384112e67a5d5e00671dc2e0ca2ec7cbfacb8b 100644 (file)
@@ -105,6 +105,7 @@ extern uint32_t nfq_get_outdev(struct nfq_data *nfad);
 extern uint32_t nfq_get_physoutdev(struct nfq_data *nfad);
 extern int nfq_get_uid(struct nfq_data *nfad, uint32_t *uid);
 extern int nfq_get_gid(struct nfq_data *nfad, uint32_t *gid);
+extern int nfq_get_secctx(struct nfq_data *nfad, unsigned char **secdata);
 
 extern int nfq_get_indev_name(struct nlif_handle *nlif_handle,
                              struct nfq_data *nfad, char *name);
@@ -129,6 +130,7 @@ enum {
        NFQ_XML_TIME    = (1 << 5),
        NFQ_XML_UID     = (1 << 6),
        NFQ_XML_GID     = (1 << 7),
+       NFQ_XML_SECCTX  = (1 << 8),
        NFQ_XML_ALL     = ~0U,
 };
 
index 5b6ae957d5c6ac69b96363c4a9275d53bc007130..1975dfa231d121e98d04d58e0b43082113e1c92f 100644 (file)
@@ -53,6 +53,7 @@ enum nfqnl_attr_type {
        NFQA_EXP,                       /* nf_conntrack_netlink.h */
        NFQA_UID,                       /* __u32 sk uid */
        NFQA_GID,                       /* __u32 sk gid */
+       NFQA_SECCTX,                    /* security context string */
 
        __NFQA_MAX
 };
@@ -106,7 +107,8 @@ enum nfqnl_attr_config {
 #define NFQA_CFG_F_CONNTRACK                   (1 << 1)
 #define NFQA_CFG_F_GSO                         (1 << 2)
 #define NFQA_CFG_F_UID_GID                     (1 << 3)
-#define NFQA_CFG_F_MAX                         (1 << 4)
+#define NFQA_CFG_F_SECCTX                      (1 << 4)
+#define NFQA_CFG_F_MAX                         (1 << 5)
 
 /* flags for NFQA_SKB_INFO */
 /* packet appears to have wrong checksums, but they are ok */
index 22f5d45de7018b7eb3ec7a0f418f7174f52977c7..030672dab643df19eddd9cdc36410fa8cbcdf2bc 100644 (file)
@@ -49,6 +49,7 @@ enum nfqnl_attr_type {
        NFQA_EXP,                       /* nf_conntrack_netlink.h */
        NFQA_UID,                       /* __u32 sk uid */
        NFQA_GID,                       /* __u32 sk gid */
+       NFQA_SECCTX,
 
        __NFQA_MAX
 };
@@ -102,7 +103,8 @@ enum nfqnl_attr_config {
 #define NFQA_CFG_F_CONNTRACK                   (1 << 1)
 #define NFQA_CFG_F_GSO                         (1 << 2)
 #define NFQA_CFG_F_UID_GID                     (1 << 3)
-#define NFQA_CFG_F_MAX                         (1 << 4)
+#define NFQA_CFG_F_SECCTX                      (1 << 4)
+#define NFQA_CFG_F_MAX                         (1 << 5)
 
 /* flags for NFQA_SKB_INFO */
 /* packet appears to have wrong checksums, but they are ok */
index c9ed865aaf4f2f803119b23cfa2bf6efef41f81c..84184eeed6299a8d831da90a5201ee36b3930a44 100644 (file)
@@ -1218,6 +1218,29 @@ int nfq_get_gid(struct nfq_data *nfad, uint32_t *gid)
 }
 EXPORT_SYMBOL(nfq_get_gid);
 
+
+/**
+ * nfq_get_secctx - get the security context for this packet
+ * \param nfad Netlink packet data handle passed to callback function
+ * \param secdata data to write the security context to
+ *
+ * \return -1 on error, otherwise > 0
+ */
+int nfq_get_secctx(struct nfq_data *nfad, unsigned char **secdata)
+{
+       if (!nfnl_attr_present(nfad->data, NFQA_SECCTX))
+               return -1;
+
+       *secdata = (unsigned char *)nfnl_get_pointer_to_data(nfad->data,
+                                                       NFQA_SECCTX, char);
+
+       if (*secdata)
+               return NFA_PAYLOAD(nfad->data[NFQA_SECCTX-1]);
+
+       return 0;
+}
+EXPORT_SYMBOL(nfq_get_secctx);
+
 /**
  * nfq_get_payload - get payload 
  * \param nfad Netlink packet data handle passed to callback function
index aebdd5e8a31068fc5f72299bd2a809713c7620ed..cabd8be064d4f55ee8687a5976f9fec9dda84836 100644 (file)
@@ -137,6 +137,7 @@ static int nfq_pkt_parse_attr_cb(const struct nlattr *attr, void *data)
        case NFQA_IFINDEX_PHYSOUTDEV:
        case NFQA_CAP_LEN:
        case NFQA_SKB_INFO:
+       case NFQA_SECCTX:
        case NFQA_UID:
        case NFQA_GID:
                if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
index b760cf0f0bad912bb15af7da5e139ee0228c1f16..5e76ffe48cc790ebce5558a05da9bfc523e05bd9 100644 (file)
@@ -17,7 +17,7 @@ static uint32_t print_pkt (struct nfq_data *tb)
        struct nfqnl_msg_packet_hw *hwph;
        uint32_t mark, ifi, uid, gid;
        int ret;
-       unsigned char *data;
+       unsigned char *data, *secdata;
 
        ph = nfq_get_msg_packet_hdr(tb);
        if (ph) {
@@ -61,6 +61,10 @@ static uint32_t print_pkt (struct nfq_data *tb)
        if (nfq_get_gid(tb, &gid))
                printf("gid=%u ", gid);
 
+       ret = nfq_get_secctx(tb, &secdata);
+       if (ret > 0)
+               printf("secctx=\"%.*s\" ", ret, secdata);
+
        ret = nfq_get_payload(tb, &data);
        if (ret >= 0)
                printf("payload_len=%d ", ret);
@@ -134,6 +138,12 @@ int main(int argc, char **argv)
                                "retrieve process UID/GID.\n");
        }
 
+       printf("setting flags to request security context\n");
+       if (nfq_set_queue_flags(qh, NFQA_CFG_F_SECCTX, NFQA_CFG_F_SECCTX)) {
+               fprintf(stderr, "This kernel version does not allow to "
+                               "retrieve security context.\n");
+       }
+
        printf("Waiting for packets...\n");
 
        fd = nfq_fd(h);