Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp9290708rwb; Thu, 24 Nov 2022 10:31:04 -0800 (PST) X-Google-Smtp-Source: AA0mqf4dmrpPxacKlGxWQoYL+iXs6/FvebHLNeHHGJLBD9TScA3NCE60zDDYUzgubHIB3/AWxL8W X-Received: by 2002:a05:6a00:f95:b0:56c:a60d:54d7 with SMTP id ct21-20020a056a000f9500b0056ca60d54d7mr21578941pfb.18.1669314664344; Thu, 24 Nov 2022 10:31:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669314664; cv=none; d=google.com; s=arc-20160816; b=HqLQeHikzmJbuR41JUp+7l+CFmbZcMflFpawt8HY+hy0Vl8K1a1bTlzgo8/Ycq9VLL EUGFTqkyioprrjwGYWb4gsfnCiHOv/vmXRXsbJoLL3iccnS+whgfk2Q5xfcNxDxKUZxE 4Gc/5FUxox0qnD1Q98EQyxnJxuuWu/EEF5h2x+/emeHaMV2o/zMQkoQznz87K9P32JJ4 trAaIdssPF9laFzXDCvb+eEkD/HgCU6xdqd3JsVoQEBZ9Wbc17s+2QHp6nj7EZfZ8EOd NOug9P4gNQrI/GNeGCS1j7XnQOO8WYpkdrTUuxgRIfc//RUz1ISThzdi/uSODPLYksMd r9dA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=/KFXcD5/aBYcFLibXAaTRcTYcuQsFKMCk/mHDgk2MPE=; b=ncxQTQLUEWZBSBEq0JsudzI5ViIW10YeT5up3Isu/Eu44NRSS0x5094kwM8ki3DtTq kBqOOcfe8cA9GC4WzNbyNWoAhLaaQpBPvVilzyF9HRijfHzh4AXkbWlaSuBSYoUjYgFJ u8uDA9eZgGnvyx6KGgeRV+dUsWFQGJkFAAct4CgOtj2wIEgT3JJTRtP3kYH1gfbDCi1j +qj4/wDvylEDpLhXl4jUeR5zBC3xVBf7UB3lpBe0/nYdnUiJdObcWbvzRF4UPNxtEjYU DbnC1cI1PBiuRuuruLbk7yV7msI43gfe0r6Nid9js8c7Dz2UcRzidWmnaVT+uA/VZVdv HUGw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=a7INjNtt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id oa11-20020a17090b1bcb00b00217d892ab40si5376698pjb.29.2022.11.24.10.30.52; Thu, 24 Nov 2022 10:31:04 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=a7INjNtt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229753AbiKXRuE (ORCPT + 86 others); Thu, 24 Nov 2022 12:50:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42942 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229690AbiKXRty (ORCPT ); Thu, 24 Nov 2022 12:49:54 -0500 Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 070AA100B28 for ; Thu, 24 Nov 2022 09:49:51 -0800 (PST) Received: by mail-wr1-x431.google.com with SMTP id g12so3428478wrs.10 for ; Thu, 24 Nov 2022 09:49:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=/KFXcD5/aBYcFLibXAaTRcTYcuQsFKMCk/mHDgk2MPE=; b=a7INjNttUqhN+V1+9wb6boOLLQlpK9hSctTnyB6MdZ+1dEuVChM2dXoINPP+KIDqem TDQMuwwh6Y2XdTfNkOE1NwRcLGAcg0BUUkV2Cb1FQN69fUqvMlNJmMEDcxo+gU+GZo7U sNIXvAc+dveqCNbijj2vbTdA2+KkePUWJN+/2PUsj2Exiycpgpje8CMbF5Av89WEQrdN sH9tNcPrmTtGl35D0OhdK/cWMWO3tJ8ptq52fJzzFAnMenVggqHjTFFsxSRkOBYi+NfG 8ROpfx8c1CwtQbrmvOQwLzNtZyd9eZ3nNcE2+QFF2f6WSjusD8bUYrNGWeCdQ+1TLtdD wehA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/KFXcD5/aBYcFLibXAaTRcTYcuQsFKMCk/mHDgk2MPE=; b=WlhnjvBqeQd3rzJesIOFWCvzQu2LoWYAV0aGRfuQqMFvXdZA93yMxz1ym/tNPi/A93 1c5rlRml/F5KvXjSJ5mky0SQ8GMamJ7B1iMnJTqQRqoCvxd8Mz9U0WfBGk+7FulS5RxC rRheE8uTpNXcUihNa5VPl4x/n9tBdM0gc7x6zx46EY7WZvM3c9NT0xvIjUwzs+YGI1oV x+bV5vQC2jIfYZNopO+2UhUrRdMcuNeY6UMrRx7aGcFiJp92cwm/+nEYnnkzKIHlgi0i pVFOsT3cg00d2Ehjliz0OWVitprbptMLZ7+p/jWmAXcosLN0ffus5Y5Z9ImMqmfmvg3r Oe9A== X-Gm-Message-State: ANoB5pkbxikRHKgzxRIosFElIC1pCfAYQ7qycEG6XFdZe1MMD9b1PozV fyyfeo+pvWKMx1JHsR+CNL5bhg== X-Received: by 2002:adf:ea43:0:b0:22e:433a:46ba with SMTP id j3-20020adfea43000000b0022e433a46bamr14163225wrn.575.1669312189541; Thu, 24 Nov 2022 09:49:49 -0800 (PST) Received: from localhost.localdomain ([5.133.47.210]) by smtp.gmail.com with ESMTPSA id l11-20020a1c790b000000b003b4a699ce8esm6250863wme.6.2022.11.24.09.49.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Nov 2022 09:49:49 -0800 (PST) From: Srinivas Kandagatla To: gregkh@linuxfoundation.org Cc: linux-kernel@vger.kernel.org, Ola Jeppsson , Abel Vesa , Srinivas Kandagatla Subject: [PATCH 3/3] misc: fastrpc: Fix use-after-free race condition for maps Date: Thu, 24 Nov 2022 17:49:41 +0000 Message-Id: <20221124174941.418450-4-srinivas.kandagatla@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221124174941.418450-1-srinivas.kandagatla@linaro.org> References: <20221124174941.418450-1-srinivas.kandagatla@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Ola Jeppsson It is possible that in between calling fastrpc_map_get() until map->fl->lock is taken in fastrpc_free_map(), another thread can call fastrpc_map_lookup() and get a reference to a map that is about to be deleted. Rewrite fastrpc_map_get() to only increase the reference count of a map if it's non-zero. Propagate this to callers so they can know if a map is about to be deleted. Fixes this warning: refcount_t: addition on 0; use-after-free. WARNING: CPU: 5 PID: 10100 at lib/refcount.c:25 refcount_warn_saturate ... Call trace: refcount_warn_saturate [fastrpc_map_get inlined] [fastrpc_map_lookup inlined] fastrpc_map_create fastrpc_internal_invoke fastrpc_device_ioctl __arm64_sys_ioctl invoke_syscall Fixes: c68cfb718c8f ("misc: fastrpc: Add support for context Invoke method") Signed-off-by: Ola Jeppsson Signed-off-by: Abel Vesa Signed-off-by: Srinivas Kandagatla --- drivers/misc/fastrpc.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c index 9ecbf673d321..80811e852d8f 100644 --- a/drivers/misc/fastrpc.c +++ b/drivers/misc/fastrpc.c @@ -332,10 +332,12 @@ static void fastrpc_map_put(struct fastrpc_map *map) kref_put(&map->refcount, fastrpc_free_map); } -static void fastrpc_map_get(struct fastrpc_map *map) +static int fastrpc_map_get(struct fastrpc_map *map) { - if (map) - kref_get(&map->refcount); + if (!map) + return -ENOENT; + + return kref_get_unless_zero(&map->refcount) ? 0 : -ENOENT; } -- 2.25.1