Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2720841pxj; Mon, 31 May 2021 09:04:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwx9VZPLYKRQqUkR0TPUnw+C8tJeotaeaeIpQ2ySFKBosC4JRLr4r0lVCL5wQWd1VlN1WnA X-Received: by 2002:a05:6e02:1bc7:: with SMTP id x7mr17555495ilv.206.1622477078753; Mon, 31 May 2021 09:04:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622477078; cv=none; d=google.com; s=arc-20160816; b=JJqfnrKI92ArM302OC1oZ45jQhZXV9c6DDbkuRZbpzPBj+t6konEHRfE/Jy01SdEyH 7P82iGr1hMUpeZAdIWrU/sHsbz4SURA32RXR8p2Y/QruzWIzN3XXWS6F+cdWvQicmKqX mTwH79vjYNDycxH8jypIVKwBkiq1y7mRMEkneQm2L+Y6PvwWCybLOHRgUZPSKPG5xH1f d9vxA90ud2oxV4RaLQspJ11cAlbkBvz+68Ca34OIsUOpecaSBVVt2SJEUEBuZukL3C2I W7VgB10NySVl8wzItJFlxDrJL82B4FsrrRhBifYFdf8JBPROMNa7GxxRZF1c7mYug4VI DscA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=5UZgeM4LqUY2448vW/xXLWOEHewE19TZujNUSg2r3vA=; b=Ykxuswwqjjk0UY40zD80CyW/bPBp16V/gIizaGGLiOrJEkFqRQ0/7TTE2EaDSLD8Um 4X9YHe2f5VKa2VBVRuyQI/37Zu8FPb4jay0PDneC3mVSS9Up4hxc7gEC4Jvn+tPNy6ur eAI/c7vNy7etEIHOSUDo9c+CG5WS6zueN0Ig3g+pCKAIbWH36O+S4jVFsiG/Fsnwbpqx vMK0EC06VTqlMfU7dW+hYbrRJb4rlliFMsFvQuxvGpkZFlZWTW7CMMnKuv53AoX99B22 f8M9hlZYNIjTjitQ156ZaZjoSjbm3Tgo2Zdb3OHEtywSvxyl5EaRiNY3urY3oKWqbhZY Y6HA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ybTIFwuL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r10si15430229ill.70.2021.05.31.09.04.25; Mon, 31 May 2021 09:04:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ybTIFwuL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233827AbhEaQDx (ORCPT + 99 others); Mon, 31 May 2021 12:03:53 -0400 Received: from mail.kernel.org ([198.145.29.99]:33230 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232923AbhEaOdf (ORCPT ); Mon, 31 May 2021 10:33:35 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id CD43361C3E; Mon, 31 May 2021 13:50:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1622469016; bh=5wWAXwDd1+8lNt75Apd3en7MY6KsLBnr9VlAI1gWSdA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ybTIFwuLjd6Vl6bhhyiNTIr8c/y97IelknsDkpkWj5cfXHj0wcrIMYKAHoCmWWafT malohKgCDZup6/c99+fFyw6zA3Az1m81XIePvZxv2hgCMmezE2kLH1VdQAsEHhvhpg gr4GJahGXWE6YmHm7OiA7QcUXtdCSa+8g6EdfGuk= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Norbert Slusarek , Thadeu Lima de Souza Cascardo , Oliver Hartkopp , Marc Kleine-Budde Subject: [PATCH 5.12 020/296] can: isotp: prevent race between isotp_bind() and isotp_setsockopt() Date: Mon, 31 May 2021 15:11:15 +0200 Message-Id: <20210531130704.442908261@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210531130703.762129381@linuxfoundation.org> References: <20210531130703.762129381@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Norbert Slusarek commit 2b17c400aeb44daf041627722581ade527bb3c1d upstream. A race condition was found in isotp_setsockopt() which allows to change socket options after the socket was bound. For the specific case of SF_BROADCAST support, this might lead to possible use-after-free because can_rx_unregister() is not called. Checking for the flag under the socket lock in isotp_bind() and taking the lock in isotp_setsockopt() fixes the issue. Fixes: 921ca574cd38 ("can: isotp: add SF_BROADCAST support for functional addressing") Link: https://lore.kernel.org/r/trinity-e6ae9efa-9afb-4326-84c0-f3609b9b8168-1620773528307@3c-app-gmx-bs06 Reported-by: Norbert Slusarek Signed-off-by: Thadeu Lima de Souza Cascardo Signed-off-by: Norbert Slusarek Acked-by: Oliver Hartkopp Signed-off-by: Marc Kleine-Budde Signed-off-by: Greg Kroah-Hartman --- net/can/isotp.c | 49 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 16 deletions(-) --- a/net/can/isotp.c +++ b/net/can/isotp.c @@ -1062,27 +1062,31 @@ static int isotp_bind(struct socket *soc if (len < ISOTP_MIN_NAMELEN) return -EINVAL; + if (addr->can_addr.tp.tx_id & (CAN_ERR_FLAG | CAN_RTR_FLAG)) + return -EADDRNOTAVAIL; + + if (!addr->can_ifindex) + return -ENODEV; + + lock_sock(sk); + /* do not register frame reception for functional addressing */ if (so->opt.flags & CAN_ISOTP_SF_BROADCAST) do_rx_reg = 0; /* do not validate rx address for functional addressing */ if (do_rx_reg) { - if (addr->can_addr.tp.rx_id == addr->can_addr.tp.tx_id) - return -EADDRNOTAVAIL; + if (addr->can_addr.tp.rx_id == addr->can_addr.tp.tx_id) { + err = -EADDRNOTAVAIL; + goto out; + } - if (addr->can_addr.tp.rx_id & (CAN_ERR_FLAG | CAN_RTR_FLAG)) - return -EADDRNOTAVAIL; + if (addr->can_addr.tp.rx_id & (CAN_ERR_FLAG | CAN_RTR_FLAG)) { + err = -EADDRNOTAVAIL; + goto out; + } } - if (addr->can_addr.tp.tx_id & (CAN_ERR_FLAG | CAN_RTR_FLAG)) - return -EADDRNOTAVAIL; - - if (!addr->can_ifindex) - return -ENODEV; - - lock_sock(sk); - if (so->bound && addr->can_ifindex == so->ifindex && addr->can_addr.tp.rx_id == so->rxid && addr->can_addr.tp.tx_id == so->txid) @@ -1164,16 +1168,13 @@ static int isotp_getname(struct socket * return ISOTP_MIN_NAMELEN; } -static int isotp_setsockopt(struct socket *sock, int level, int optname, +static int isotp_setsockopt_locked(struct socket *sock, int level, int optname, sockptr_t optval, unsigned int optlen) { struct sock *sk = sock->sk; struct isotp_sock *so = isotp_sk(sk); int ret = 0; - if (level != SOL_CAN_ISOTP) - return -EINVAL; - if (so->bound) return -EISCONN; @@ -1248,6 +1249,22 @@ static int isotp_setsockopt(struct socke return ret; } +static int isotp_setsockopt(struct socket *sock, int level, int optname, + sockptr_t optval, unsigned int optlen) + +{ + struct sock *sk = sock->sk; + int ret; + + if (level != SOL_CAN_ISOTP) + return -EINVAL; + + lock_sock(sk); + ret = isotp_setsockopt_locked(sock, level, optname, optval, optlen); + release_sock(sk); + return ret; +} + static int isotp_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen) {