inet_diag: Move the INET_DIAG_REQ_BYTECODE nlattr to cb->data

The INET_DIAG_REQ_BYTECODE nlattr is currently re-found every time when
the "dump()" is re-started.

In a latter patch, it will also need to parse the new
INET_DIAG_REQ_SK_BPF_STORAGES nlattr to learn the map_fds. Thus, this
patch takes this chance to store the parsed nlattr in cb->data
during the "start" time of a dump.

By doing this, the "bc" argument also becomes unnecessary
and is removed.  Also, the two copies of the INET_DIAG_REQ_BYTECODE
parsing-audit logic between compat/current version can be
consolidated to one.

Signed-off-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Song Liu <songliubraving@fb.com>
Link: https://lore.kernel.org/bpf/20200225230415.1975555-1-kafai@fb.com
This commit is contained in:
Martin KaFai Lau 2020-02-25 15:04:15 -08:00 committed by Alexei Starovoitov
parent 5682d393b4
commit 0df6d32842
8 changed files with 98 additions and 64 deletions

View File

@ -15,8 +15,7 @@ struct netlink_callback;
struct inet_diag_handler { struct inet_diag_handler {
void (*dump)(struct sk_buff *skb, void (*dump)(struct sk_buff *skb,
struct netlink_callback *cb, struct netlink_callback *cb,
const struct inet_diag_req_v2 *r, const struct inet_diag_req_v2 *r);
struct nlattr *bc);
int (*dump_one)(struct netlink_callback *cb, int (*dump_one)(struct netlink_callback *cb,
const struct inet_diag_req_v2 *req); const struct inet_diag_req_v2 *req);
@ -39,6 +38,11 @@ struct inet_diag_handler {
__u16 idiag_info_size; __u16 idiag_info_size;
}; };
struct inet_diag_dump_data {
struct nlattr *req_nlas[__INET_DIAG_REQ_MAX];
#define inet_diag_nla_bc req_nlas[INET_DIAG_REQ_BYTECODE]
};
struct inet_connection_sock; struct inet_connection_sock;
int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk, int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
struct sk_buff *skb, struct netlink_callback *cb, struct sk_buff *skb, struct netlink_callback *cb,
@ -46,8 +50,7 @@ int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
u16 nlmsg_flags, bool net_admin); u16 nlmsg_flags, bool net_admin);
void inet_diag_dump_icsk(struct inet_hashinfo *h, struct sk_buff *skb, void inet_diag_dump_icsk(struct inet_hashinfo *h, struct sk_buff *skb,
struct netlink_callback *cb, struct netlink_callback *cb,
const struct inet_diag_req_v2 *r, const struct inet_diag_req_v2 *r);
struct nlattr *bc);
int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo, int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo,
struct netlink_callback *cb, struct netlink_callback *cb,
const struct inet_diag_req_v2 *req); const struct inet_diag_req_v2 *req);

View File

