Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp2129647rwe; Fri, 2 Sep 2022 08:57:28 -0700 (PDT) X-Google-Smtp-Source: AA6agR5WpPOIJeQuRY6Eun9X10Y2XwS+9i4T2FPhkGly7rjcN6qeCGU1Z+GIWtsYBaLxtcXQfvDC X-Received: by 2002:a17:907:3f22:b0:73d:9a03:2abf with SMTP id hq34-20020a1709073f2200b0073d9a032abfmr27250630ejc.518.1662134248058; Fri, 02 Sep 2022 08:57:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662134248; cv=none; d=google.com; s=arc-20160816; b=z2dKNzc0g/yUg8MCUF/L5vEo7bpFOucyg79KS0w5fwUXlFiAa9PeKfCaaxXOMYQ0eg xlqATGgFsC8IvhxwH8o32gxrxuXzA3ttK7jf3j4GIGF3j9hFZXYCz/5oXd01AI6np2eL l5u7pcciYkepD62UyeHTw91mOz0UgdBj7g6mNBvf4AAvQkh+3YfmPKWRHiLLeibcMOSI ZHA1bpvgttCJDueOXiaRgZwBRWlUR+36Ig9oHHjQZdGKS1jorWh3RxiSnzd3gtUmqCqs 281/IrRMBmqEP8NngYqMM2mFTEfpxgX3xo4KFCGB9L0kUle508+uKYfc+C2T6UW1Kkkk jokg== 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=ppnIzcD1Lvog5KH9Fyfxjv2BYJnZPxI6mOxm9069x0Q=; b=0MHm9kaPPA3pPqdW/7jWCVW5iCtNQ3U/Tu39tVNkaOHp6VqcBBIiypUJtcnrOQ/z5V X60fJ1Aun8rrrfuNkEL/QVKJbUoPqmMIVAK/BZDXfr1GIJXNoI/3zGmVhNTU2GU/mhbL Clr+t48sT8nef38BZAp8QUQa+0RGI63tQn5SocLRpYrLM4FQWQm9UyS3Gq9feKwcJVbs VsQp8d6V/s7jgoVXQ6Y45oMILd3bOWjsT7xwLvSbz0R7889REwzmV9DGWxPbCoh3ygXW urnXCDEZrVJa2f88BQ8AiZbbQ6sOswlVIKYz7ys2Pisi6jPKJ1HuL4nSnJJHC5C0yiC2 v/Nw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Fvsdo4X7; 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 x9-20020a05640226c900b00445e20bc95bsi568537edd.428.2022.09.02.08.56.39; Fri, 02 Sep 2022 08:57:28 -0700 (PDT) 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=Fvsdo4X7; 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 S236612AbiIBPc4 (ORCPT + 99 others); Fri, 2 Sep 2022 11:32:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37582 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236606AbiIBPcZ (ORCPT ); Fri, 2 Sep 2022 11:32:25 -0400 Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C4F9F656E for ; Fri, 2 Sep 2022 08:15:03 -0700 (PDT) Received: by mail-wm1-x331.google.com with SMTP id m17-20020a7bce11000000b003a5bedec07bso3539566wmc.0 for ; Fri, 02 Sep 2022 08:15:03 -0700 (PDT) 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; bh=ppnIzcD1Lvog5KH9Fyfxjv2BYJnZPxI6mOxm9069x0Q=; b=Fvsdo4X7t20LzXiT5gHh6YEPAklYxulIy6hd9e1+71rOqYERWz3mUY8QIpce1YWs5c T4GxcW+az/HLD9HYS7mo1WII36FmPQ62/cGEAJtSHd4mhVyFpkXKXjTmAPX2rHvb9Yjl Wqc40omyd0iKDDLJkDYsYX1UUrW1NYCPYzx04NkdrKpDmvNTUK+atIJDatnxUJVTIaz3 R8GCtkFGKGeMd5D+tqYAvs2oChSOTCBt9HUJDvR+iJzpcZFrBlyA72C7ZuMddbEhzxMw A8TXhu5Zpw2yscF2sKZ8r07wchn6YlROakTcdPXQ15+/Q4SU9f2bcjlVF3nZWT5YWHjq 3lyA== 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; bh=ppnIzcD1Lvog5KH9Fyfxjv2BYJnZPxI6mOxm9069x0Q=; b=NNP6ihMXlhC0Ys4SFUia+KwLEpt/puoDwoV4oPb0flCGRl8OHixIWXBDG76mwVOMjC Do9DreXh523dEJ+LOtss7Gp66+6jia5QtZrp+X7yZluxd35Uz2ibMYTJ/1iz01VBjOdZ OlKx25wJcvDOp9CxKjIDKVqhRqlldlV2hIfr9qZ/gt+XhBbIrRTivpe728BsJOvrFZBM RJM4jMcj+SpBMe9HXo77jECIxPhUovaBfDdXHXCABtLabKgvVnYWJrT2ba6HtJ/h7oOq L7O0IGFHPo1cGC3paHAX9is99mC0Fx9J3TsgbcEjPBrbsxuxyqVoJwAt+TeKWLY4MHPW 5+lw== X-Gm-Message-State: ACgBeo2WiZE9AUq9c0vcAAJeD0nqdkn1hLxxnAWF4pB3yez7v1Ay6aoK 0xtW6rDwuPm3HMIUQVne1Z1wQA== X-Received: by 2002:a05:600c:2311:b0:3a6:84e5:bcad with SMTP id 17-20020a05600c231100b003a684e5bcadmr3239020wmo.72.1662131702350; Fri, 02 Sep 2022 08:15:02 -0700 (PDT) Received: from hackbox.lan ([94.52.112.99]) by smtp.gmail.com with ESMTPSA id w10-20020a05600c014a00b003a4efb794d7sm2361493wmm.36.2022.09.02.08.15.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Sep 2022 08:15:01 -0700 (PDT) From: Abel Vesa To: Srinivas Kandagatla , Amol Maheshwari , Ekansh Gupta , Vamsi Krishna Gattupalli Cc: Arnd Bergmann , Greg Kroah-Hartman , linux-arm-msm@vger.kernel.org, Linux Kernel Mailing List , Ola Jeppsson Subject: [PATCH 3/3] misc: fastrpc: Fix use-after-free race condition for maps Date: Fri, 2 Sep 2022 18:14:23 +0300 Message-Id: <20220902151423.3351414-3-abel.vesa@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220902151423.3351414-1-abel.vesa@linaro.org> References: <20220902151423.3351414-1-abel.vesa@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,T_SCC_BODY_TEXT_LINE 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: c68cfb718c8f9 ("misc: fastrpc: Add support for context Invoke method") Signed-off-by: Ola Jeppsson Signed-off-by: Abel Vesa --- 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 50c17f5da3a8..58654d394d17 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.34.1