Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp2128385rwe; Fri, 2 Sep 2022 08:56:20 -0700 (PDT) X-Google-Smtp-Source: AA6agR5/rYOV8TLvb1t0sPj0D7kUdD8JRWpMolzVSBsNXhHn2ou5jewwHiArlqop5aJNQk+Nz++W X-Received: by 2002:a17:907:7f8b:b0:73d:6f4f:30f7 with SMTP id qk11-20020a1709077f8b00b0073d6f4f30f7mr27423230ejc.323.1662134180613; Fri, 02 Sep 2022 08:56:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662134180; cv=none; d=google.com; s=arc-20160816; b=S1GWEot0Rccf7jrT2LBcPWXIVN44EMTbpw1hjZfrwG5FyHMiWITaRw4JRKfVeDpc2C ag4gpJB3Oob1+eTLpWHAwjBPU936l9PhaG/94AncGDg54oKFh5UFk8hpQ6IYatNJOhZm hGu8BmxaoqsdNWiwmBhQq6lHgMA1Zg998Uhdkyr+r2uhR/vvzdAFvcggsED9gpfGeQEG 9aCkjf4pu5c0U+fFjLMENNyA68SoaqTShVeSiKGb0PXUVqVqePVcJsw0LwdygYY5nfPi 7EXvvgkgIA1uN/z6RKpQB0f64wepiay7NAO8rFDkZ9o/eJU2t9/eNrIXkwu/RNxBZz+N XeFg== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=7Woa6saHlcsqIiHSXMMSyYnI6pEpe3JjUt9ToPHjSVc=; b=DIJ9Y7iNfWylQxWJwh/v3gL0hhzPB5/edmb26/8fOxaIkdYmb9CAyFYUE/jJi8XwLX EIBMgS5WFqqVXXBWPaj/tB8MztGnpwiOqtmNaRkVpfFDPhd5YaCY5MbNcVpNBb+ws1CT wI9syzL8xA53rSxzyxaY3GOxtaILd8na7zH02+JF3dyqgYpBW2Gi7IHNhFsuslyut9PG pO9Ipkjis0unTuSXhonsQYwvS/HFdXfXq3aozFfsNVNWB5xBUnMmbLa3dfu+BwdWk6v6 jRooEmQddNasAaueCXnpZF5VVTydUdsFvXIDQpfYkBTjiICLSRYSUHq0PKjkn1/MTWPp bGWw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=zvTQisGu; 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 gb22-20020a170907961600b0073dc770cdc9si2438931ejc.756.2022.09.02.08.55.53; Fri, 02 Sep 2022 08:56:20 -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=zvTQisGu; 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 S237003AbiIBPcq (ORCPT + 99 others); Fri, 2 Sep 2022 11:32:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44620 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236582AbiIBPcY (ORCPT ); Fri, 2 Sep 2022 11:32:24 -0400 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1D5D829831 for ; Fri, 2 Sep 2022 08:15:00 -0700 (PDT) Received: by mail-wr1-x42e.google.com with SMTP id u17so2736117wrp.3 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=vZiAmbYGEIv/aYR62A/SM92n7RkKIAaLeo1GF5puCTlszgXlTBfNm0L3gQh9ORAI9O KleJCOdFQs5/8jMsrG964dCUjWbZOmT5IJ1ELzUTMCCMqpLj1kXXxTfiqtjLZBCa4rJS /j6geoKO1HqPQ9XSM5L0+lYSZetzZyhIAHFtRMo8WfZiAYAe2WeeRAGb8hJ9qu66KaAN sUF3cndNYIadeygVoAWA5Vy6G1f0ywa2kG4iMnNhIZ5is/4FSq35WnFF3yeilI7zVUsY iwL+FEG3h1dy0H1JsYyj0XaQ7BUcIc0eSJKoe/AgXZmfuDkoS3YdeqAwkFUCtiHncjdD kKWw== X-Gm-Message-State: ACgBeo0VsTE24V2agAe9XBpeAqah0BEY4SiGa5GBWXYcYzMakK9YTgIm Ji71gypbB0ghskmmHoySTrWQ9w== 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 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=unavailable 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 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 --- 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); } -- 2.34.1