@ -64,9 +64,10 @@ struct inet_diag_req_raw {
enum { enum {
INET_DIAG_REQ_NONE, INET_DIAG_REQ_NONE,
INET_DIAG_REQ_BYTECODE, INET_DIAG_REQ_BYTECODE,
__INET_DIAG_REQ_MAX,
}; };
#define INET_DIAG_REQ_MAX INET_DIAG_REQ_BYTECODE #define INET_DIAG_REQ_MAX (__INET_DIAG_REQ_MAX - 1)
/* Bytecode is sequence of 4 byte commands followed by variable arguments. /* Bytecode is sequence of 4 byte commands followed by variable arguments.
* All the commands identified by "code" are conditional jumps forward: * All the commands identified by "code" are conditional jumps forward:

View File

@ -46,9 +46,9 @@ static void dccp_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
} }
static void dccp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, static void dccp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
const struct inet_diag_req_v2 *r, struct nlattr *bc) const struct inet_diag_req_v2 *r)
{ {
inet_diag_dump_icsk(&dccp_hashinfo, skb, cb, r, bc); inet_diag_dump_icsk(&dccp_hashinfo, skb, cb, r);
} }
static int dccp_diag_dump_one(struct netlink_callback *cb, static int dccp_diag_dump_one(struct netlink_callback *cb,

View File

@ -495,9 +495,11 @@ static int inet_diag_cmd_exact(int cmd, struct sk_buff *in_skb,
if (IS_ERR(handler)) { if (IS_ERR(handler)) {
err = PTR_ERR(handler); err = PTR_ERR(handler);
} else if (cmd == SOCK_DIAG_BY_FAMILY) { } else if (cmd == SOCK_DIAG_BY_FAMILY) {
struct inet_diag_dump_data empty_dump_data = {};
struct netlink_callback cb = { struct netlink_callback cb = {
.nlh = nlh, .nlh = nlh,
.skb = in_skb, .skb = in_skb,
.data = &empty_dump_data,
}; };
err = handler->dump_one(&cb, req); err = handler->dump_one(&cb, req);
} else if (cmd == SOCK_DESTROY && handler->destroy) { } else if (cmd == SOCK_DESTROY && handler->destroy) {
@ -863,14 +865,17 @@ static void twsk_build_assert(void)
void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb, void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
struct netlink_callback *cb, struct netlink_callback *cb,
const struct inet_diag_req_v2 *r, struct nlattr *bc) const struct inet_diag_req_v2 *r)
{ {
bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN); bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN);
struct inet_diag_dump_data *cb_data = cb->data;
struct net *net = sock_net(skb->sk); struct net *net = sock_net(skb->sk);
u32 idiag_states = r->idiag_states; u32 idiag_states = r->idiag_states;
int i, num, s_i, s_num; int i, num, s_i, s_num;
struct nlattr *bc;
struct sock *sk; struct sock *sk;
bc = cb_data->inet_diag_nla_bc;
if (idiag_states & TCPF_SYN_RECV) if (idiag_states & TCPF_SYN_RECV)
idiag_states |= TCPF_NEW_SYN_RECV; idiag_states |= TCPF_NEW_SYN_RECV;
s_i = cb->args[1]; s_i = cb->args[1];
@ -1014,15 +1019,14 @@ out:
EXPORT_SYMBOL_GPL(inet_diag_dump_icsk); EXPORT_SYMBOL_GPL(inet_diag_dump_icsk);
static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
const struct inet_diag_req_v2 *r, const struct inet_diag_req_v2 *r)
struct nlattr *bc)
{ {
const struct inet_diag_handler *handler; const struct inet_diag_handler *handler;
int err = 0; int err = 0;
handler = inet_diag_lock_handler(r->sdiag_protocol); handler = inet_diag_lock_handler(r->sdiag_protocol);
if (!IS_ERR(handler)) if (!IS_ERR(handler))
handler->dump(skb, cb, r, bc); handler->dump(skb, cb, r);
else else
err = PTR_ERR(handler); err = PTR_ERR(handler);
inet_diag_unlock_handler(handler); inet_diag_unlock_handler(handler);
@ -1032,13 +1036,57 @@ static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb) static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
{ {
int hdrlen = sizeof(struct inet_diag_req_v2); return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh));
struct nlattr *bc = NULL; }
if (nlmsg_attrlen(cb->nlh, hdrlen)) static int __inet_diag_dump_start(struct netlink_callback *cb, int hdrlen)
bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE); {
const struct nlmsghdr *nlh = cb->nlh;
struct inet_diag_dump_data *cb_data;
struct sk_buff *skb = cb->skb;
struct nlattr *nla;
int rem, err;
return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh), bc); cb_data = kzalloc(sizeof(*cb_data), GFP_KERNEL);
if (!cb_data)
return -ENOMEM;
nla_for_each_attr(nla, nlmsg_attrdata(nlh, hdrlen),
nlmsg_attrlen(nlh, hdrlen), rem) {
int type = nla_type(nla);
if (type < __INET_DIAG_REQ_MAX)
cb_data->req_nlas[type] = nla;
}
nla = cb_data->inet_diag_nla_bc;
if (nla) {
err = inet_diag_bc_audit(nla, skb);
if (err) {
kfree(cb_data);
return err;
}
}
cb->data = cb_data;
return 0;
}
static int inet_diag_dump_start(struct netlink_callback *cb)
{
return __inet_diag_dump_start(cb, sizeof(struct inet_diag_req_v2));
}
static int inet_diag_dump_start_compat(struct netlink_callback *cb)
{
return __inet_diag_dump_start(cb, sizeof(struct inet_diag_req));
}
static int inet_diag_dump_done(struct netlink_callback *cb)
{
kfree(cb->data);
return 0;
} }
static int inet_diag_type2proto(int type) static int inet_diag_type2proto(int type)
@ -1057,9 +1105,7 @@ static int inet_diag_dump_compat(struct sk_buff *skb,
struct netlink_callback *cb) struct netlink_callback *cb)
{ {
struct inet_diag_req *rc = nlmsg_data(cb->nlh); struct inet_diag_req *rc = nlmsg_data(cb->nlh);
int hdrlen = sizeof(struct inet_diag_req);
struct inet_diag_req_v2 req; struct inet_diag_req_v2 req;
struct nlattr *bc = NULL;
req.sdiag_family = AF_UNSPEC; /* compatibility */ req.sdiag_family = AF_UNSPEC; /* compatibility */
req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type); req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type);
@ -1067,10 +1113,7 @@ static int inet_diag_dump_compat(struct sk_buff *skb,
req.idiag_states = rc->idiag_states; req.idiag_states = rc->idiag_states;
req.id = rc->id; req.id = rc->id;
if (nlmsg_attrlen(cb->nlh, hdrlen)) return __inet_diag_dump(skb, cb, &req);
bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
return __inet_diag_dump(skb, cb, &req, bc);
} }
static int inet_diag_get_exact_compat(struct sk_buff *in_skb, static int inet_diag_get_exact_compat(struct sk_buff *in_skb,
@ -1098,22 +1141,12 @@ static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh)
return -EINVAL; return -EINVAL;
if (nlh->nlmsg_flags & NLM_F_DUMP) { if (nlh->nlmsg_flags & NLM_F_DUMP) {
if (nlmsg_attrlen(nlh, hdrlen)) { struct netlink_dump_control c = {
struct nlattr *attr; .start = inet_diag_dump_start_compat,
int err; .done = inet_diag_dump_done,
.dump = inet_diag_dump_compat,
attr = nlmsg_find_attr(nlh, hdrlen, };
INET_DIAG_REQ_BYTECODE); return netlink_dump_start(net->diag_nlsk, skb, nlh, &c);
err = inet_diag_bc_audit(attr, skb);
if (err)
return err;
}
{
struct netlink_dump_control c = {
.dump = inet_diag_dump_compat,
};
return netlink_dump_start(net->diag_nlsk, skb, nlh, &c);
}
} }
return inet_diag_get_exact_compat(skb, nlh); return inet_diag_get_exact_compat(skb, nlh);
@ -1129,22 +1162,12 @@ static int inet_diag_handler_cmd(struct sk_buff *skb, struct nlmsghdr *h)
if (h->nlmsg_type == SOCK_DIAG_BY_FAMILY && if (h->nlmsg_type == SOCK_DIAG_BY_FAMILY &&
h->nlmsg_flags & NLM_F_DUMP) { h->nlmsg_flags & NLM_F_DUMP) {
if (nlmsg_attrlen(h, hdrlen)) { struct netlink_dump_control c = {
struct nlattr *attr; .start = inet_diag_dump_start,
int err; .done = inet_diag_dump_done,
.dump = inet_diag_dump,
attr = nlmsg_find_attr(h, hdrlen, };
INET_DIAG_REQ_BYTECODE); return netlink_dump_start(net->diag_nlsk, skb, h, &c);
err = inet_diag_bc_audit(attr, skb);
if (err)
return err;
}
{
struct netlink_dump_control c = {
.dump = inet_diag_dump,
};
return netlink_dump_start(net->diag_nlsk, skb, h, &c);
}
} }
return inet_diag_cmd_exact(h->nlmsg_type, skb, h, nlmsg_data(h)); return inet_diag_cmd_exact(h->nlmsg_type, skb, h, nlmsg_data(h));

View File

@ -138,17 +138,21 @@ static int sk_diag_dump(struct sock *sk, struct sk_buff *skb,
} }
static void raw_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, static void raw_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
const struct inet_diag_req_v2 *r, struct nlattr *bc) const struct inet_diag_req_v2 *r)
{ {
bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN); bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN);
struct raw_hashinfo *hashinfo = raw_get_hashinfo(r); struct raw_hashinfo *hashinfo = raw_get_hashinfo(r);
struct net *net = sock_net(skb->sk); struct net *net = sock_net(skb->sk);
struct inet_diag_dump_data *cb_data;
int num, s_num, slot, s_slot; int num, s_num, slot, s_slot;
struct sock *sk = NULL; struct sock *sk = NULL;
struct nlattr *bc;
if (IS_ERR(hashinfo)) if (IS_ERR(hashinfo))
return; return;
cb_data = cb->data;
bc = cb_data->inet_diag_nla_bc;
s_slot = cb->args[0]; s_slot = cb->args[0];
num = s_num = cb->args[1]; num = s_num = cb->args[1];

