From owner-freebsd-net@freebsd.org Mon Sep 10 21:00:46 2018 Return-Path: Delivered-To: freebsd-net@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 4C338109C4CE for ; Mon, 10 Sep 2018 21:00:46 +0000 (UTC) (envelope-from rysto32@gmail.com) Received: from mail-lj1-x22b.google.com (mail-lj1-x22b.google.com [IPv6:2a00:1450:4864:20::22b]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id B2F6E76573 for ; Mon, 10 Sep 2018 21:00:45 +0000 (UTC) (envelope-from rysto32@gmail.com) Received: by mail-lj1-x22b.google.com with SMTP id u83-v6so19077578lje.12 for ; Mon, 10 Sep 2018 14:00:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=KnShdxc81pfWLMMKF2/DAzt47st20f1fXM2xX5XB37U=; b=YXa/K7bB+knbhfen1ofKTuFQU7247KqNnG1asSk71wMrhPHMVS5davkzXNzdRLsWpd V+uYja5O8uOgvDXr7+1SaeonBZYw3GrR8U83jM+fJ9xMcQclZkj65+tDpI1w3AuOaAHl +uBzsAHwEAb3ja6R8PMy+3sS1Iqt5xCIAx/BjfL7NefK/Ku1EUKmgSDrpXOF3S4wzBWu Fu5c378yaDOPk81qYZ238kxrZC5L0C51VADDeXTTRFKYA8bdNX8/UrsTpZ6UDRDTRAjy Vffs9hrIYD15DOQN6pvU2Jz0dv65USOINhXGSteuuHwoAKysCtQRzmjjrVF7M490DpBx CqLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=KnShdxc81pfWLMMKF2/DAzt47st20f1fXM2xX5XB37U=; b=iYH65QYdlDM367IDI+yuFN8Qq0FMhOPfaX2oerzveKe6YKYV0lq1Mv0YM3Yzg++ceS f94adrSJhDK6LLu/8Gc6a0V0P9lvqfCn3ty8MUThgFYA5Yza1tReiUtAj5bHDQAio456 tGZM6IsD2FwUGYVpN8OzfrCIdJQ+6eup9Ts+gGgY5hGUZrzeOlwTNx4nbxFImxUHJ0bW AkpQGM/S06ZePJfS8cq70CC0w8ZP1LlGgdtRVzNtK3IN5fkd55m7QuVl0yMXa2ns/AU5 KzElZ2PZYQEB7Ae7IqwLAKLer9SuNsK8O6dqckot6XeZotDQU+EI95KB4/tF4GS/8Vsw 7akA== X-Gm-Message-State: APzg51Ct1o3b4JDMRqvv+3endzrq5Wa9xlyYfqjnh70x4JSVDw4gFrq3 IP7Y2NQaRg/b0PesXAIykprLyVRF/7zhv5B/7ICQB8JBnTc= X-Google-Smtp-Source: ANB0VdbC/Ylx4qyvLTyXyye0dfGQLDfrW/AHqj7bTxLB/eLUjUOnJpU895/Kl5dCoVxPWmDnn8FKhaiBgDYjLJSfq+c= X-Received: by 2002:a2e:1301:: with SMTP id 1-v6mr13075732ljt.56.1536613244000; Mon, 10 Sep 2018 14:00:44 -0700 (PDT) MIME-Version: 1.0 From: Ryan Stone Date: Mon, 10 Sep 2018 17:00:33 -0400 Message-ID: Subject: udp_notify() invalidates the routing cache without a write lock To: freebsd-net Content-Type: text/plain; charset="UTF-8" X-BeenThere: freebsd-net@freebsd.org X-Mailman-Version: 2.1.27 Precedence: list List-Id: Networking and TCP/IP with FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 10 Sep 2018 21:00:46 -0000 Recently at work I had a system crash while executing RTFREE() in udp_notify(). In looking at the system I discovered that two threads had called udp_notify() on the same pcb. This was possible because the threads only held a read lock on the socket. The obvious solution is to hold a write lock in this path. I haven't even compile-tested the patch below yet, but would anybody have any objections to this approach? diff --git a/sys/netinet/udp_usrreq.c b/sys/netinet/udp_usrreq.c index cae044c066c3..429f195ee954 100644 --- a/sys/netinet/udp_usrreq.c +++ b/sys/netinet/udp_usrreq.c @@ -756,13 +756,7 @@ struct inpcb * udp_notify(struct inpcb *inp, int errno) { - /* - * While udp_ctlinput() always calls udp_notify() with a read lock - * when invoking it directly, in_pcbnotifyall() currently uses write - * locks due to sharing code with TCP. For now, accept either a read - * or a write lock, but a read lock is sufficient. - */ - INP_LOCK_ASSERT(inp); + INP_WLOCK_ASSERT(inp); if ((errno == EHOSTUNREACH || errno == ENETUNREACH || errno == EHOSTDOWN) && inp->inp_route.ro_rt) { RTFREE(inp->inp_route.ro_rt); @@ -808,13 +802,13 @@ udp_common_ctlinput(int cmd, struct sockaddr *sa, void *vip, if (ip != NULL) { uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2)); inp = in_pcblookup(pcbinfo, faddr, uh->uh_dport, - ip->ip_src, uh->uh_sport, INPLOOKUP_RLOCKPCB, NULL); + ip->ip_src, uh->uh_sport, INPLOOKUP_WLOCKPCB, NULL); if (inp != NULL) { - INP_RLOCK_ASSERT(inp); + INP_WLOCK_ASSERT(inp); if (inp->inp_socket != NULL) { udp_notify(inp, inetctlerrmap[cmd]); } - INP_RUNLOCK(inp); + INP_WUNLOCK(inp); } else { inp = in_pcblookup(pcbinfo, faddr, uh->uh_dport, ip->ip_src, uh->uh_sport,