From nobody Fri Apr 19 23:47:40 2024 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 EB1A6C4332F for ; Thu, 24 Nov 2022 17:50:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229734AbiKXRt7 (ORCPT ); Thu, 24 Nov 2022 12:49:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42928 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229563AbiKXRtx (ORCPT ); Thu, 24 Nov 2022 12:49:53 -0500 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 CEDDD146FA6 for ; Thu, 24 Nov 2022 09:49:47 -0800 (PST) Received: by mail-wr1-x42b.google.com with SMTP id x17so3453352wrn.6 for ; Thu, 24 Nov 2022 09:49:47 -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=ETuS/ZDws2DT8KOkGYpc0M9sZjTmBC+kSqRug/kfs+s=; b=wBQ1HND9z8sejyb+zqjOaM1X1LrGU1oSlXQ4/ofgf+1t1AKE1EtwI6PZ8Tri+dvaJx VQJ6FPF+1+p2swQ4bE5bqd8cHj0nOJsRzwDqgsQbKfj5AbsAqDUiaTWXT17gumACJs7k kJFufmNX8c1lD0ZnRb6xG0y7EpnqvMfDF402l95eyB8lWi9Cd36i34Olm7EFmVfxMFkH DWOfNbxqMW9rRkAfL5v1tvTek/O+0RbmT+9iQHp7bcTvD+mjFFcxMQqlVX4Xwv9iav7x CCm3uUmc8bjqfubvE/kFgUYnlkLYVON1zPAQOCgZ9vtS20KgKI8gEuC95Kn+9dc0RFIW XurQ== 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=ETuS/ZDws2DT8KOkGYpc0M9sZjTmBC+kSqRug/kfs+s=; b=TxQ6dfI9FSvjNzukP4oo7G6+qaSYyhQ3qJCVUlDm7yZG/fQRqJ+k+m8TAzfx3kJOsf yL7JzQmlPrxU8GCAxf4M/CzbxDvseJ/umToxGAZzE3cvHwk+GyrZcxWr/TsdCTt+CEPV jQ0ckrTChN3wtPegYYlGrFAKHk6bvLcg33nbrP/bcIrjNo+gOzgLb+6oQHaGCuIr6miQ RmMtriNUEnQ4s1y4T27giInRuIBfTrVZcX1gKOk+G9Mkws1jjxxDPUDfmLsx2Wd9SbIF ICQHrJNlCc/ILoERTGiVGNJTVRpegsc8zwVKovge76/OLQj3bnlDoePJ+ZqvtV6Uv5hu KUPw== X-Gm-Message-State: ANoB5pndiGEwWI0E/B3lKuR6dZLYpDqEWbdxvdEwsDIq7FGCgXK/+7Q1 Q5qZt5FFncgh4K+Hgp4vnD8Bxg== X-Google-Smtp-Source: AA0mqf64vXtoeEO51fLM7U/uUO6E1N1fp6snZEZr9TUArEDNMuTKKrl8fBcglSl3treU193O+3ZE0w== X-Received: by 2002:a5d:4601:0:b0:241:bf7b:b2e5 with SMTP id t1-20020a5d4601000000b00241bf7bb2e5mr17704618wrq.626.1669312186316; Thu, 24 Nov 2022 09:49:46 -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.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Nov 2022 09:49:45 -0800 (PST) From: Srinivas Kandagatla To: gregkh@linuxfoundation.org Cc: linux-kernel@vger.kernel.org, Abel Vesa , Ola Jeppsson , Srinivas Kandagatla Subject: [PATCH 1/3] misc: fastrpc: Fix use-after-free and race in fastrpc_map_find Date: Thu, 24 Nov 2022 17:49:39 +0000 Message-Id: <20221124174941.418450-2-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: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Abel Vesa 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 Signed-off-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 7ff0b63c25e3..fc2173c59f8b 100644 --- a/drivers/misc/fastrpc.c +++ b/drivers/misc/fastrpc.c @@ -333,30 +333,31 @@ static void fastrpc_map_get(struct fastrpc_map *map) =20 =20 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 =3D fl->sctx; struct fastrpc_map *map =3D NULL; + int ret =3D -ENOENT; =20 - mutex_lock(&fl->mutex); + spin_lock(&fl->lock); list_for_each_entry(map, &fl->maps, node) { - if (map->fd =3D=3D fd) { - *ppmap =3D map; - mutex_unlock(&fl->mutex); - return 0; - } - } - mutex_unlock(&fl->mutex); - - return -ENOENT; -} + if (map->fd !=3D fd) + continue; =20 -static int fastrpc_map_find(struct fastrpc_user *fl, int fd, - struct fastrpc_map **ppmap) -{ - int ret =3D fastrpc_map_lookup(fl, fd, ppmap); + if (take_ref) { + ret =3D fastrpc_map_get(map); + if (ret) { + dev_dbg(sess->dev, "%s: Failed to get map fd=3D%d ret=3D%d\n", + __func__, fd, ret); + break; + } + } =20 - if (!ret) - fastrpc_map_get(*ppmap); + *ppmap =3D map; + ret =3D 0; + break; + } + spin_unlock(&fl->lock); =20 return ret; } @@ -703,7 +704,7 @@ static int fastrpc_map_create(struct fastrpc_user *fl, = int fd, struct fastrpc_map *map =3D NULL; int err =3D 0; =20 - if (!fastrpc_map_find(fl, fd, ppmap)) + if (!fastrpc_map_lookup(fl, fd, ppmap, true)) return 0; =20 map =3D kzalloc(sizeof(*map), GFP_KERNEL); @@ -1026,7 +1027,7 @@ static int fastrpc_put_args(struct fastrpc_invoke_ctx= *ctx, for (i =3D 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); } =20 --=20 2.25.1 From nobody Fri Apr 19 23:47:40 2024 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 39F7BC4332F for ; Thu, 24 Nov 2022 17:50:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229739AbiKXRuC (ORCPT ); Thu, 24 Nov 2022 12:50:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42938 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229504AbiKXRty (ORCPT ); Thu, 24 Nov 2022 12:49:54 -0500 Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9D50914A2EB for ; Thu, 24 Nov 2022 09:49:49 -0800 (PST) Received: by mail-wr1-x435.google.com with SMTP id x5so3459424wrt.7 for ; Thu, 24 Nov 2022 09:49:49 -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=WnrDbdRJkbksqZItXruR6kyL5+sqtubhQgH9dDxfehQ=; b=xoMkl3EeeMJyo2ElRyotEqbHF4q3toa1fIBVElAPlmfzfxIlN/Tv9nzrFEvqKil4U/ T75C7W15kUrJq4v5/Pp6sX+DBu/0JPFeMJE04JZ0B8+1qRZkB74QFhAfJu5fQpuMLsg2 NG15tCUEQrlJI+1nTxOou0+jKdYTrAdqzZJOEgc1r5sMNasBL/fbyfIRDVQtVyVrpxxp AmwszvnbzSEqM2HkdCprCvHXGKr8ZH4y7QeTTjJNynOcy3ezTJtZVTfc3lrNqUCFSMoF l765G/f21FSHhFSMS984AihwFVeebbBXbkIfHYV59Lch5+Gn3B6BUAbGu5rLbXl1F7wm D8jQ== 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=WnrDbdRJkbksqZItXruR6kyL5+sqtubhQgH9dDxfehQ=; b=mnVCSLw8rQ4keoJMScOGohluU/WMOlRXl94NfnuX/qjjG4IG+ea7+dHwstu+R89m5g XIZ/HuwhlWqpIJ/WqKavG/oDFJaa3U+/9ztQ57uTuth4qcOWXO2AOLhLK4PJrWHghp0g 3yMosvXIZYc1eL6CoxWTro5ZCZ9OFKCyUwiwQk1IZxzoVTvG3/iSl0P9l5ciHKYciWBF tXP/4mkffY2lScYtZ5V1vuzscssGL3vrxE3RXEsRBekyBeIBGyjg3ViLiAdkcL5ippnc JinTb/0S9KKWiZ1nhD2HjHrNcrx9v5cooDvtimP6Y827ZH/SsabGGNTcXTUWCDISyqVf io6Q== X-Gm-Message-State: ANoB5pnDhMnArzJ2dkWuhLu7wk5lvFZ+Lna7QUoG56Ye/D5WFI8b9dkx t2MKxKhjb5g65/nXw0TMoX1dow== X-Google-Smtp-Source: AA0mqf6zIsKV2cNw5MNIc5hWrXSQbLgOgCA06wPURrAvGy4D6Zu6D4mui3Y6CpYaH4oDMecauRctqw== X-Received: by 2002:a5d:6b46:0:b0:236:56a6:823e with SMTP id x6-20020a5d6b46000000b0023656a6823emr20762122wrw.495.1669312188150; Thu, 24 Nov 2022 09:49:48 -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.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Nov 2022 09:49:47 -0800 (PST) From: Srinivas Kandagatla To: gregkh@linuxfoundation.org Cc: linux-kernel@vger.kernel.org, Abel Vesa , Ola Jeppsson , Srinivas Kandagatla Subject: [PATCH 2/3] misc: fastrpc: Don't remove map on creater_process and device_release Date: Thu, 24 Nov 2022 17:49:40 +0000 Message-Id: <20221124174941.418450-3-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: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Abel Vesa 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: b49f6d83e290 ("misc: fastrpc: Fix a possible double free") Co-developed-by: Ola Jeppsson Signed-off-by: Ola Jeppsson Signed-off-by: Abel Vesa Signed-off-by: Srinivas Kandagatla --- 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 fc2173c59f8b..9ecbf673d321 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); } =20 + if (map->fl) { + spin_lock(&map->fl->lock); + list_del(&map->node); + spin_unlock(&map->fl->lock); + map->fl =3D NULL; + } + kfree(map); } =20 @@ -1266,12 +1273,7 @@ static int fastrpc_init_create_process(struct fastrp= c_user *fl, fl->init_mem =3D 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); =20 @@ -1347,10 +1349,8 @@ static int fastrpc_device_release(struct inode *inod= e, struct file *file) fastrpc_context_put(ctx); } =20 - 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); - } =20 list_for_each_entry_safe(buf, b, &fl->mmaps, node) { list_del(&buf->node); --=20 2.25.1 From nobody Fri Apr 19 23:47:40 2024 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 91263C433FE for ; Thu, 24 Nov 2022 17:50:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229755AbiKXRuF (ORCPT ); Thu, 24 Nov 2022 12:50:05 -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-Google-Smtp-Source: AA0mqf5LwL8aKM4fi/jEURxCd0yPCPgYlEa/IAxrEh76YlqGn6ZwasvivmbqpNeXhQ3W+P8eIjB9/w== 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: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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); } =20 -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; } =20 =20 --=20 2.25.1