From patchwork Fri Sep 2 15:14:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abel Vesa X-Patchwork-Id: 602221 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 528C2ECAAD5 for ; Fri, 2 Sep 2022 15:32:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236908AbiIBPco (ORCPT ); Fri, 2 Sep 2022 11:32:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49334 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236548AbiIBPcY (ORCPT ); Fri, 2 Sep 2022 11:32:24 -0400 Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1D4F729828 for ; Fri, 2 Sep 2022 08:15:00 -0700 (PDT) Received: by mail-wr1-x42b.google.com with SMTP id b5so2722691wrr.5 for ; Fri, 02 Sep 2022 08:15:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date; bh=7Woa6saHlcsqIiHSXMMSyYnI6pEpe3JjUt9ToPHjSVc=; b=zvTQisGu7Utjpmg7NStfiBPYJGrLEtYn4b2DMeUfX2NFLX3/z+Mv0dXr5OlYgoqt7L oMzsQ0fhUGYvi0KdkycR04vYJbUzKI/NoZWzzYTVqFFdc3tleFmPiKo2d3ymZCgzJRbV dmTrgjfAGhddobhhTCJxDpYHydYF6VR1+T0EV9NLaq8YWSDf2oCO2VOrXQCmckTS7/9G H9z7A4sQgtFuWcYPw2w8ExFxSo0OmrC2cHyLCapJJW6cu5nVr7U58PU5WXzeVnUenYUf q/fkWnQBp5ZBf+fj9HLeKuvJwI1SqyJuXhubFkDYvRvR3GIfug8dGkDrY3j8mcghqEuq RLaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date; bh=7Woa6saHlcsqIiHSXMMSyYnI6pEpe3JjUt9ToPHjSVc=; b=WyU5flgclt4mpi4xR87BkhKkuI7uIo4oX2rjxP4TVpG748ZDHZQOAUMuznjeDCQhqO m3CEf5RtEP7uY9Q2w7RbbR4NJLMjdW2Roj77Yupsxh8XEkJU0/L6X3wC+Auc80xvxzJq 3NW7I3BDUapwUw6ZthdP00+wQ4/jPeqt/oKSJ+ZsqmWlZ9IzSjIVW047r/UD79sOhkl7 hItVBHT5XjdypVTnD49/yRAlaEvgxYLfrs2YW+DTaLh2FuwZm7vZLCmnwpUuhY/hczka DvpyMl8ZrUFM0AEwd8SpofPBBq1r73ea4LW6PzKEVhDRV6YIorK206KkeHktv9xIst6L qDXg== X-Gm-Message-State: ACgBeo2/a8Yv+WLFl8ywsm8rSm98OeuNR7C+KbocU0x8Xh2Sr55yP2yI KITKRwP1kueW4TmFwdnkSRSm8Q== X-Google-Smtp-Source: AA6agR47s6LZ7URck0z+aWYLm4j0Hqze93FHpXJcyDcX7Npe9C2JConVK4hs9FfI9+mXKSM6lA+1DQ== X-Received: by 2002:adf:dec9:0:b0:226:e033:c048 with SMTP id i9-20020adfdec9000000b00226e033c048mr12330529wrn.577.1662131699369; Fri, 02 Sep 2022 08:14:59 -0700 (PDT) Received: from hackbox.lan ([94.52.112.99]) by smtp.gmail.com with ESMTPSA id w10-20020a05600c014a00b003a4efb794d7sm2361493wmm.36.2022.09.02.08.14.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Sep 2022 08:14:58 -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 1/3] misc: fastrpc: Fix use-after-free and race in fastrpc_map_find Date: Fri, 2 Sep 2022 18:14:21 +0300 Message-Id: <20220902151423.3351414-1-abel.vesa@linaro.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-arm-msm@vger.kernel.org Currently, there is a race window between the point when the mutex is unlocked in fastrpc_map_lookup and the reference count increasing (fastrpc_map_get) in fastrpc_map_find, which can also lead to use-after-free. So lets merge fastrpc_map_find into fastrpc_map_lookup which allows us to both protect the maps list by also taking the &fl->lock spinlock and the reference count, since the spinlock will be released only after. Add take_ref argument to make this suitable for all callers. Fixes: 8f6c1d8c4f0c ("misc: fastrpc: Add fdlist implementation") Co-developed-by: Ola Jeppsson Signed-off-by: Ola Jeppsson Signed-off-by: Abel Vesa Reviewed-by: Srinivas Kandagatla --- drivers/misc/fastrpc.c | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c index 93ebd174d848..0c816a11eeec 100644 --- a/drivers/misc/fastrpc.c +++ b/drivers/misc/fastrpc.c @@ -333,30 +333,31 @@ static void fastrpc_map_get(struct fastrpc_map *map) static int fastrpc_map_lookup(struct fastrpc_user *fl, int fd, - struct fastrpc_map **ppmap) + struct fastrpc_map **ppmap, bool take_ref) { + struct fastrpc_session_ctx *sess = fl->sctx; struct fastrpc_map *map = NULL; + int ret = -ENOENT; - mutex_lock(&fl->mutex); + spin_lock(&fl->lock); list_for_each_entry(map, &fl->maps, node) { - if (map->fd == fd) { - *ppmap = map; - mutex_unlock(&fl->mutex); - return 0; - } - } - mutex_unlock(&fl->mutex); - - return -ENOENT; -} + if (map->fd != fd) + continue; -static int fastrpc_map_find(struct fastrpc_user *fl, int fd, - struct fastrpc_map **ppmap) -{ - int ret = fastrpc_map_lookup(fl, fd, ppmap); + if (take_ref) { + ret = fastrpc_map_get(map); + if (ret) { + dev_dbg(sess->dev, "%s: Failed to get map fd=%d ret=%d\n", + __func__, fd, ret); + break; + } + } - if (!ret) - fastrpc_map_get(*ppmap); + *ppmap = map; + ret = 0; + break; + } + spin_unlock(&fl->lock); return ret; } @@ -703,7 +704,7 @@ static int fastrpc_map_create(struct fastrpc_user *fl, int fd, struct fastrpc_map *map = NULL; int err = 0; - if (!fastrpc_map_find(fl, fd, ppmap)) + if (!fastrpc_map_lookup(fl, fd, ppmap, true)) return 0; map = kzalloc(sizeof(*map), GFP_KERNEL); @@ -1026,7 +1027,7 @@ static int fastrpc_put_args(struct fastrpc_invoke_ctx *ctx, for (i = 0; i < FASTRPC_MAX_FDLIST; i++) { if (!fdlist[i]) break; - if (!fastrpc_map_lookup(fl, (int)fdlist[i], &mmap)) + if (!fastrpc_map_lookup(fl, (int)fdlist[i], &mmap, false)) fastrpc_map_put(mmap); } From patchwork Fri Sep 2 15:14:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abel Vesa X-Patchwork-Id: 602438 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F0EB2C54EE9 for ; Fri, 2 Sep 2022 15:32:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236998AbiIBPcq (ORCPT ); Fri, 2 Sep 2022 11:32:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49374 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236598AbiIBPcY (ORCPT ); Fri, 2 Sep 2022 11:32:24 -0400 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BD2BC6577 for ; Fri, 2 Sep 2022 08:15:02 -0700 (PDT) Received: by mail-wm1-x32a.google.com with SMTP id d5so1418328wms.5 for ; Fri, 02 Sep 2022 08:15:02 -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=vho+m1JeucUWDUID2NTMp/Noqso8ylqG8qhETHGnWbE=; b=QVOiQ2e7b4P6KA2s1KjnsxNEuUTskSx/mRNh8xvVVJe7CdZqbZqcy0gJl4S6H9za6D yF8sX0J/6Vu3KQYvuMWTkOvGNW5WsIqHfLqk51R1wijjiAGl7b8oqNIE+OOmXKMqsTS2 XClwgCJhR5GazQ1izvnAKX5Qa1vjJ2A6RLSNVSYvhyeZTQeTeodPSiCfPatk9SqzEKiy Kh4NXr1RWZu/WKw3M3HhVI8IBfF7Bm7CqF14G1rzwK7dVHPqzNqId9I0W2EDz4sDUMYV sJTG+sJNnfxpWKSVwQeUAUZ1Pu5mVFoNfHvptmsq+2DrzqpqjWqHqStXjOwB5OdDoF2i WlhA== 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=vho+m1JeucUWDUID2NTMp/Noqso8ylqG8qhETHGnWbE=; b=GEMW0ZGx2ygKZALWJ9bpOZC3zBplR2h0RZtZM/+nFn2pD7ENCXZKlN5VEBXJIfjFyY wnMShhHMQIC1wCAxzPkaRWjTwTJQbf/YV1mnoUar3YGdZvwLKi0LXUrRt4qoJ6F5JuEh sH6RFUkszMYzNqULfa85T2m2a1pfY8xV3tGtlTOcPN8GrsguzKtK/I9Rv9uN6Z7+oLPm UrON62IsU64CFumtfwH1ak7akXONPocH01CvXbiYf1klHlPbTxJtpuQLlE4UhqF/vAym sJ9tGZ2T721DTcWJUEs1pUKCR3HO8cn/urbV479IIj/1szo3xRx+9/G0zm0/6+THTILm hWHg== X-Gm-Message-State: ACgBeo1vEFfDE8Pmk7i1lriNxnoug+ynMVUXwXiHd5LZNryjCQLUvoGm BVgEkFLIjyhS/E0LiPEIai7Wlw== X-Google-Smtp-Source: AA6agR5YmV6Ry4M1ECcCPF0E4/LManFJOQ1dPt91d6wIVp9px6/G8v/itdygTPG+0kCcSxt+YL8fsg== X-Received: by 2002:a7b:cd11:0:b0:3a8:3f6c:9abf with SMTP id f17-20020a7bcd11000000b003a83f6c9abfmr3299238wmj.30.1662131700793; Fri, 02 Sep 2022 08:15:00 -0700 (PDT) Received: from hackbox.lan ([94.52.112.99]) by smtp.gmail.com with ESMTPSA id w10-20020a05600c014a00b003a4efb794d7sm2361493wmm.36.2022.09.02.08.14.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Sep 2022 08:15:00 -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 2/3] misc: fastrpc: Don't remove map on creater_process and device_release Date: Fri, 2 Sep 2022 18:14:22 +0300 Message-Id: <20220902151423.3351414-2-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 Precedence: bulk List-ID: X-Mailing-List: linux-arm-msm@vger.kernel.org Do not remove the map from the list on error path in fastrpc_init_create_process, instead call fastrpc_map_put, to avoid use-after-free. Do not remove it on fastrpc_device_release either, call fastrpc_map_put instead. The fastrpc_free_map is the only proper place to remove the map. This is called only after the reference count is 0. Fixes: b49f6d83e290f ("misc: fastrpc: Fix a possible double free") Co-developed-by: Ola Jeppsson Signed-off-by: Ola Jeppsson Signed-off-by: Abel Vesa --- drivers/misc/fastrpc.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c index 0c816a11eeec..50c17f5da3a8 100644 --- a/drivers/misc/fastrpc.c +++ b/drivers/misc/fastrpc.c @@ -316,6 +316,13 @@ static void fastrpc_free_map(struct kref *ref) dma_buf_put(map->buf); } + if (map->fl) { + spin_lock(&map->fl->lock); + list_del(&map->node); + spin_unlock(&map->fl->lock); + map->fl = NULL; + } + kfree(map); } @@ -1266,12 +1273,7 @@ static int fastrpc_init_create_process(struct fastrpc_user *fl, fl->init_mem = NULL; fastrpc_buf_free(imem); err_alloc: - if (map) { - spin_lock(&fl->lock); - list_del(&map->node); - spin_unlock(&fl->lock); - fastrpc_map_put(map); - } + fastrpc_map_put(map); err: kfree(args); @@ -1347,10 +1349,8 @@ static int fastrpc_device_release(struct inode *inode, struct file *file) fastrpc_context_put(ctx); } - list_for_each_entry_safe(map, m, &fl->maps, node) { - list_del(&map->node); + list_for_each_entry_safe(map, m, &fl->maps, node) fastrpc_map_put(map); - } list_for_each_entry_safe(buf, b, &fl->mmaps, node) { list_del(&buf->node); From patchwork Fri Sep 2 15:14:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abel Vesa X-Patchwork-Id: 602439 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B11B3C001B5 for ; Fri, 2 Sep 2022 15:32:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235792AbiIBPcn (ORCPT ); Fri, 2 Sep 2022 11:32:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49290 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236916AbiIBPcX (ORCPT ); Fri, 2 Sep 2022 11:32:23 -0400 Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C7768A1BB for ; Fri, 2 Sep 2022 08:15:03 -0700 (PDT) Received: by mail-wm1-x332.google.com with SMTP id bd26-20020a05600c1f1a00b003a5e82a6474so1593102wmb.4 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=j9RgpVNtN6tPZi2zwTKaRoEXFXy6ZG7n7JNsEaZyIIMiHKLGZQOsxQnapPJFMFuxjK A0uGVFhxsKt75KTJ7IMl4iRKgzSW2IXpsoXD25Fghmw0fGhz2zRbpkAjmkhm52OoABe+ hdZAyvkxGSUhxlb6Oi8UgzY5m8g71FKv9rcy+C0lefOt+uX4twtlI1EAp2SdiYuMIKBt BAYflyU/vTeEJPdI+wKttVqLWZ5mu2q67OSvsqOXbKhY98yEHj34OtomO57dkvkKsToC 6+NxjtQqih2DeLKFzvqovbYTwu7PzSbG5gU40ID4CJpArAT38Ht+bUo9jAi+28mr1hGx w1vQ== X-Gm-Message-State: ACgBeo0LFqndiXnnMMbuFb9pzJJmcdQPH5Xt2rahBzh59022PSoNoaxu p9xX93T1PiBfpKF/c2DOmR5YtDzgA9zRxZWv X-Google-Smtp-Source: AA6agR7FlLcf18Fay4hYuE/8S7Rn5LcJExIwtFG5sZbJLUHX9IIteBZnXfpei+us/9OKzgyeQZXOyg== 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 Precedence: bulk List-ID: X-Mailing-List: linux-arm-msm@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; }