View File

@ -179,9 +179,9 @@ static size_t tcp_diag_get_aux_size(struct sock *sk, bool net_admin)
} }
static void tcp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, static void tcp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
const struct inet_diag_req_v2 *r, struct nlattr *bc) const struct inet_diag_req_v2 *r)
{ {
inet_diag_dump_icsk(&tcp_hashinfo, skb, cb, r, bc); inet_diag_dump_icsk(&tcp_hashinfo, skb, cb, r);
} }
static int tcp_diag_dump_one(struct netlink_callback *cb, static int tcp_diag_dump_one(struct netlink_callback *cb,

View File

@ -89,12 +89,16 @@ out_nosk:
static void udp_dump(struct udp_table *table, struct sk_buff *skb, static void udp_dump(struct udp_table *table, struct sk_buff *skb,
struct netlink_callback *cb, struct netlink_callback *cb,
const struct inet_diag_req_v2 *r, struct nlattr *bc) const struct inet_diag_req_v2 *r)
{ {
bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN); bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN);
struct net *net = sock_net(skb->sk); struct net *net = sock_net(skb->sk);
struct inet_diag_dump_data *cb_data;
int num, s_num, slot, s_slot; int num, s_num, slot, s_slot;
struct nlattr *bc;
cb_data = cb->data;
bc = cb_data->inet_diag_nla_bc;
s_slot = cb->args[0]; s_slot = cb->args[0];
num = s_num = cb->args[1]; num = s_num = cb->args[1];
@ -142,9 +146,9 @@ done:
} }
static void udp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, static void udp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
const struct inet_diag_req_v2 *r, struct nlattr *bc) const struct inet_diag_req_v2 *r)
{ {
udp_dump(&udp_table, skb, cb, r, bc); udp_dump(&udp_table, skb, cb, r);
} }
static int udp_diag_dump_one(struct netlink_callback *cb, static int udp_diag_dump_one(struct netlink_callback *cb,
@ -245,10 +249,9 @@ static const struct inet_diag_handler udp_diag_handler = {
}; };
static void udplite_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, static void udplite_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
const struct inet_diag_req_v2 *r, const struct inet_diag_req_v2 *r)
struct nlattr *bc)
{ {
udp_dump(&udplite_table, skb, cb, r, bc); udp_dump(&udplite_table, skb, cb, r);
} }
static int udplite_diag_dump_one(struct netlink_callback *cb, static int udplite_diag_dump_one(struct netlink_callback *cb,

View File

@ -471,7 +471,7 @@ static int sctp_diag_dump_one(struct netlink_callback *cb,
} }
static void sctp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, static void sctp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
const struct inet_diag_req_v2 *r, struct nlattr *bc) const struct inet_diag_req_v2 *r)
{ {
u32 idiag_states = r->idiag_states; u32 idiag_states = r->idiag_states;
struct net *net = sock_net(skb->sk); struct net *net = sock_net(skb->sk);