History

[NETLINK]: Synchronous message processing.

Let's recap the problem. The current asynchronous netlink kernel message processing is vulnerable to these attacks: 1) Hit and run: Attacker sends one or more messages and then exits before they're processed. This may confuse/disable the next netlink user that gets the netlink address of the attacker since it may receive the responses to the attacker's messages. Proposed solutions: a) Synchronous processing. b) Stream mode socket. c) Restrict/prohibit binding. 2) Starvation: Because various netlink rcv functions were written to not return until all messages have been processed on a socket, it is possible for these functions to execute for an arbitrarily long period of time. If this is successfully exploited it could also be used to hold rtnl forever. Proposed solutions: a) Synchronous processing. b) Stream mode socket. Firstly let's cross off solution c). It only solves the first problem and it has user-visible impacts. In particular, it'll break user space applications that expect to bind or communicate with specific netlink addresses (pid's). So we're left with a choice of synchronous processing versus SOCK_STREAM for netlink. For the moment I'm sticking with the synchronous approach as suggested by Alexey since it's simpler and I'd rather spend my time working on other things. However, it does have a number of deficiencies compared to the stream mode solution: 1) User-space to user-space netlink communication is still vulnerable. 2) Inefficient use of resources. This is especially true for rtnetlink since the lock is shared with other users such as networking drivers. The latter could hold the rtnl while communicating with hardware which causes the rtnetlink user to wait when it could be doing other things. 3) It is still possible to DoS all netlink users by flooding the kernel netlink receive queue. The attacker simply fills the receive socket with a single netlink message that fills up the entire queue. The attacker then continues to call sendmsg with the same message in a loop. Point 3) can be countered by retransmissions in user-space code, however it is pretty messy. In light of these problems (in particular, point 3), we should implement stream mode netlink at some point. In the mean time, here is a patch that implements synchronous processing.

Herbert Xu committed 21 years ago

Commit 2a0a6eb1 parent 96c36027 files · +55 −48Browse files

Unified · Split

7 files
  1. kernel/audit.c +8 −11
  2. net/core/rtnetlink.c +14 −9
  3. net/decnet/netfilter/dn_rtmsg.c +2 −1
  4. net/ipv4/netfilter/ip_queue.c +9 −11
  5. net/ipv4/tcp_diag.c +2 −1
  6. net/ipv6/netfilter/ip6_queue.c +9 −11
  7. net/xfrm/xfrm_user.c +11 −4
