From patchwork Mon Feb 10 10:51:55 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 863952 Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 604DE1CDA14 for ; Mon, 10 Feb 2025 10:52:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739184730; cv=none; b=V7PHN7m/VRQwyHrY7wAfB9ktpEozah/QCKLh18fp/2wiMZD71O/A0GXXpuzER2hl0hIi7ubwUMybvaPenvW2BlvvXPVQRzYzEEsxAX3ueqbh3M08ryLm18oYwG8uJ0qSvcmGDvU4KEavdUCc7nnpAdZWSQdgVT2MJJmKQVVt5xc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739184730; c=relaxed/simple; bh=RfHBELWftEOI6aduadNpWSnZVyvCMZTRiybMbk/nsKY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=n5NqBqzqs6V1t5fyyBfg6zU5mWrVrTE01Tcu2c2JNL+XGs4uDx79hPSxFvO7ftz8Us5W4Vg9Bfp3Ox83Hu3F59tGKQOvs5uHN5u4hNizVELBXOKApdfE7k5QvqkfN0jc3KaKD7lutnRB6A8aQDV1oVlhWy/r4X1HBT16AahpcE4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl; spf=none smtp.mailfrom=bgdev.pl; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b=wI3pd9px; arc=none smtp.client-ip=209.85.128.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b="wI3pd9px" Received: by mail-wm1-f41.google.com with SMTP id 5b1f17b1804b1-439350f1a0bso9116455e9.0 for ; Mon, 10 Feb 2025 02:52:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1739184727; x=1739789527; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=oe6RHeWjCutLo5iDZJ7f7rTUbrd3skJS2x0XvYoqI4M=; b=wI3pd9pxnebMqrq3eY4jY87tbCb9REOQW/vNTZc9FtYS1LToJxqwnhEtSxeF4C2QnR A+jwFiRgmUK3DQywWk//dqo+pOD1On08wVDLAwr8llvbbimo9G+9fxY4NjhdZUzChch7 KyD2WwgS/FpZqLcAAkjAIMzVuyk9Fy3FkRPIAGKUwSVoM4gHOaHuDSEk3KalEmYEo61m LbmlySyey2wnVyzshKUEfFKAeQ4Q9KmUlv/sOx/gLeADFzajwLk+SsbJwvTGeJTCO0DY 7el3Q3Uz90HBteyuNHDAaJXtAlzEqKcVJKoVcjfLTCXpikj0Xi9+yu2KJth7cRZjZESd FhfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739184727; x=1739789527; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=oe6RHeWjCutLo5iDZJ7f7rTUbrd3skJS2x0XvYoqI4M=; b=oWq0YxsZ3+QorA39TPzNX/l8E/YvKht6X91/p+r2l6fnAJX0VeQX6z96cG0ycSkyvA cooLjp1UcfT8Zi244XMNgLvkoftk4NLixXxGVz499JDk+ms+hG54alQb9S4Ax2y+1Cq3 1aFJS94QYwixyI3i0UCsHoGhcGlRFDMEIsLMLeZ/87bl7Dl+DuzhRfOFWaYtk+5Lc/i8 OnLZ6yMHRxiBOJzio5Fc00zw1jYKJctWq4GS/QrtVy6KOg02SX3494PjTZDnHH4Uc2LK ByI82LIij7SPmpWFzz5LHv5WNfuTI9wkEd9Ogvdb6W3iGSvSK6185sD5ughhzlSynmNN 8fMA== X-Gm-Message-State: AOJu0Ywlv7DCuLm+HVnxc2z2uvlFfDItgrij6cjCp5Cx0RExUg9suEog b8asQXFnXl4BCeDXv91rEpgD7RK7JMvJyvh5tO0Up9dfmHPjONL0yBAPdF6xOhk= X-Gm-Gg: ASbGncvBS18w6UlFuEEXva5Ok4NqhkBcUv1jYcXCpavd+43vms4VCKeiLC0olzUb8Mk hPe4ITov8UcUYYf2OdnTq6S1/myQy0kSDUWx2ZUyEZFKlqM/UqBZbNCIIgaXHYXoPyK4NmttBHJ kHiiw1/hCGS963MXU5qW1g4F7ZoPbNYspw+CvgT+yvRyz4RY3Z0Ev5lECP+BXI9FzLRGjKCXMwM uywX/WKDvmJBYH/Fe8hgVUx7CVlQDMEHgBQ96S+Ta3G4i1MhoEMVLRjNrsEZqck0QpLgwkOJNIq 2x5W0Hiu X-Google-Smtp-Source: AGHT+IGBAZdBYR3TYPZOFingx6IDVarvNd1xtuG26gadPMUlJJHpUqSLxIS+6zs0KMce2R6DTrtQJg== X-Received: by 2002:a05:600c:3c96:b0:436:1b86:f05 with SMTP id 5b1f17b1804b1-439255b81e1mr94425715e9.11.1739184726533; Mon, 10 Feb 2025 02:52:06 -0800 (PST) Received: from [127.0.1.1] ([2a01:cb1d:dc:7e00:f3e9:47d0:94c6:777f]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38dc53ae9c0sm10557919f8f.101.2025.02.10.02.52.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2025 02:52:05 -0800 (PST) From: Bartosz Golaszewski Date: Mon, 10 Feb 2025 11:51:55 +0100 Subject: [PATCH 1/8] gpiolib: check the return value of gpio_chip::get_direction() Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250210-gpio-sanitize-retvals-v1-1-12ea88506cb2@linaro.org> References: <20250210-gpio-sanitize-retvals-v1-0-12ea88506cb2@linaro.org> In-Reply-To: <20250210-gpio-sanitize-retvals-v1-0-12ea88506cb2@linaro.org> To: Linus Walleij , Bartosz Golaszewski Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski , stable@vger.kernel.org X-Mailer: b4 0.14.1 X-Developer-Signature: v=1; a=openpgp-sha256; l=2846; i=bartosz.golaszewski@linaro.org; h=from:subject:message-id; bh=lpE55aITs3TR0edspV0JF4dj9P5jKU0BTQ+eUb+VBxg=; b=owEBbQKS/ZANAwAKARGnLqAUcddyAcsmYgBnqdpSfnhhEUo6hT94zgAJgM2xjsjhsYO1vA+T0 5EoGpZakeGJAjMEAAEKAB0WIQQWnetsC8PEYBPSx58Rpy6gFHHXcgUCZ6naUgAKCRARpy6gFHHX cplCEAC9jg6cqsozxgAdfJBzUaYa+ZLzYNwuow+HZhV8fZDTTaaAyTUsXwiBvRP+WiPTHDehfMA GTEnGaHrEsFimr6T9IHElWUzybgmTrQRTW2MIfMUjxubcxckxHyPhJw97ErCdtUhfUll0OdXY+T ONyN3rJdZkz24OqcN422aTAdH4hKLyMfnv/w0s6x1K/gzsXlf6NyHmDDJh80JRkBMwzEsL6YbO9 B65JKmxa0gBdnI36HYvYDHPs9QZAMlae0WzAVFzHQRmrNj+pdk932spDIFUwyLCnZC1Ar23KCda nbCd0PS4/DgSK1OaEgMamD7jguHCwyF7LIzDou2alhSCWukW4KjO714XP9w7gBWN32YaIibeUyY BRX9blIl4eQGtj78PdDvri2lAVfEMT/4rutTNEruVsle+9EPDKf0RsqlnESe4ooJyD3qQZBgo1+ Gt1eRnBrGe64Id35RqFhIjNgeUroDx7+kGRocsobPWoonDCs9FzDKNiNEpqJEoK5VGlQrZq4+Kf 1ANNV39UumMhSktuLvFaYeAXjeDgn5ceL3WtGchzDRLIbkEhKuHrvn+PoKWQPGnnWcQG7uPH5xq j3SFVEy6JfpgAbpdwr1FHdgvOpP/3jLtNRQHVZ42v2G0mi29Z+9X5cPEDcgXYdwNsrZc1Y5hViP xYbtXvwIj1LkIIQ== X-Developer-Key: i=bartosz.golaszewski@linaro.org; a=openpgp; fpr=169DEB6C0BC3C46013D2C79F11A72EA01471D772 From: Bartosz Golaszewski As per the API contract - gpio_chip::get_direction() may fail and return a negative error number. However, we treat it as if it always returned 0 or 1. Check the return value of the callback and propagate the error number up the stack. Cc: stable@vger.kernel.org Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpiolib.c | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 679ed764cb14..5d3774dc748b 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -1057,8 +1057,11 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, desc->gdev = gdev; if (gc->get_direction && gpiochip_line_is_valid(gc, desc_index)) { - assign_bit(FLAG_IS_OUT, - &desc->flags, !gc->get_direction(gc, desc_index)); + ret = gc->get_direction(gc, desc_index); + if (ret < 0) + goto err_cleanup_desc_srcu; + + assign_bit(FLAG_IS_OUT, &desc->flags, !ret); } else { assign_bit(FLAG_IS_OUT, &desc->flags, !gc->direction_input); @@ -2728,13 +2731,18 @@ int gpiod_direction_input_nonotify(struct gpio_desc *desc) if (guard.gc->direction_input) { ret = guard.gc->direction_input(guard.gc, gpio_chip_hwgpio(desc)); - } else if (guard.gc->get_direction && - (guard.gc->get_direction(guard.gc, - gpio_chip_hwgpio(desc)) != 1)) { - gpiod_warn(desc, - "%s: missing direction_input() operation and line is output\n", - __func__); - return -EIO; + } else if (guard.gc->get_direction) { + ret = guard.gc->get_direction(guard.gc, + gpio_chip_hwgpio(desc)); + if (ret < 0) + return ret; + + if (ret != GPIO_LINE_DIRECTION_IN) { + gpiod_warn(desc, + "%s: missing direction_input() operation and line is output\n", + __func__); + return -EIO; + } } if (ret == 0) { clear_bit(FLAG_IS_OUT, &desc->flags); @@ -2771,12 +2779,18 @@ static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value) gpio_chip_hwgpio(desc), val); } else { /* Check that we are in output mode if we can */ - if (guard.gc->get_direction && - guard.gc->get_direction(guard.gc, gpio_chip_hwgpio(desc))) { - gpiod_warn(desc, - "%s: missing direction_output() operation\n", - __func__); - return -EIO; + if (guard.gc->get_direction) { + ret = guard.gc->get_direction(guard.gc, + gpio_chip_hwgpio(desc)); + if (ret < 0) + return ret; + + if (ret != GPIO_LINE_DIRECTION_OUT) { + gpiod_warn(desc, + "%s: missing direction_output() operation\n", + __func__); + return -EIO; + } } /* * If we can't actively set the direction, we are some From patchwork Mon Feb 10 10:51:56 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 864247 Received: from mail-wm1-f44.google.com (mail-wm1-f44.google.com [209.85.128.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6E8201D47C7 for ; Mon, 10 Feb 2025 10:52:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739184731; cv=none; b=qeMvom34o0exKvvnJ82sHfQ2Azai10Mts+fyGmBmRhX6Egio5dxlHOHAx5cLOlPXQYnCTDhMG3YoadTkHCkXXGDeOlGWBJqEiNkWSIo6tcZ80ZfxqS7wmNEcdYNuRu3/ETLKXqWoqr+bc+sMgGDfJqERPmuiYFZksNT5WMVPAG4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739184731; c=relaxed/simple; bh=VEYyCI/WQWt7FUs1FcU8Tp3ZXokrKORavVAubjuZUu8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=dlMIPxWL9spfMaFzsGuem92W6FxYdeg6BnVERnsZuRlq52YMiKgSRRNfo5q19QQ07O7N09I01AnDfcQzElDI4ddfYvpXvsmr1AtbEOuZ8eK0mDH2CsS5KqXE1xxoLrjWQUYR9XProzbM4+0CEY0jkfCUB67Xm8ONZZpvXCcpuZs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl; spf=none smtp.mailfrom=bgdev.pl; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b=hlEyuhNk; arc=none smtp.client-ip=209.85.128.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b="hlEyuhNk" Received: by mail-wm1-f44.google.com with SMTP id 5b1f17b1804b1-4393f6a2c1bso5679955e9.1 for ; Mon, 10 Feb 2025 02:52:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1739184728; x=1739789528; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=sO4RL0P3MsiqhqZaJGlhjjTXZ+rOkejLGclJdeKjOgk=; b=hlEyuhNkkSn+szlvX30e26u4pl5dtDJsgFq79gdyeP8LiSN8Mwy3EdQEL5FtYe5w2M kIfS9Xd5hgnIJ8Odax6F4J7LJ3CpZThBwTed0Jwx7z3/bRGORCEQDeACVuJsDzV6N6Rt Ro1PrBPv/6JYi71hi+XpZVwpDgXoT1CTmFSa6rjHX0ffR105u11wGJ0LuDkRSdDx9fCr OPjWhyxOhu86sGKd5TBpQntsZRoWLnUHdcx8Nj1alf8nPgzUYyXmyyqJLEKu75KvdhsF smCqW9OC6wJ8P8nEx6Kt3ZiXJw5WEpubNKV8fqEm2TX5yyAYC6nF7sM8Nc9TEh9EIwBm n5ug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739184728; x=1739789528; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=sO4RL0P3MsiqhqZaJGlhjjTXZ+rOkejLGclJdeKjOgk=; b=AE671gOVZlZ5/pK4q8rV9vlYlnRm431Dd3oENRIos385n5ekwQMbX0sojnddyJOO6n OsVkNeRANvxmRH/MtVIzzqK9MvC1lcOdtWrV773uJqf21P4sY6kp8ybDUxSfE/b4qzOF rPp1ihrEH7l5WSbPJ5kuzZ3GeLJoKVEJuY7shQttwVWMLgSdrKPyVeXPmPm7FAdNpVTK A+y0VSVf1oPPwJapZrY/i7FbN+sKdW9e7MuhvxDebbped7AMPrO7YTK8u+1pODX5QWie qa5EWo7MW/hhu3c9NoztVpnWxRa8Up8dAMKopcj374VQcRl+hezmQ8nx+11sPjm9LWDu sdzQ== X-Gm-Message-State: AOJu0YwRt6YFI1tfvF/YEBo7Wexa5rRC+agTWmAQqkF6M2Bcg27BZMnw qxxYML22y1RQVagnSkwqgkSdT8ZtC2M3WEqFph6k9dtqU/1jEv+679L9NnVtID4= X-Gm-Gg: ASbGncuWL6gcHULudIJ94+dDI4KmNRU7S/5P2Nvt8w7dFXpV1m8jC3yO5qSkLywiz/w 7sVcLtvl3LQkYiRN5ywV/4VSv4QoG+sQe1c/5JD330HGjRzQ+s0AMl153QmkGJtataLndST90u+ JWnb+WZELRC6V0WdktmHAvh8kfa+tqwQ6X/EF3orAwr1FPgz5E6R4/B/ob1QOD4eMj1lXMkDmP6 8Vovp3LPnU2AJnGpRJmDjuPV6zQrNAOYosUfXSh0Dad1ZHSnwPSowKunK08p8a8UZlP9ZAFvFEd d4LOAX3/ X-Google-Smtp-Source: AGHT+IG9w8+Y2BmWt3Wy2zCugo1yyVPEoCM54U4nHTtttPaMyB9ZvLuV+Rm+5tQM3AD5nQFQQRIc0w== X-Received: by 2002:a05:600c:1e1c:b0:434:f1bd:1e40 with SMTP id 5b1f17b1804b1-439255514c0mr84862755e9.6.1739184727605; Mon, 10 Feb 2025 02:52:07 -0800 (PST) Received: from [127.0.1.1] ([2a01:cb1d:dc:7e00:f3e9:47d0:94c6:777f]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38dc53ae9c0sm10557919f8f.101.2025.02.10.02.52.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2025 02:52:06 -0800 (PST) From: Bartosz Golaszewski Date: Mon, 10 Feb 2025 11:51:56 +0100 Subject: [PATCH 2/8] gpiolib: sanitize the return value of gpio_chip::request() Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250210-gpio-sanitize-retvals-v1-2-12ea88506cb2@linaro.org> References: <20250210-gpio-sanitize-retvals-v1-0-12ea88506cb2@linaro.org> In-Reply-To: <20250210-gpio-sanitize-retvals-v1-0-12ea88506cb2@linaro.org> To: Linus Walleij , Bartosz Golaszewski Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski X-Mailer: b4 0.14.1 X-Developer-Signature: v=1; a=openpgp-sha256; l=1657; i=bartosz.golaszewski@linaro.org; h=from:subject:message-id; bh=xkoYB7QevhvR3b+N2NNvGIdS2HFsaFzerPPF2/0s9uc=; b=owEBbQKS/ZANAwAKARGnLqAUcddyAcsmYgBnqdpSeBElLfXXQ84Z9WEjnwnVZFBwpXL6a3buq qxEtPp/EGGJAjMEAAEKAB0WIQQWnetsC8PEYBPSx58Rpy6gFHHXcgUCZ6naUgAKCRARpy6gFHHX cttFD/9KMeATzvVcS8fkNWtmLZqCca0VOv3FIImFijcRAY1VSscZnpcKmcSFogZrPYFVPS3AwaT UmELIhw2EbSWGCN4YgmeuEcf++8V6C5MKxwU7qGmq+vPKUeySCdoc30XQuV1212EJCK2XEwDHu9 SDU5bB56HMMj0rZA43rwIUxps7o861ZL+8LPTAYakQEYQE/YhztjoYlncE7+iXlBLkfS+16eIh1 NBttreNhs0cRQS+/TfSXEBjlnMC1II6mYNCs4716Oh9jfylxLrPvdPQuwrjclzQvb9V5dHfQAtx rutAgVd/esaXieQjwPHHSGIwS8qAPQa9ZLHRE1Vsg2VfuCd9jhmjpiugDzpsZ7/LJOy1dYOuIB8 ga9X9D8Ft2x2LeBMQlTo7GYROzRXKLUjvMCrPHIsDfGXhgLOzqLCVtYHCqzMXjlWLIm7wEppYDf 1wXJg2IP4MnoCBnppWRe6wuN4UJaHMdkPJJvXLut2xzQ8Ql+uuCaIRabQdpBm0/1hYl2LTvMr5l JcOM7Z7KYcXYAI8Q/m9lDZINgILVtMZjZ00q6acCGrrGBreJ/yQofrY20Ag+MrgGQHlUhu9IX8D FN2SaOCoE4xPafKxDBKjJklp2p7UB7/YlyC87I0uLbLe81cP8LuZVRIyPxsN/t7OCY+SM8cibmv i+jiWfu5itVunew== X-Developer-Key: i=bartosz.golaszewski@linaro.org; a=openpgp; fpr=169DEB6C0BC3C46013D2C79F11A72EA01471D772 From: Bartosz Golaszewski The return value of the request() callback may be propagated to user-space. If a bad driver returns a positive number, it may confuse user programs. Tighten the API contract and check for positive numbers returned by GPIO controllers. Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpiolib.c | 2 ++ include/linux/gpio/driver.h | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 5d3774dc748b..42625da7e797 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -2331,6 +2331,8 @@ static int gpiod_request_commit(struct gpio_desc *desc, const char *label) ret = guard.gc->request(guard.gc, offset); else ret = -EINVAL; + if (ret > 0) + ret = -EBADE; if (ret) goto out_clear_bit; } diff --git a/include/linux/gpio/driver.h b/include/linux/gpio/driver.h index 2dd7cb9cc270..26fcddcb74b8 100644 --- a/include/linux/gpio/driver.h +++ b/include/linux/gpio/driver.h @@ -328,7 +328,8 @@ struct gpio_irq_chip { * @fwnode: optional fwnode providing this controller's properties * @owner: helps prevent removal of modules exporting active GPIOs * @request: optional hook for chip-specific activation, such as - * enabling module power and clock; may sleep + * enabling module power and clock; may sleep; must return 0 on success + * or negative error number on failure * @free: optional hook for chip-specific deactivation, such as * disabling module power and clock; may sleep * @get_direction: returns direction for signal "offset", 0=out, 1=in, From patchwork Mon Feb 10 10:51:57 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 863951 Received: from mail-wr1-f50.google.com (mail-wr1-f50.google.com [209.85.221.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 46A311DE3B8 for ; Mon, 10 Feb 2025 10:52:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739184731; cv=none; b=XUQobf5fdLrQW/dj7mE8TlRGiDm9tkdyUEJtVrRox3GmRQX9iqAlZU+2YQUxNwVWdSa0urwxG8zrC+97pkHVYEF60wXQ9peikhxsnNvIy0SV6vxNbVqdfKaBEotA1eh8dwiob9oTt/q7jWnUa1wd0FpMxqXDQ8j6c9TgJa1lXyk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739184731; c=relaxed/simple; bh=q+8h6LGXedGssIbDpk6doFlGaWnQknR2RXXNfrg5t/8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=AiGDfQm1VNxYO33uCfgntevYQOMRzw1X4m5hbbXZWJz/jfI4MLT8y6DKqsAjYRn0L/dBabNmUMYDUqoDFZ+70EjIsatZ1blY47tzr22saPaUcxEZQbzhjTL3V1xOiX+dgD+dgbOTfkCaqB1Sf0UL5dEePEbwsKJ2pVMTYZ64SUk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl; spf=none smtp.mailfrom=bgdev.pl; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b=2Lh0H0hF; arc=none smtp.client-ip=209.85.221.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b="2Lh0H0hF" Received: by mail-wr1-f50.google.com with SMTP id ffacd0b85a97d-38dc5b8ed86so1693057f8f.1 for ; Mon, 10 Feb 2025 02:52:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1739184728; x=1739789528; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=m3TsbNFVHS/IC4iUVVEnnqOzTVBqUgDCo4J9C33YERY=; b=2Lh0H0hFqNsL+IsX4ev0uq8p3xK32oNhnIuqY0dHMMfo2xY3u0SgU9fa7W/Sgav9V+ rMYlLpM9R4ijkezIu8uRv3Lf/vl6SF5k7yc/ZIqg6GwuR9V+R3YHFm4ysqERDtQ6gBe9 y6hIfBc05kUfahH/cGv8ne42/6THsAjDEwPDAHKt4lb5p8QokzgiLrKz6MgJpQCxTqxg bn/RU7WaCqKoZoDhDwFps2P6dREovmuX+d344vx5+XsPABXmKFsz+ATRr6SBErGQklWE ogGAZD2Pqs4pbSq8u+MapS/j/Bkz1KQIIx7kHy/BQtZ7M8T8Ur4sH3mUlWsrK+cBbTN5 ZUag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739184728; x=1739789528; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=m3TsbNFVHS/IC4iUVVEnnqOzTVBqUgDCo4J9C33YERY=; b=tCHul8mCpjd5kkloeMvKk9DgFL6Pf2qzs0k9eDTHZOphxBuZLWdeVtYwpR5OFaH9XT USPwhKABvc//SDITEGtkV+XgUlERnkru0nD5QhMeE7GB8NA6tuPWL/eCo27u75CRUrXX M3TVFOkYA5NIzsmv06TgROPPDNb0JTDa7iLUyh+gyPuUnHJlzDCz+BSbmKvkz0rQlZQk xnOMqPM+48DmoAZNcrmyvw5jOw9dhlfjHAnUQ05iaJZ4r3SDvIZzhWBzGggRD6yjP1Rk Xc/Iwjkvf7GxaKeP9fha7OVaUbhqVeA6XBBsUZiGT/LwTYEtIJC7JQ58gCao04ev5usV liDA== X-Gm-Message-State: AOJu0Yz0HXOb/H5H9ImqXLOh4AdowCCqvg7f0JFljavpJz91Jf9I4/YD /rLYdKF05JIMm4sYFXEon/kD2YJSP6cZxRtaSrudCFH+z+sfo3kdx/yAnMEuH2o= X-Gm-Gg: ASbGncsHYY0ztpLMGGOIH9YNrK1+D7Rgm3mJ2d973tMKixbNqnYJE6uLaavqDFho4Po 8/3V22BuBqxWg07COoC0EXi5irTE+UN38vJCx7SzWNLsa2O8Jd95xDR8s5fTQE/nXp7gcrO5NGk 8vR2KACFkojK0gxp6bOT4Oa7h+Ywo4GyasGl5mM/u1wq5ltDTicYSqHQGd/s48PmyskagIbL7zq lAWyaF8ee/HwEqD2J9Vmfu3Eyd1GKvYmLzg7sW9+9Her2fWFNaBmhFp1SCK5kASjtyHzZGGEnV/ +DXeDhey X-Google-Smtp-Source: AGHT+IFx774tEdrjGX+K2g7goOYqdE3lgbg6MjFHO4VWjKPEXooaMul9dyJFxepOkUugTJxkMdbmeQ== X-Received: by 2002:a05:6000:1448:b0:385:fd24:3303 with SMTP id ffacd0b85a97d-38dc8920858mr10829010f8f.0.1739184728588; Mon, 10 Feb 2025 02:52:08 -0800 (PST) Received: from [127.0.1.1] ([2a01:cb1d:dc:7e00:f3e9:47d0:94c6:777f]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38dc53ae9c0sm10557919f8f.101.2025.02.10.02.52.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2025 02:52:07 -0800 (PST) From: Bartosz Golaszewski Date: Mon, 10 Feb 2025 11:51:57 +0100 Subject: [PATCH 3/8] gpiolib: sanitize the return value of gpio_chip::set_config() Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250210-gpio-sanitize-retvals-v1-3-12ea88506cb2@linaro.org> References: <20250210-gpio-sanitize-retvals-v1-0-12ea88506cb2@linaro.org> In-Reply-To: <20250210-gpio-sanitize-retvals-v1-0-12ea88506cb2@linaro.org> To: Linus Walleij , Bartosz Golaszewski Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski X-Mailer: b4 0.14.1 X-Developer-Signature: v=1; a=openpgp-sha256; l=1734; i=bartosz.golaszewski@linaro.org; h=from:subject:message-id; bh=HDFuarshvnhtSITkzvfZTJ5ikvSyc/L6lYIYqosoVLU=; b=owEBbQKS/ZANAwAKARGnLqAUcddyAcsmYgBnqdpSf217Wf1JeG6YPA/mU60jZhlxBYHM2RnXr T14M8fOvfyJAjMEAAEKAB0WIQQWnetsC8PEYBPSx58Rpy6gFHHXcgUCZ6naUgAKCRARpy6gFHHX cj2JD/9L0JUUmq+8J51P5JZ0Rt3kj/bnafEDEzgrQ/gMTholFSRkpee9phHqoxAIAt1OoznT/mk uoWoc/iTpuUDZm2havuWGOmgrkJIuBFWMSko4N5oFTiTTRkmO/0GCFLqFXdu1uFxo3kpSIReBuk SwyZwypfcl0gu6/CB9xiec1858rYV/6dlHqIkPaPZAJjpMc49g93UEWvK9wQJgeGDF4U1UwV0Er +Oo/jNVyyqIbFyYiRMUetm06idm4R+wcPlBbJuU5kJUwAE2XYOKVIdXfGiegVcpcRQUOvuXTk+v 7rGsBk2RgVHWSQaJxWZJe/BLcIhZGKP8zb0HL+YN13Ph9YMtG1xzF1HICbHDJV+NNygAoCSjsxS cT7fCCmXo77ZyQnBpXTENmgzGzc8gn9WJWwsGd+GOoj70+nVxOCjv4QEjgnFo+3RfvvyXiYNEbR giG8wHb1JR2mhUxHuy4hD7LRgHLeQVQ7mOlPoiX8tg3oEk6M2UQ9btdzPgE3DMeBRqSnj9Am5+h FaASZH5TX81qcRHMwPNUSZiSuB3QTGL9kd1mlCH1fCD/K6ZtsveRso0PZaPkO7ikmNdC7sQeUnU RuS8Qbsvq26JW0rgI5bDBnTI4tlo3H16CgL/kuuOs3xlXD+deznNZxcFJjCa2JZFEtpAG42kahx cEzefhqzQ19HmmA== X-Developer-Key: i=bartosz.golaszewski@linaro.org; a=openpgp; fpr=169DEB6C0BC3C46013D2C79F11A72EA01471D772 From: Bartosz Golaszewski The return value of the set_config() callback may be propagated to user-space. If a bad driver returns a positive number, it may confuse user programs. Tighten the API contract and check for positive numbers returned by GPIO controllers. Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpiolib.c | 3 +++ include/linux/gpio/driver.h | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 42625da7e797..95ea300da109 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -2575,6 +2575,9 @@ int gpio_do_set_config(struct gpio_desc *desc, unsigned long config) return -ENOTSUPP; ret = guard.gc->set_config(guard.gc, gpio_chip_hwgpio(desc), config); + if (ret > 0) + ret = -EBADE; + #ifdef CONFIG_GPIO_CDEV /* * Special case - if we're setting debounce period, we need to store diff --git a/include/linux/gpio/driver.h b/include/linux/gpio/driver.h index 26fcddcb74b8..81c06fc8dcda 100644 --- a/include/linux/gpio/driver.h +++ b/include/linux/gpio/driver.h @@ -348,7 +348,8 @@ struct gpio_irq_chip { * @set: assigns output value for signal "offset" * @set_multiple: assigns output values for multiple signals defined by "mask" * @set_config: optional hook for all kinds of settings. Uses the same - * packed config format as generic pinconf. + * packed config format as generic pinconf. Must return 0 on success and + * a negative error number on failure. * @to_irq: optional hook supporting non-static gpiod_to_irq() mappings; * implementation may not sleep * @dbg_show: optional routine to show contents in debugfs; default code From patchwork Mon Feb 10 10:51:58 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 864246 Received: from mail-wr1-f53.google.com (mail-wr1-f53.google.com [209.85.221.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6D4B21DED71 for ; Mon, 10 Feb 2025 10:52:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739184733; cv=none; b=a0YP3wzUTAz0tdo8xCDZATBbtv5kTMKjTFf6VXgivcVwbPnkeyDT4NqBweq19Q7zYo0z+5WvWWLk/8r/b7U4SEput5gg/dsP1eXRLlfzM/Ra7d5u3XBbyJQ+yD22Mj7mjIoGqBLYPupjETQVGUwwm0BGqi2IGqKEZSa7HxLPfEE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739184733; c=relaxed/simple; bh=Gx/ZBLRyHxi/oAJGICkvIOrL3DfPBNr8PZ8qnHSfnJI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=XNnRoY66rd4JHL1GBh7PzdxjDXsK782N20fks5wt7VBYV3qrsJ/mRNhc6U6A1D2K+9ofT7rqo4r55mFvSA4NRQ1MeVuU82Y/Ff088Umyd7XbZwMZaKnEYaOSueKz6lukHwGNtXGdnzKZ3d3PYM4fG4btLhmrc/fIHPTHqlsXEuI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl; spf=none smtp.mailfrom=bgdev.pl; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b=Z/RujvIb; arc=none smtp.client-ip=209.85.221.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b="Z/RujvIb" Received: by mail-wr1-f53.google.com with SMTP id ffacd0b85a97d-38dc9eba8a1so2425736f8f.1 for ; Mon, 10 Feb 2025 02:52:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1739184730; x=1739789530; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=t38FtLAfWg90q8K8cZGqF/lNiD/EaoI3sQrqeZGRCB0=; b=Z/RujvIbDFJ1PskZAr83IpNHwmet1cBQ/LePQl48cyJO8jrKSRMsv5YUxWsflwoPIQ WVa0j8IvfJrjEZCkursjtDBbs3FnuU2m0Ku4TzyNsNolh6TGJ7xL4ufKmza38YbqJ7QN 4QXz7bnRG3+PfEF4YR9HVdoGfKU07AssoQUCTONd8k9M2DDPIPWQTM8pZLCn3u/iNa22 pgrurZRm2RomTE6r6nv60tl0k5MLs562WqqOk8Z25TF5WeDUOFqzzmrFyz/8lS34m9Y9 FCeLnEJHDN1rcjP5z3BJ3789XgP8R/B18qCLF6JHW/l29xgRBHq5ei7i5FXVnOWn39kC lfYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739184730; x=1739789530; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=t38FtLAfWg90q8K8cZGqF/lNiD/EaoI3sQrqeZGRCB0=; b=CskdZ1n/2Zr+xnw55YyPJvFuKLkDvvzpx2MGjG3c3HZSAtAJiJGey7dcQHbi0cDXWH y5BUGdjRjPhpkRRgIzgZU73Iy9ccppcZj8ttr+3HHk4fiI8AhdggHeellzFMgTmSfrNu ktNY+VdDSf9g5GQK+T3EJ56sMp8KoSUBS4r95fSeI/Nd/k1AWKT8r7Q0sEi6fphccKEz 5FSovcUUerAKZdw7GgBmwo8LbBGo+3/EvUitkkga/CA+jcPdVutsCpX6cRygnt+gzHXE o3VNLBtDLbklYw/UnnU1zNwDl24zoBEY2f9gXKDZp7DM8qtS2nGG3tdfj7EJJ+FRM8Ze Kinw== X-Gm-Message-State: AOJu0Yw4yaa9AdrP1kzVuTyGfLWivcE+U5YY5k8HlNNwn6Q8s+9y3NIk Mff7XjLahc6QybHbJvLoSNvIowobwIf+XjKcG5YyFVNc+Q4/7Vr2UIjljpuhk7k= X-Gm-Gg: ASbGncvConb00p0y4nemsy3flRvyOJ6tKWcjhvPxSQsKk1jYE9iHiLdb19lnolquHIf yu9+gf4Zfu+X+6AWlJ+fpucs+MtLAafsvgrhtxKV3AzDqLOtwiBmiIr72zHzaFz3naW1Sg8vg/4 K6devQaBs02dizfnAApAjcxgj8Wq3cfFWTUhg/E5jjjBUAAAFKgvOahIANbeq/8oFEa1KrFOMMi oDy6zAhSiC5MTLp0JLcaMI4f2gvr84i7t64J1ZT2BjonvCDQwjbVHMENXJvJK27W5T16yPSbjGq Yg5KmH66 X-Google-Smtp-Source: AGHT+IF41u1Rc0MGEu0xC4rMreQdWbirY1mfHhVjlzZ3Y6T+bbpkH6lVhcVk1GsNexhAZl9QPKcjmw== X-Received: by 2002:a05:6000:402c:b0:385:d852:29ed with SMTP id ffacd0b85a97d-38dc9138a84mr8096702f8f.36.1739184729776; Mon, 10 Feb 2025 02:52:09 -0800 (PST) Received: from [127.0.1.1] ([2a01:cb1d:dc:7e00:f3e9:47d0:94c6:777f]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38dc53ae9c0sm10557919f8f.101.2025.02.10.02.52.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2025 02:52:08 -0800 (PST) From: Bartosz Golaszewski Date: Mon, 10 Feb 2025 11:51:58 +0100 Subject: [PATCH 4/8] gpiolib: sanitize the return value of gpio_chip::get() Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250210-gpio-sanitize-retvals-v1-4-12ea88506cb2@linaro.org> References: <20250210-gpio-sanitize-retvals-v1-0-12ea88506cb2@linaro.org> In-Reply-To: <20250210-gpio-sanitize-retvals-v1-0-12ea88506cb2@linaro.org> To: Linus Walleij , Bartosz Golaszewski Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski X-Mailer: b4 0.14.1 X-Developer-Signature: v=1; a=openpgp-sha256; l=1494; i=bartosz.golaszewski@linaro.org; h=from:subject:message-id; bh=9oAnPodPjMQXAB4r9yjDBYdm9AiwJTye+nOcbjeQucw=; b=owEBbQKS/ZANAwAKARGnLqAUcddyAcsmYgBnqdpT8Lz34Hyog+MximLfUUtQo58kNHUFpAlFi cLetkdMhFWJAjMEAAEKAB0WIQQWnetsC8PEYBPSx58Rpy6gFHHXcgUCZ6naUwAKCRARpy6gFHHX cgUUEADCbShVCj0j1jH40nTMWKvrHuWMRwYEHR6WZi3MS6Q2v3k0bUFWz5+mLd1BQGJkldVarlu DcWAwfRb/iPEqVbG77GtNpwP7h5KD+mWjJg72KbB3TDT1WCG4o0rnP714j8bhliH+iAfNGrrrJp HXh/7axNvTZf0jQaZOXG8wMH5hS4tlP7yN3jXYz0z+mvjMJ+BuE2AAN7TRgX+y7N3EI+x3heI5w 8dSdvzTzD/KHqVyLcRH6Thrd6ozuLJ0kJCKQxNtauD+o99DXgmPvVSnfx4cMNejJu3n2Oiv06Wf GA9AZN2QO3g5LCzwXuYOEsgjfOGu/VFnMV7Xze90BaSnE9trpP2DcqrXQIzKn/Wn2Hf0/N2A+Mt H3XWCcH+PfeD3VG6LsE5KoluIp33RijuRp2OpLl65sXgj6MH6vCQopatps2ccugG218hgyGCV3Q ByPFFUjQYydPs3OBY+6Dqy/1t3j9ZGQbqD7NS1o/t8o75j6gK2P1MmHAUnf0Kb/BXoOziQhJhcM Y2C9EW3XRIq7CNRm/TsE4gMIIXX7TfczdJTeU+7UQF7MUv6zOhCI0vx+3bxP+jZIL4rQLletX6I d/djzKW9qhOTt1jhkEM1TXtE9l67kOhCNyYvF9IhkHA0bGg2Lc05F2DEoYDrwcyTRXStnmwOcvu J2GLd7vjEhj1olw== X-Developer-Key: i=bartosz.golaszewski@linaro.org; a=openpgp; fpr=169DEB6C0BC3C46013D2C79F11A72EA01471D772 From: Bartosz Golaszewski As per the API contract, the get() callback is only allowed to return 0, 1 or a negative error number. Add a wrapper around the callback calls that filters out anything else. Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpiolib.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 95ea300da109..7bc316154fdc 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -3097,9 +3097,25 @@ void gpiod_toggle_active_low(struct gpio_desc *desc) } EXPORT_SYMBOL_GPL(gpiod_toggle_active_low); +static int gpiochip_get(struct gpio_chip *gc, unsigned int offset) +{ + int ret; + + lockdep_assert_held(&gc->gpiodev->srcu); + + if (!gc->get) + return -EIO; + + ret = gc->get(gc, offset); + if (ret > 1) + ret = -EBADE; + + return ret; +} + static int gpio_chip_get_value(struct gpio_chip *gc, const struct gpio_desc *desc) { - return gc->get ? gc->get(gc, gpio_chip_hwgpio(desc)) : -EIO; + return gpiochip_get(gc, gpio_chip_hwgpio(desc)); } /* I/O calls are only valid after configuration completed; the relevant @@ -3154,7 +3170,7 @@ static int gpio_chip_get_multiple(struct gpio_chip *gc, int i, value; for_each_set_bit(i, mask, gc->ngpio) { - value = gc->get(gc, i); + value = gpiochip_get(gc, i); if (value < 0) return value; __assign_bit(i, bits, value); From patchwork Mon Feb 10 10:51:59 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 863950 Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A91431DF992 for ; Mon, 10 Feb 2025 10:52:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739184734; cv=none; b=oCE0EQc5L0XjdbpQOAmUPA1Fgt8fTS8VzKNw6r1Y2YIxNNAwVHdhaBMiK9pgv5GqgQTpxc6TfPkbtlMgGHZLW0UPhTblJr7wVVrO9cVtgUCXXS62K+4SHru0jL0/jb9YAMf0fM2F3wKay96BjecMOZ1+77Yd3lpVkYeCYsyesuQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739184734; c=relaxed/simple; bh=HiD8MOfVO6NKeeMuA8BdD1R1bH1+rCekUjVkFE0Z6yA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=lTjepC+RfxSrUJemb/nJAN/lVilx0M1+a9ce7frM9atI7t9pzFLcTfrlssAoOaWz8U/LPl5Z3zajajZAlflN8B0IqstKTx+YRo+pyTlbQa8N7in6R6OxCna+9s7ZcFg1fgJmZ2a0NciKEA6sZidgRVxeXavo2kuyDlV2qBczCgQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl; spf=none smtp.mailfrom=bgdev.pl; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b=0XCdfwVO; arc=none smtp.client-ip=209.85.128.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b="0XCdfwVO" Received: by mail-wm1-f52.google.com with SMTP id 5b1f17b1804b1-43934d6b155so9181825e9.1 for ; Mon, 10 Feb 2025 02:52:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1739184731; x=1739789531; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=6P+XIrg3e48T+45SB7OV7xfhAg5zNW2nJdNfDkQWT08=; b=0XCdfwVONJ5jhKywk/IrdXsTjvFzUulwJeZAUDa5vnym9h4Fsk7Oj3jyMlcYfJOMRk sa/XNuv7B2a5KKFXBiCzZDxeBMUBaayFU+lH/AB9GG4E0CQUkB0iTSpq6ro6lPialrBi pADrnTzPxsSshmZ7mnNJtYMYcKaxTymZu9fJ4p8QJSbvgwTosE+eKQ/Lov1q0oH109nw xZDpkWFhlJpq1t7uDkUni8wCVpvmmCeKFN5V3kZ1hu0jL9MXaJEqsEsEeau9zBpoQkja ZqMX6bc9WDJdEQbmRsFdd6dwaWh0blqR9xuk6alH8lGiaIv6tkTgTmHiy2o194RvJGox OT7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739184731; x=1739789531; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=6P+XIrg3e48T+45SB7OV7xfhAg5zNW2nJdNfDkQWT08=; b=Qm6nvMGdQGZ8RukvzAMnbKdoBxLlqmEmMISGtSXEKdtan/sd+Y85mGQyDNtkzD+HHZ S+skx5uTCtNj8Qf7I+3bmPyP3XHFE9PbnRWaEY+Ds72Vg1yR9BG79V3I2gKLuc75YHM+ ugV3wm9Hj21DWE1DzO5+iYFdoZuZnmuE0zVfdmb0h39DqECPnPWnDRNphY3LhR90fLXB t024u2p3lfazZ5IfPFugbj0luon8OwszeMYWcRiwGuGzhZltqXMigrrfyiN0n/YvROEx 2wXkwUuUsFVh8JerWbUJ4QRzp7eX7+o8Nn83Buk/xu/GxI6mCP52hAlvUOE/+L/ndCBM HW/g== X-Gm-Message-State: AOJu0Ywqq7cfwZNwyY6znlQQ7hreLYQ2bpdmp1/K4VFiAiIc5mX8Id/B QdK/9VNtQds/N4AEwnXWwU7+hI1LgDGjwLMGLwteHfwxzIjkIWbH+rwsGR+qyd4kEiTN/nNplPv 8ijw= X-Gm-Gg: ASbGncs+zx9CngYwUeJJ8G7B8HmFU8QYSpMFJCRaeflMfgSdylX6RQ0ZkbvlkuiMb6a 4y/0oyUfWCgTMQfVRgP0rYPR+odtQml/1eHzf87FOrk8zOArLhUuj0fjjhLkODK+s87J96eGt06 HamglPxOQCzZzA8j36QgQdZBGlXd4wEdwEocJFhWmU48YfzyBORosspIy8Z4kZYX71tXb/mKsRU V200riSxcwIFlDDSBv3KmmBKfamHOXgBa98h16AB51GNkzuNlLdTG/jpE8GaICjOdhK6ApwVL97 83qrYCaX X-Google-Smtp-Source: AGHT+IEzwSIHGoNewIvTLcEhxf/kvVRgtP5c2APQDYBcmqg0Byln/uiQ28OJCvQ+bKmommBuoCUsBg== X-Received: by 2002:a05:600c:348c:b0:434:ffb2:f9cf with SMTP id 5b1f17b1804b1-439255c10f7mr90135275e9.14.1739184731083; Mon, 10 Feb 2025 02:52:11 -0800 (PST) Received: from [127.0.1.1] ([2a01:cb1d:dc:7e00:f3e9:47d0:94c6:777f]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38dc53ae9c0sm10557919f8f.101.2025.02.10.02.52.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2025 02:52:10 -0800 (PST) From: Bartosz Golaszewski Date: Mon, 10 Feb 2025 11:51:59 +0100 Subject: [PATCH 5/8] gpiolib: sanitize the return value of gpio_chip::get_multiple() Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250210-gpio-sanitize-retvals-v1-5-12ea88506cb2@linaro.org> References: <20250210-gpio-sanitize-retvals-v1-0-12ea88506cb2@linaro.org> In-Reply-To: <20250210-gpio-sanitize-retvals-v1-0-12ea88506cb2@linaro.org> To: Linus Walleij , Bartosz Golaszewski Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski X-Mailer: b4 0.14.1 X-Developer-Signature: v=1; a=openpgp-sha256; l=923; i=bartosz.golaszewski@linaro.org; h=from:subject:message-id; bh=ooqgFTnKyGUq0xodNGxj0NJ5WHZ4siV0rPbv6X1n4l0=; b=owEBbQKS/ZANAwAKARGnLqAUcddyAcsmYgBnqdpTz03CtD1sSmlmUvJeBfmOF/PjBOFxKvIoT Fg3PrbKONmJAjMEAAEKAB0WIQQWnetsC8PEYBPSx58Rpy6gFHHXcgUCZ6naUwAKCRARpy6gFHHX ckFvEACH52s6NAoQ/9TFvaFeQNcNnY9bdDsYFk5YCAehtc2TOoX8FTbmcOgWBnYOSdLNXCa9xB2 btHB15kbHII/rFCmPqGTcmLCqADxG4uGOR7vQY1kiQtQJdN4Sv0xyi2ZV7k+MZ3M+q+OtNa5yqh aeDNzr/NrRA3lo7FR+bHj+M2qC87j2tma/Ol/86klYc04X+GcXFSAfj3Ytfc2w5Scd7NI9XzymK POzvnT7Uz2usEUmRjClf70luwKH65GZ2l7nshpKsmMFQbsjXcT3noZFz4+rZQtZnGvUigEkxb/D AF9/lcpZfG7zF3TAYZcuVLzw6SWbYIMzqrzwximCZNy4cs0WRcQtHeCkMgkc9+v1S2sxYfqsZE4 UdBnNuc5wUxkjK9JAkisqdBZfzfuSaagsEo3OOlAtQEDDDtqqleMmdrLLL0Nke5PN2/T0tWKxCR HiF4hL4sYN0ZEFsqr3XG2wk+hdU/jAqUJtJ5yz5kPF5xF4vd+kH9opUE3v+A+UAv9Cj/q6atNiB uB4288uYuNG8ihkPygBRCYp4VH5tu6BdSC7mD8J5CqRscAXFmwTL8onelkGEdat3f/NSkTBX/Er Au30IbWiVEz7FQt2k4HJoUg6FHoO0BDamBgOANFiqOf1x7BtbXlC78oQkWXo0Ct8Wbk9nmAvh+K cREp2IAL7WYsr3Q== X-Developer-Key: i=bartosz.golaszewski@linaro.org; a=openpgp; fpr=169DEB6C0BC3C46013D2C79F11A72EA01471D772 From: Bartosz Golaszewski As per the API contract, the get_multiple() callback is only allowed to return 0 or a negative error number. Filter out anything else. Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpiolib.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 7bc316154fdc..7255f4df6a8b 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -3164,8 +3164,13 @@ static int gpiod_get_raw_value_commit(const struct gpio_desc *desc) static int gpio_chip_get_multiple(struct gpio_chip *gc, unsigned long *mask, unsigned long *bits) { - if (gc->get_multiple) - return gc->get_multiple(gc, mask, bits); + int ret; + + if (gc->get_multiple) { + ret = gc->get_multiple(gc, mask, bits); + if (ret > 0) + return -EBADE; + } if (gc->get) { int i, value; From patchwork Mon Feb 10 10:52:00 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 864245 Received: from mail-wr1-f43.google.com (mail-wr1-f43.google.com [209.85.221.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EF8AE1E0DE6 for ; Mon, 10 Feb 2025 10:52:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739184735; cv=none; b=NCcfQ8A0D5l3hjqUzE8yPMMZr/+Sz9QOvGO2L3yaiT2aYcihM49nrvOzsSlHYqEat5CKCNiCHP9PSIY+dcPAIIdN45IBudhaBEKNtji18ifNLahBIPjF5VA8a20oEtLq96rY8ALLATjstxFTV9at2kbMHX3SiX3APZegrr/QvH8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739184735; c=relaxed/simple; bh=V6WoqRuA4KI05FV1+v4Oy7g4HufVg8Bs8TaRncykJPI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=pQ6A2k0+WGlzHe2jgIf1oz7ONQC0Id+8HmLX3pfcu3DEolyoWe4yyk6NFq77KtLdXrtiGsyYXsLVl8qfIzgnN/XspwUtj+ZnGAAUvff1AI2kj7c/TF2mdPh3ATN/OAZDDcXMX7xT/xTtDSS/89MewB8GRLC8Z8E7ydY/EZfzylY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl; spf=none smtp.mailfrom=bgdev.pl; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b=VgIXrlWf; arc=none smtp.client-ip=209.85.221.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b="VgIXrlWf" Received: by mail-wr1-f43.google.com with SMTP id ffacd0b85a97d-38de17a5fc9so229412f8f.3 for ; Mon, 10 Feb 2025 02:52:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1739184732; x=1739789532; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=LENKvGX4vcq9UheVuJ0AMEOpXRzUqJjB/uWgFb41XbQ=; b=VgIXrlWfT5IAZfuqHO8Tx74JYjImtjYpLJdAaL5kzcGpHR1jGE8Yw43LNgu0tiHqL7 R0tE/595Ae+JJ0ieIxU+lI867V8/wPDuQWuOu4tRBlSqOy69EjCJtYhVyGoWhyP4vqbg m+ETKwGHKmDhfbsniJ6LlHSihuhTFJXzGMEpjx0+ZtJecyHg2V/qu7DdDzgqvATXySdI 3oLKxZ5HBI/W/Yy+fGu4+SfLmgnt7j+YXirvJoldB6TDUh20oDozmKW+taVhTyaChG9Q /lP79wWkxbCATIRer8KXKepu2POP4MPT/vqpUsy0lbR7aMv0RcfWEqH+ujv0MIRg3pGG Mt9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739184732; x=1739789532; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=LENKvGX4vcq9UheVuJ0AMEOpXRzUqJjB/uWgFb41XbQ=; b=i2+jxuv4l/pYNdDEzEm1vZXOw4xjLcY3eMOIjd6k+xX6xfaBcAk4IlVU8CthG33NVz 1q8IvdKpJPqa7iE7GQKLZ9QHuiJaEQIsF2TvljI0nAKfYz1b2o89M5v9P1BeTqU0Z0yV In0a+eVnaWDv42w7R/WUsU4fHOESRLJIJYP+zHoSME7Z9kpUdAqulJWW86qsGa8/1fW7 /QsdKOb4ai1zMIC0uvCV/v5s44I28H1tmYDELXC8V/tCJbT1ZFiO0hoJA7+amPBW9dTv 2EnPZVmIuxjkKxE8uUYEQcb+AZxUALXixmVNqeTnz/LneBCOb/p6qtDolB8VlY9ltBYY 8KeA== X-Gm-Message-State: AOJu0Yw7D5m5RJ7Ozqey9NgxJr5zJrI/9XDYwwst02KgLb1oueKEkoPj YrOVALCI9nztNVHfKP98VSVsYXUr240wpoHaPo453Acna3yPvgaZ7oj1oFvMiFc00+5aOn/Q4JX GjRU= X-Gm-Gg: ASbGncvKfHy3h7njWCIfbefaftRFznTEwqU4g1w5ZL1zn1tjpC4hJhX+whhA1ZWZR0u JYc+rxtzGyhd5qb6OzXcBMpukQ7e6esrbHfaUhX8d7AhbjRHfOv7bEqa6lUTqKSbz0PH68cQzsd wcMc3vwzDixO5EPkjLzx/opW1tFGySR4qUL/GS+GNBgNCz4CD3wkk426h/61daNv0HqNaVZR8HO Snu0XAWMLQGIgLH64xHiNXxc1mz9FOO8u/ZeZKWgXptJImwZ5H4eYsPOnzDjqKRin374R0bDbXc UpVM5f20 X-Google-Smtp-Source: AGHT+IGcts0QLUB4a77mWTRAzfoRCt4VM8k7qsgfMeWESQ+XzkXt5a40ga/6FmpFrd0x0OUpy75nFQ== X-Received: by 2002:a05:6000:4012:b0:38d:e378:20f7 with SMTP id ffacd0b85a97d-38de37821c5mr46131f8f.41.1739184732183; Mon, 10 Feb 2025 02:52:12 -0800 (PST) Received: from [127.0.1.1] ([2a01:cb1d:dc:7e00:f3e9:47d0:94c6:777f]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38dc53ae9c0sm10557919f8f.101.2025.02.10.02.52.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2025 02:52:11 -0800 (PST) From: Bartosz Golaszewski Date: Mon, 10 Feb 2025 11:52:00 +0100 Subject: [PATCH 6/8] gpiolib: sanitize the return value of gpio_chip::direction_output() Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250210-gpio-sanitize-retvals-v1-6-12ea88506cb2@linaro.org> References: <20250210-gpio-sanitize-retvals-v1-0-12ea88506cb2@linaro.org> In-Reply-To: <20250210-gpio-sanitize-retvals-v1-0-12ea88506cb2@linaro.org> To: Linus Walleij , Bartosz Golaszewski Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski X-Mailer: b4 0.14.1 X-Developer-Signature: v=1; a=openpgp-sha256; l=2266; i=bartosz.golaszewski@linaro.org; h=from:subject:message-id; bh=v5PaGseLV+iX53DOub4sqpS1V3bIng/nrHtGLctVVdQ=; b=owEBbQKS/ZANAwAKARGnLqAUcddyAcsmYgBnqdpT05VU7QlrO9fymh6SdG4wxJ+svHmCP6bdk bYTEogd8/6JAjMEAAEKAB0WIQQWnetsC8PEYBPSx58Rpy6gFHHXcgUCZ6naUwAKCRARpy6gFHHX ciQAD/0Y/GW+31oLwGIVNlHfLMDdu64zc6BxN76j5jU+SUqFRhYTXjvnrhNEj8N6eYT83mfS0xI EUpIOfTnApVbqXF9er69IkYITWOrB7kdv8kdlk0KYSJEVVkthBYzgwHDppdNjcMRqrGzoO7/SOP Jfcjb6h1eFVONFj7XIvPRaBwMKVoZsp8f/eUHanFvdvZBnuOQkQspsyHRcZNNTI2gt3dEHi/Sr1 Ao+CAANEx8bc/t322NflWuhwEyL7hjV6juc4soB5MuAEvjL3rmri2mXfHG7Qqm0tcFmSfKMqGMY v2p7w0MH8lwpsyKh0EyUuFXx6LiB8lmTulv+5mSSBkZBOseUz2uSoNCT578EwS/GmJLGltrGCUd 12e79Jdzg/YHg1FR5PYRrj1N5/23swbeqRiQXUXI5ZXCEwHr21y6XRwvTfuX+fUbYkamZlSgJb3 R8dnGO5rJ1q7J6QARkgrZsu50cRgelpXBdxhp9Jdpvce2S5enJQK0VGDoew3c6a6Hf4FOc9wfGC vc9jIaUdd+xumNRLcl+N5ZM0elXqBSPYYGFxxmjt+bGyp+Ae1YMKthq4qkRTPsIFJGOIHPwsL8V EUrF3Flnm3EdT4yFKIcP10LQsbU3i2pgZWxugBzL7BXHOB2HtKlKmgIfcVsQCIZiW6dw2Wr+VtV cy7E2gz6ZN2Zb7Q== X-Developer-Key: i=bartosz.golaszewski@linaro.org; a=openpgp; fpr=169DEB6C0BC3C46013D2C79F11A72EA01471D772 From: Bartosz Golaszewski The return value of the direction_output() callback may be propagated to user-space. As per the API contract it can only return 0 or a negative error number. Add a wrapper around the callback calls that filters out anything else. Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpiolib.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 7255f4df6a8b..1f75ae6e208c 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -2683,6 +2683,23 @@ int gpio_set_debounce_timeout(struct gpio_desc *desc, unsigned int debounce) return ret; } +static int gpiochip_direction_output(struct gpio_chip *gc, unsigned int offset, + int value) +{ + int ret; + + lockdep_assert_held(&gc->gpiodev->srcu); + + if (WARN_ON(!gc->direction_output)) + return -EOPNOTSUPP; + + ret = gc->direction_output(gc, offset, value); + if (ret > 0) + ret = -EBADE; + + return ret; +} + /** * gpiod_direction_input - set the GPIO direction to input * @desc: GPIO to set to input @@ -2780,8 +2797,8 @@ static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value) } if (guard.gc->direction_output) { - ret = guard.gc->direction_output(guard.gc, - gpio_chip_hwgpio(desc), val); + ret = gpiochip_direction_output(guard.gc, + gpio_chip_hwgpio(desc), val); } else { /* Check that we are in output mode if we can */ if (guard.gc->get_direction) { @@ -3433,7 +3450,7 @@ static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value) if (value) { ret = guard.gc->direction_input(guard.gc, offset); } else { - ret = guard.gc->direction_output(guard.gc, offset, 0); + ret = gpiochip_direction_output(guard.gc, offset, 0); if (!ret) set_bit(FLAG_IS_OUT, &desc->flags); } @@ -3458,7 +3475,7 @@ static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value return; if (value) { - ret = guard.gc->direction_output(guard.gc, offset, 1); + ret = gpiochip_direction_output(guard.gc, offset, 1); if (!ret) set_bit(FLAG_IS_OUT, &desc->flags); } else { From patchwork Mon Feb 10 10:52:01 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 863949 Received: from mail-wr1-f50.google.com (mail-wr1-f50.google.com [209.85.221.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0CF2F1E2850 for ; Mon, 10 Feb 2025 10:52:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739184736; cv=none; b=NbYZcbGmzt0W63H4m+cNIWXoMAIGzB2KypEbW6+ITqaGoklvwkejELfzSX5PDnhxXA0D6JlL1Bbs+HPVItyLCTIfLm2mP7j1l6Odvu43Z4UaNBVC0tohR1K73K430aNsr/NfUH2qx2Tpdv3eaZ95X6QlDTRnYiLUZK45BVDJ0A0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739184736; c=relaxed/simple; bh=Tt49jpwzZ3ol8gmWUWuTAh+k9QZSdGkNwmYsZGaiOMY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=GhdIlGgBNLGyqBxRx0O+E5tYFs9/FXg0kOSa8LSuNocV0KIMyuXJ/3vwYpqQAv1B0WBqI7RXEQebrLdD/kCnAQ8yYYOLly3vCDffRSaotTcyLzEn3itKNlLZx/yUoxzlHN+e0sSJQpRBGP0OLpcaHny6Y4I+5g11wDiZiioCcgM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl; spf=none smtp.mailfrom=bgdev.pl; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b=NtHDS7Jw; arc=none smtp.client-ip=209.85.221.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b="NtHDS7Jw" Received: by mail-wr1-f50.google.com with SMTP id ffacd0b85a97d-38dc6d55ebaso1843551f8f.1 for ; Mon, 10 Feb 2025 02:52:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1739184733; x=1739789533; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=H1+79In2bEjMxbwOZBK0mwGOJ1VVLwxcGp4ZvDTlgOY=; b=NtHDS7JwMrw0NbVU0WYyH+n8WEzZqx8yC3+SK3hxQDZXxmuAXH8Nrx9aYL8NGcxKAT k66EePqnotMhvvxRFeri4egfDLKS7P0QYgMsYUqTPlyBq7cWXYKY0iXFiS0RY3S1X56Q M35Yy390PdBtZwTG8TqShngd/9Nr9aBQ+SvrGq3P1YLXnMOLNdgN2oGEQgUjKDOOpLuN GdV47tZAHZM3uQ5rZ6+CKoeMo/yaGa6qQpwYi1C9Aw7zPl59z6CD5m6jW/uh2hLyjbew vkuC1XIikMDriOSzV2+lr8c6nfhnZoKx6ROSAHP646cwp4BRGTu+sXQniQ8YlFXFS8oG a9Dw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739184733; x=1739789533; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=H1+79In2bEjMxbwOZBK0mwGOJ1VVLwxcGp4ZvDTlgOY=; b=Wsv6mNO7EYVv+P233nyXXlwx90MoQipbP/kiciYJBKJNG0vbnR/6gisoIp7E+PJ29d 9Vla4p6gNIsokgYwv0sqrRgwsEgWbaVl5LLAFYHFd/GuVyHXhehjoiPB9Ne45MbISzr6 pw61qtLh6zJvhKQYfOtoNpLEUIS0S5Lajfylhbb3LhwCiOHmeu8nAEjFIkOq1H9+ppzN LHqq+Yq+rnBFibd4GzTfMu7KwPPjJvlfcZ3RDX/xP77K4FXkQl+7yWkiNHrWaGLI6v3+ we16jaugsIahIvaJJrXipXi30D3skHigg3SfOi031UlXbl33drkGtidUr0dx6LiLopmp LcxQ== X-Gm-Message-State: AOJu0YwBXl48d7R0IlEWy6BskR6qi7hkxE1VZXKVs5Yct297Ct4zULX6 fY+VeebDf+FRY39DBX9ZBELKheErcYSLnh6cl238e2ubFHMp/teH62yJHc+3LsU= X-Gm-Gg: ASbGncsVq9NfrL8zs8xsP9mMk7By1jl3EcMxdjKT7F5c15HkQzkuGkp0iew9JHG0H8i 8jGSjIIyDC5q/ut2j52Iok+ZRBoia1kKs+iG/wMmezJyt6EuIR75NfPJ9Jsqg70Ld9Pd40UTgRl tR2cgTeKP8/UAjY8+GI8y5cE3THE35MvZV97wE5L7nn6K3PpMcT3aYAL6lvXx+P9O1zMTDhkhPI 7smdzGErG9sKsi6DC7BPmsSV9l/tGsNt/zSrb1IBaizuj/xGyVtzvtEazIoPs6t2Yzlx3rnJ48s oC98uWVD X-Google-Smtp-Source: AGHT+IEAPJaOIykPBZKf10WDEwpUc48jTfw5DdMg+WEP+fcubB4B1vmlbiOPLCgUcSlejxMiJ97MeA== X-Received: by 2002:a05:6000:402b:b0:38d:afc8:954e with SMTP id ffacd0b85a97d-38dbb251e08mr14592234f8f.11.1739184733273; Mon, 10 Feb 2025 02:52:13 -0800 (PST) Received: from [127.0.1.1] ([2a01:cb1d:dc:7e00:f3e9:47d0:94c6:777f]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38dc53ae9c0sm10557919f8f.101.2025.02.10.02.52.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2025 02:52:12 -0800 (PST) From: Bartosz Golaszewski Date: Mon, 10 Feb 2025 11:52:01 +0100 Subject: [PATCH 7/8] gpiolib: sanitize the return value of gpio_chip::direction_input() Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250210-gpio-sanitize-retvals-v1-7-12ea88506cb2@linaro.org> References: <20250210-gpio-sanitize-retvals-v1-0-12ea88506cb2@linaro.org> In-Reply-To: <20250210-gpio-sanitize-retvals-v1-0-12ea88506cb2@linaro.org> To: Linus Walleij , Bartosz Golaszewski Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski X-Mailer: b4 0.14.1 X-Developer-Signature: v=1; a=openpgp-sha256; l=2283; i=bartosz.golaszewski@linaro.org; h=from:subject:message-id; bh=LPsmymBhAWix2fPGJHIO52f9SJsvYZKGQf0Il6uLLSI=; b=owEBbQKS/ZANAwAKARGnLqAUcddyAcsmYgBnqdpT8oXiXz9TnBI1D2A6CzeHwMoqa/NdBW/TW 4K7Bj/U+4mJAjMEAAEKAB0WIQQWnetsC8PEYBPSx58Rpy6gFHHXcgUCZ6naUwAKCRARpy6gFHHX crh+EAC9LNr/MGK2bSbtPtqpnVNx1KGQ8xzgGhBILcBQg6u2n5w2cReBWOif7fVpn4TEEV3kqjZ 7HpF3DW/DbfyHCsVTtZVLhw6j8BXntb2UhDUUJSGnTBNK3vfW3MBESE72/PIuOZkCW0rVqVvPYa T+iXWydoCDKbPDpJVgzPcy0Bll2136Jeqi159psFDOrV/ggUQ2cO2IVmV+L7i9oJ+H6p4FjcPBK YJmjHcimq8oAx7mYShGy4YRQf39bssMIyjynccC/xki4cJ7uaqwfWY8umG2+NXSdQJP+k80Wp0D JmQmcsNHDVBiQbKfqnqHxUiq0x0sFDg042LghmXceMIrgVfRGSHpmfOu+aS97rCshyP6Uuk6oJ0 KX+Kw7YuBTqAumA6kH1Z4yYJgmOYoiBhC8t9wkB8aztwSlqEVWhZK/R1X/uYHOoYa4Ovz7AKLVx KmfQOEoI3/hLKRX3X7KmIw9GujO32FqOzFigBkFhYGSSfPnhDe+efIFTUgfIPujoYcKWKUEfC7z KjS1WwrlkuSSY4lzerZsU3EKKnYB8JRbuN6vYYHYvxe+yolnANFeWenny5+2Qhm7FHZE6Gxte4y iyTBXx5Q5rapDFGP+AxpxbktzdEHFaqN7+fIlirQiTYt+vpqkvkv/C+8AX57DL0+6XNcP3ViIjr xHfoFdPEJetQGRA== X-Developer-Key: i=bartosz.golaszewski@linaro.org; a=openpgp; fpr=169DEB6C0BC3C46013D2C79F11A72EA01471D772 From: Bartosz Golaszewski The return value of the direction_input() callback may be propagated to user-space. As per the API contract it can only return 0 or a negative error number. Add a wrapper around the callback calls that filters out anything else. Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpiolib.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 1f75ae6e208c..683a03d237c0 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -2683,6 +2683,22 @@ int gpio_set_debounce_timeout(struct gpio_desc *desc, unsigned int debounce) return ret; } +static int gpiochip_direction_input(struct gpio_chip *gc, unsigned int offset) +{ + int ret; + + lockdep_assert_held(&gc->gpiodev->srcu); + + if (WARN_ON(!gc->direction_input)) + return -EOPNOTSUPP; + + ret = gc->direction_input(gc, offset); + if (ret > 0) + ret = -EBADE; + + return ret; +} + static int gpiochip_direction_output(struct gpio_chip *gc, unsigned int offset, int value) { @@ -2751,8 +2767,8 @@ int gpiod_direction_input_nonotify(struct gpio_desc *desc) * assume we are in input mode after this. */ if (guard.gc->direction_input) { - ret = guard.gc->direction_input(guard.gc, - gpio_chip_hwgpio(desc)); + ret = gpiochip_direction_input(guard.gc, + gpio_chip_hwgpio(desc)); } else if (guard.gc->get_direction) { ret = guard.gc->get_direction(guard.gc, gpio_chip_hwgpio(desc)); @@ -3448,7 +3464,7 @@ static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value) return; if (value) { - ret = guard.gc->direction_input(guard.gc, offset); + ret = gpiochip_direction_input(guard.gc, offset); } else { ret = gpiochip_direction_output(guard.gc, offset, 0); if (!ret) @@ -3479,7 +3495,7 @@ static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value if (!ret) set_bit(FLAG_IS_OUT, &desc->flags); } else { - ret = guard.gc->direction_input(guard.gc, offset); + ret = gpiochip_direction_input(guard.gc, offset); } trace_gpio_direction(desc_to_gpio(desc), !value, ret); if (ret < 0) From patchwork Mon Feb 10 10:52:02 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 864244 Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 27AA21E47D9 for ; Mon, 10 Feb 2025 10:52:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739184739; cv=none; b=V14E3Ws+OwOus+CUsHnG1iY8WU+7Hn31gT6iFdVfpUNvJoDcwO9Vfi2up+5IXciXWEeg1xbKXC0BjqFbznGhgX8g1vR3x/wULvWqKoSGzWKDS1HG1wmF/3bQQOsuWbNzBQUvYJ8bds5yrgWQ4BuKJv6SuViBFJicEogwVQAVBpY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739184739; c=relaxed/simple; bh=COgSGy9olGzNPwhRD7Ivun1jBH1Om1X5+XQKQJmEmoc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=lUXa/THukXp0PcpyooDxC54uOuxQrEZpjXdFPjtBCXPO5e3j0N7F3Q1ffNQMnRDdYAa1LSSUcGQKzeDTngBnXUvmTkvBO3mmAlLhrBENYfsJ4+qbH8hXFPJivSWwFQygwpLjHMqfpBOA0NqvAuEK9Kkex79ZKq0MXuNrQjSG5bg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl; spf=none smtp.mailfrom=bgdev.pl; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b=nyFzsGLg; arc=none smtp.client-ip=209.85.128.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b="nyFzsGLg" Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-436ce2ab251so28122755e9.1 for ; Mon, 10 Feb 2025 02:52:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1739184734; x=1739789534; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=/0/sW6H/bmkXZigeRuuijEq0ngfaHSABgKK2stXVqho=; b=nyFzsGLgxQUyQzDJczAhpuVG7WCRyJpZOAsMUI4pMHFD1cUuabUett+RWYwqQ/Mdzq iHjfXwISkAAPIMCqiHZOlcoc46erkE6/yfcjyVqhNl/PKaC7dfI9wze/xc+/IpdvaTsd Bn4nw0j0DeIedCW4JgH+Go03yCgdCeEMXKpN4q70Ld5gQLdE54/rfCxKK7+aMAY8l6aW eYD/qo3LD3c0QnNyoLmeHt9SzYUwmsgCrm2DRavzCho/i5LhumLnnghVlX8XMsC/MXsq FXKVYP6HNdHIAfXxNPTxyWRaRZamSrLQEROre9kwJ0eVNeBqNGeVnsGFHk32fjUHVBZM SsKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739184734; x=1739789534; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/0/sW6H/bmkXZigeRuuijEq0ngfaHSABgKK2stXVqho=; b=YTvZanEupasW/ANV8nCgArV5/02XA5X8Y8nU0D5eCM0YOk6i1B3w718uILCVN6st/q O73277VQt/ErLbk3RDVYgpxKTI/JF8NKDjxIojAX3Xz0PiAk/Twb8JVlbzFksihmopkW TD7iyYpMlGQ1s1XgKM7NOQc7yw0pD1PEmdpBowoptEzlm+eOWTCXgJcuDqMM6BntfkUY H96GzM+SPGdCq/zbxIWR37v/tzlEHlhO6i4wf7RYD/j8DDaP4YZ4iYEaY+4zYymEMTFk HFux9c08Y9ZqojqwFxB2gd/wOyXcu8WbLPlFACPxXp7n69EgRXunHBvI++Rp14wcWGEa Up5Q== X-Gm-Message-State: AOJu0YxcHtcf0p0C31yNhpqRcPjXWUpbAkc2ZTwFOZwAIUYJETIrrWWS /USju1Rkv6cEwS3DwUYxb6JeCS1P6c7odH6xNXmP2yFCkksyb9Tj7CceLudaAB6clk4KOMZj3lL ZiNQ= X-Gm-Gg: ASbGnctX8g00IOmWuCCcz0lJxHqOFHOZuc0O3bZ4b8TCYmsLlkA3/tp5Wk+bd+gpUOF RtdG+iui/uujyHf0WJGkZ+m4Oy3pEKFbTigf0r6B7UoZls+PDxiYOSPmZc9ixglTPvj2qHXSJxT 0zORxEOnSINOn2/T4EAfZl2HvY0wEWGnrlw4Xon5ASF5dMi0KV+T2FbMPOxFEI1OYF/+mjweXbU Qs6L/+YVnvhhIBiU17XKVKl3mWRiMrXmYKNfmKRYreH9juJTjZgTtPfqiuSS1FSACp0LexqRUKE Py5nZDQv X-Google-Smtp-Source: AGHT+IFBMafOIJZOhE6ova2QrfOmITLX2hGvdoIMAqytoVBhRb0mjwMuLoWCZbIRz9oCE1M0IxfgHA== X-Received: by 2002:a05:600c:4f90:b0:434:fd01:2e5f with SMTP id 5b1f17b1804b1-439249b2a77mr113896705e9.29.1739184734552; Mon, 10 Feb 2025 02:52:14 -0800 (PST) Received: from [127.0.1.1] ([2a01:cb1d:dc:7e00:f3e9:47d0:94c6:777f]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38dc53ae9c0sm10557919f8f.101.2025.02.10.02.52.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2025 02:52:13 -0800 (PST) From: Bartosz Golaszewski Date: Mon, 10 Feb 2025 11:52:02 +0100 Subject: [PATCH 8/8] gpiolib: sanitize the return value of gpio_chip::get_direction() Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250210-gpio-sanitize-retvals-v1-8-12ea88506cb2@linaro.org> References: <20250210-gpio-sanitize-retvals-v1-0-12ea88506cb2@linaro.org> In-Reply-To: <20250210-gpio-sanitize-retvals-v1-0-12ea88506cb2@linaro.org> To: Linus Walleij , Bartosz Golaszewski Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski X-Mailer: b4 0.14.1 X-Developer-Signature: v=1; a=openpgp-sha256; l=2451; i=bartosz.golaszewski@linaro.org; h=from:subject:message-id; bh=odzeSyQqR+d/IE3LqbI4s+DJaeStILeldUjQZftzBGo=; b=owEBbQKS/ZANAwAKARGnLqAUcddyAcsmYgBnqdpTAseynSVPGOyZXsIdGv+COuC2m4YKbY4VO v6aBvQju4CJAjMEAAEKAB0WIQQWnetsC8PEYBPSx58Rpy6gFHHXcgUCZ6naUwAKCRARpy6gFHHX cpIWD/9OFLX/Q3ohOtl8qp/j4xwAioTU/BcpGSOdamRbQPOjdB6zN5ZxNVr1WxMwn7lKbR7Sfv5 8oL7kx9oADBDK5Uv4i1b/JMYOqJqhWHa6wKjyYmIQiN8fmLrqViQXDVX5uxBASVRI31AlosbPyr s5wKhIc3HJKVgweR+nEilBYPLEVozG/EMl/DfQ+9oXZMzzDuTObO/2R/XntJWTJktl+A8AUo0M7 mh7rQOd2QhBCRV6rPGzcZD0oL7qtLTttzTH6u8i8cb0sW3QlQk+NyWqijkC0nKcrcXe0BzDI6Lr vPYyTsm0a/spZ4M1v/kTWgaYlGlAlTI0nVUpUzfCyQJ9Ct34BweM7gejjnqnWn+54NXh4MLAJsy A81zsE4XL7g7799J3Jlyahr5VhMALAgHZeG889f9HO5A+jhIKURQKcf+qnYcvPRxvs3tbh5c/Lp KuKy5HNOpPOzsX+n4Ai0wJUuXWRykiYKFWutj4NvlYdeU3pDqvASfqdeyTbZY/csTVt69nfgwlD A5HedZXqLIb941ECLj6oX4zoM05l6qsMIuJh0XkqEb47/wBq1YUsBWBm4d9t2+7eKwBDG4GKeoc aZj9krEwHJYCngnBWLnWiFm+VXstyosJUyaEmaw5NMZKBNHOobckgvX+TR5n16jywUIDvIoOy1q 2C0HJ6+h0+BXYkQ== X-Developer-Key: i=bartosz.golaszewski@linaro.org; a=openpgp; fpr=169DEB6C0BC3C46013D2C79F11A72EA01471D772 From: Bartosz Golaszewski As per the API contract, the get_direction() callback can only return 0, 1 or a negative error number. Add a wrapper around the callback calls that filters out anything else. Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpiolib.c | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 683a03d237c0..7f2aca9f81a1 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -341,6 +341,22 @@ static int gpiochip_find_base_unlocked(u16 ngpio) } } +static int gpiochip_get_direction(struct gpio_chip *gc, unsigned int offset) +{ + int ret; + + lockdep_assert_held(&gc->gpiodev->srcu); + + if (WARN_ON(!gc->get_direction)) + return -EOPNOTSUPP; + + ret = gc->get_direction(gc, offset); + if (ret > 1) + ret = -EBADE; + + return ret; +} + /** * gpiod_get_direction - return the current direction of a GPIO * @desc: GPIO to get the direction of @@ -381,7 +397,7 @@ int gpiod_get_direction(struct gpio_desc *desc) if (!guard.gc->get_direction) return -ENOTSUPP; - ret = guard.gc->get_direction(guard.gc, offset); + ret = gpiochip_get_direction(guard.gc, offset); if (ret < 0) return ret; @@ -1057,7 +1073,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, desc->gdev = gdev; if (gc->get_direction && gpiochip_line_is_valid(gc, desc_index)) { - ret = gc->get_direction(gc, desc_index); + ret = gpiochip_get_direction(gc, desc_index); if (ret < 0) goto err_cleanup_desc_srcu; @@ -2770,8 +2786,7 @@ int gpiod_direction_input_nonotify(struct gpio_desc *desc) ret = gpiochip_direction_input(guard.gc, gpio_chip_hwgpio(desc)); } else if (guard.gc->get_direction) { - ret = guard.gc->get_direction(guard.gc, - gpio_chip_hwgpio(desc)); + ret = gpiochip_get_direction(guard.gc, gpio_chip_hwgpio(desc)); if (ret < 0) return ret; @@ -2818,8 +2833,8 @@ static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value) } else { /* Check that we are in output mode if we can */ if (guard.gc->get_direction) { - ret = guard.gc->get_direction(guard.gc, - gpio_chip_hwgpio(desc)); + ret = gpiochip_get_direction(guard.gc, + gpio_chip_hwgpio(desc)); if (ret < 0) return ret;