kernel/audit.c+8−11
@@ -427,7 +427,7 @@ static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh) · 426 unmodified lines
427427 /* Get message from skb (based on rtnetlink_rcv_skb). Each message is
428428 * processed by audit_receive_msg. Malformed skbs with wrong length are
429429 * discarded silently. */
430−static int audit_receive_skb(struct sk_buff *skb)
430+static void audit_receive_skb(struct sk_buff *skb)
431431 {
432432 int err;
433433 struct nlmsghdr *nlh;
@@ -436,7 +436,7 @@ static int audit_receive_skb(struct sk_buff *skb) · 2 unmodified lines
436436 while (skb->len >= NLMSG_SPACE(0)) {
437437 nlh = (struct nlmsghdr *)skb->data;
438438 if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
439− return 0;
439+ return;
440440 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
441441 if (rlen > skb->len)
442442 rlen = skb->len;
@@ -446,23 +446,20 @@ static int audit_receive_skb(struct sk_buff *skb) · 3 unmodified lines
446446 netlink_ack(skb, nlh, 0);
447447 skb_pull(skb, rlen);
448448 }
449− return 0;
450449 }
451450
452451 /* Receive messages from netlink socket. */
453452 static void audit_receive(struct sock *sk, int length)
454453 {
455454 struct sk_buff *skb;
455+ unsigned int qlen;
456456
457− if (down_trylock(&audit_netlink_sem))
458− return;
457+ down(&audit_netlink_sem);
459458
460− /* FIXME: this must not cause starvation */
461− while ((skb = skb_dequeue(&sk->sk_receive_queue))) {
462− if (audit_receive_skb(skb) && skb->len)
463− skb_queue_head(&sk->sk_receive_queue, skb);
464− else
465− kfree_skb(skb);
459+ for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
460+ skb = skb_dequeue(&sk->sk_receive_queue);
461+ audit_receive_skb(skb);
462+ kfree_skb(skb);
466463 }
467464 up(&audit_netlink_sem);
468465 }
net/core/rtnetlink.c+14−9
@@ -609,26 +609,31 @@ static inline int rtnetlink_rcv_skb(struct sk_buff *skb) · 608 unmodified lines
609609
610610 /*
611611 * rtnetlink input queue processing routine:
612− * - try to acquire shared lock. If it is failed, defer processing.
612+ * - process as much as there was in the queue upon entry.
613613 * - feed skbs to rtnetlink_rcv_skb, until it refuse a message,
614− * that will occur, when a dump started and/or acquisition of
615− * exclusive lock failed.
614+ * that will occur, when a dump started.
616615 */
617616
618617 static void rtnetlink_rcv(struct sock *sk, int len)
619618 {
619+ unsigned int qlen = skb_queue_len(&sk->sk_receive_queue);
620+
620621 do {
621622 struct sk_buff *skb;
622623
623− if (rtnl_shlock_nowait())
624− return;
624+ rtnl_lock();
625+
626+ if (qlen > skb_queue_len(&sk->sk_receive_queue))
627+ qlen = skb_queue_len(&sk->sk_receive_queue);
625628
626− while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
629+ while (qlen--) {
630+ skb = skb_dequeue(&sk->sk_receive_queue);
627631 if (rtnetlink_rcv_skb(skb)) {
628− if (skb->len)
632+ if (skb->len) {
629633 skb_queue_head(&sk->sk_receive_queue,
630634 skb);
631− else
635+ qlen++;
636+ } else
632637 kfree_skb(skb);
633638 break;
634639 }
@@ -638,7 +643,7 @@ static void rtnetlink_rcv(struct sock *sk, int len) · 3 unmodified lines
638643 up(&rtnl_sem);
639644
640645 netdev_run_todo();
641− } while (rtnl && rtnl->sk_receive_queue.qlen);
646+ } while (qlen);
642647 }
643648
644649 static struct rtnetlink_link link_rtnetlink_table[RTM_NR_MSGTYPES] =
net/decnet/netfilter/dn_rtmsg.c+2−1
@@ -119,8 +119,9 @@ static inline void dnrmg_receive_user_skb(struct sk_buff *skb) · 118 unmodified lines
119119 static void dnrmg_receive_user_sk(struct sock *sk, int len)
120120 {
121121 struct sk_buff *skb;
122+ unsigned int qlen = skb_queue_len(&sk->sk_receive_queue);
122123
123− while((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
124+ while (qlen-- && (skb = skb_dequeue(&sk->sk_receive_queue))) {
124125 dnrmg_receive_user_skb(skb);
125126 kfree_skb(skb);
126127 }
net/ipv4/netfilter/ip_queue.c+9−11
@@ -546,20 +546,18 @@ ipq_rcv_skb(struct sk_buff *skb) · 545 unmodified lines
546546 static void
547547 ipq_rcv_sk(struct sock *sk, int len)
548548 {
549− do {
550− struct sk_buff *skb;
549+ struct sk_buff *skb;
550+ unsigned int qlen;
551551
552− if (down_trylock(&ipqnl_sem))
553− return;
552+ down(&ipqnl_sem);
554553
555− while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
556− ipq_rcv_skb(skb);
557− kfree_skb(skb);
558− }
554+ for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
555+ skb = skb_dequeue(&sk->sk_receive_queue);
556+ ipq_rcv_skb(skb);
557+ kfree_skb(skb);
558+ }
559559
560− up(&ipqnl_sem);
561−
562− } while (ipqnl && ipqnl->sk_receive_queue.qlen);
560+ up(&ipqnl_sem);
563561 }
564562
565563 static int
net/ipv4/tcp_diag.c+2−1
@@ -777,8 +777,9 @@ static inline void tcpdiag_rcv_skb(struct sk_buff *skb) · 776 unmodified lines
777777 static void tcpdiag_rcv(struct sock *sk, int len)
778778 {
779779 struct sk_buff *skb;
780+ unsigned int qlen = skb_queue_len(&sk->sk_receive_queue);
780781
781− while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
782+ while (qlen-- && (skb = skb_dequeue(&sk->sk_receive_queue))) {
782783 tcpdiag_rcv_skb(skb);
783784 kfree_skb(skb);
784785 }
net/ipv6/netfilter/ip6_queue.c+9−11
@@ -549,20 +549,18 @@ ipq_rcv_skb(struct sk_buff *skb) · 548 unmodified lines
549549 static void
550550 ipq_rcv_sk(struct sock *sk, int len)
551551 {
552− do {
553− struct sk_buff *skb;
552+ struct sk_buff *skb;
553+ unsigned int qlen;
554554
555− if (down_trylock(&ipqnl_sem))
556− return;
555+ down(&ipqnl_sem);
557556
558− while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
559− ipq_rcv_skb(skb);
560− kfree_skb(skb);
561− }
557+ for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
558+ skb = skb_dequeue(&sk->sk_receive_queue);
559+ ipq_rcv_skb(skb);
560+ kfree_skb(skb);
561+ }
562562
563− up(&ipqnl_sem);
564−
565− } while (ipqnl && ipqnl->sk_receive_queue.qlen);
563+ up(&ipqnl_sem);
566564 }
567565
568566 static int
net/xfrm/xfrm_user.c+11−4
@@ -1008,17 +1008,24 @@ static int xfrm_user_rcv_skb(struct sk_buff *skb) · 1007 unmodified lines
10081008
10091009 static void xfrm_netlink_rcv(struct sock *sk, int len)
10101010 {
1011+ unsigned int qlen = skb_queue_len(&sk->sk_receive_queue);
1012+
10111013 do {
10121014 struct sk_buff *skb;
10131015
10141016 down(&xfrm_cfg_sem);
10151017
1016− while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
1018+ if (qlen > skb_queue_len(&sk->sk_receive_queue))
1019+ qlen = skb_queue_len(&sk->sk_receive_queue);
1020+
1021+ while (qlen--) {
1022+ skb = skb_dequeue(&sk->sk_receive_queue);
10171023 if (xfrm_user_rcv_skb(skb)) {
1018− if (skb->len)
1024+ if (skb->len) {
10191025 skb_queue_head(&sk->sk_receive_queue,
10201026 skb);
1021− else
1027+ qlen++;
1028+ } else
10221029 kfree_skb(skb);
10231030 break;
10241031 }
@@ -1027,7 +1034,7 @@ static void xfrm_netlink_rcv(struct sock *sk, int len) · 2 unmodified lines
10271034
10281035 up(&xfrm_cfg_sem);
10291036
1030− } while (xfrm_nl && xfrm_nl->sk_receive_queue.qlen);
1037+ } while (qlen);
10311038 }
10321039
10331040 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, int hard)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t