From patchwork Wed Sep 18 14:08:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Markus Elfring X-Patchwork-Id: 13806921 X-Patchwork-Delegate: mpatocka@redhat.com Received: from mout.web.de (mout.web.de [212.227.15.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9C86C189914; Wed, 18 Sep 2024 14:08:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=212.227.15.4 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726668500; cv=none; b=ezmuE0yZ7U2e63gMPH35PRPGsvHOfBLDGljZj0STlHlUFqKMzkjkO2vqnZ7apAYKkSpgD90es33/j5JXNKG4eUb51YBXBHmDqMxs5iurUCzicLisAXQ971+SmmCQSBYVRxQzWvDXeV3uFpnGJD8vRXeIwriOISjvcwMa7Mjd5G8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726668500; c=relaxed/simple; bh=SR59Zy5+0H2JkKgz/OJzRShajwpCLZ8pjA8HUTdsCYE=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=C9vPQl4u91zffKPkwDvU0AErkYjYhe/Yeijo/eB75t1iuzVO8HiqvuHK+fMiNMs00Zp4k5tin2k80ld4x+kDEqlg2Mku9ailqMRwSUJIUeGSxHwUiUXl1JsHeZP/SGSSd7hAQsQ5kVYCtljMi8l75rgwNz5dWFFeFcfaL4+NAZE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=web.de; spf=pass smtp.mailfrom=web.de; dkim=pass (2048-bit key) header.d=web.de header.i=markus.elfring@web.de header.b=iPidPgVw; arc=none smtp.client-ip=212.227.15.4 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=web.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=web.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=web.de header.i=markus.elfring@web.de header.b="iPidPgVw" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=web.de; s=s29768273; t=1726668490; x=1727273290; i=markus.elfring@web.de; bh=pEmW+piCbVWV20mN7xgHAtfyqql/eYPfht5wyl2LEeA=; h=X-UI-Sender-Class:Message-ID:Date:MIME-Version:Subject:From:To: Cc:References:In-Reply-To:Content-Type:Content-Transfer-Encoding: cc:content-transfer-encoding:content-type:date:from:message-id: mime-version:reply-to:subject:to; b=iPidPgVwc7e8j1FxRQuKnWlir0Czsek05Gob4msjleTXKIROzBHhZXKPegE5Ozbz zVd+l7wEch3nN1fkVT1SQhKSLxeYObrE98gldx4b8ybrdeI+nmsGwHmE4vxlr4kw/ mkA2RfzBgL1RXgPVjFvjXZbh/xDrTG4GnvkCwgTmyt5KxKSgBJ8bVa6JGWXwKls7Q f16fWfnddFTe40fn6UtSDdUi8LuMqn+5SYdplGBAsLovYF4H1Yw/cSUHiHK8RLM0h dKa7b4tBFo6f5PSCzgyzBiDTjUDPgsjp26koJBKM843tU7Af+7hYS0RMV2i70j7ED UjlvQSSfS5mqYWgu8Q== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Received: from [192.168.178.21] ([94.31.85.95]) by smtp.web.de (mrweb006 [213.165.67.108]) with ESMTPSA (Nemesis) id 1Mmymp-1s9dvC3wzH-00h69H; Wed, 18 Sep 2024 16:08:09 +0200 Message-ID: Date: Wed, 18 Sep 2024 16:08:09 +0200 Precedence: bulk X-Mailing-List: dm-devel@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: [PATCH 1/2] dm-crypt: Use up_read() together with key_put() only once in crypt_set_keyring_key() From: Markus Elfring To: dm-devel@lists.linux.dev, kernel-janitors@vger.kernel.org, Alasdair Kergon , Mike Snitzer , Mikulas Patocka , Ondrej Kozina , Waiman Long Cc: LKML References: <68ee5a00-888c-420f-a3a9-a556c19ee6eb@web.de> Content-Language: en-GB In-Reply-To: <68ee5a00-888c-420f-a3a9-a556c19ee6eb@web.de> X-Provags-ID: V03:K1:O5jt9HpoSMIFeehshGrf2vDObu39p8RKz1mnqQeYuS08zJaR/Ao C4LWSzrloc+Drx0c72xC8UWRoewf54YhwBgqO1+aGBZAqrrJ+2Hv38H0lZUXu6OHMJn09v+ eWwW6IDzVi+n3vYl/USLThX48T/lJW6qljSxj58QfFkJtZt06PwuO0r5RezpRjWk5k/xC0z O/peMe/I/ENH3bggzlTpQ== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:nuwLnslBJj4=;KwLhJNfMX6ifWY6KIF/utjj9zhF 3TtHHMvyUoxnw22uyS02ywq0aVEPtz0JFtkcPxiTY9UxCQU+QZAqbru2lipl55cZVQhw+CuG6 usuA5y0Rcfni41fpzHRSNuzkHH3f01/QKku1BRFNU3m4KHqGfPAeSVEL2ISO27Pnzv0LAxSyi NvGD9HfQtXEsDu+y4YttpTDd7ZYeAIVaAxsQNpriNnGRER6cd6P7UqGvya+lUumLIEPAVo6ak ZhXS4+CRuKzalxtGXT8hJix6FfiQ1qTeNP75yJX7vNL9l+2KgNUfZWBN2ksxpRNwPxZuSpqhl 1cLC4WbZd4sCLtkmmdAiKEOTEpO3zqIa7ouz2nNRdwxkMmgRcZer3RCfVU+DppG2HwoO86hAe 36x1vlwLtU4IsQhOK5Sjrp3klV4HO6OQga4fGhMlq5gJ7uBLlIsAlxoQv4q2LBFPbMWYYzn1A HTycWHgmEJPJ2QVPLIP4K6D9oeCupiUHMu2IaSk7AwgJ1cF/sJXRiD1ho1KTP3VbzGQaRzR4E i/OViMhx2AOQ3p6L4NyeRVDOShrFTHEAsxOiTODyIHHeNAZ9ou5qtsDTakIC6lFZbkveQey/3 L1J34nSf9GicTQGp1IJ5M96fQgtIl+sV8ZSdGG1MWVGTThVdhRMUNc45jlPl4JJ1Btd0KX8Up qSyhyOQmhQtkTOKcA2sk91QBER4xzbbIFQ//rACgakMb0x8mzNTehWgEcTtzYfHgk/SVFNruD j69IEMmKBcvehoeq6PlVLAs+PRTzoA5+sSL3QWjdvTySWWUlmcbvYkFXs8k0tyKOmUjkut3rW l3aFd55MyPnkruTtrYyDejKA== From: Markus Elfring Date: Wed, 18 Sep 2024 15:05:29 +0200 The combination of the calls “up_read(&key->sem)” and “key_put(key)” was immediately used after a return code check for a set_key() call in this function implementation. Thus use such a function call pair only once instead directly before the check. This issue was transformed by using the Coccinelle software. Signed-off-by: Markus Elfring --- drivers/md/dm-crypt.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) -- 2.46.0 diff --git a/drivers/md/dm-crypt.c b/drivers/md/dm-crypt.c index d5533b43054e..dae2fe3cb182 100644 --- a/drivers/md/dm-crypt.c +++ b/drivers/md/dm-crypt.c @@ -2621,16 +2621,13 @@ static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string down_read(&key->sem); ret = set_key(cc, key); + up_read(&key->sem); + key_put(key); if (ret < 0) { - up_read(&key->sem); - key_put(key); kfree_sensitive(new_key_string); return ret; } - up_read(&key->sem); - key_put(key); - /* clear the flag since following operations may invalidate previously valid key */ clear_bit(DM_CRYPT_KEY_VALID, &cc->flags); From patchwork Wed Sep 18 14:10:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Markus Elfring X-Patchwork-Id: 13806925 X-Patchwork-Delegate: mpatocka@redhat.com Received: from mout.web.de (mout.web.de [212.227.15.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DFF39F9CB; Wed, 18 Sep 2024 14:10:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=212.227.15.4 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726668634; cv=none; b=dPT110NgJnGTGwccv5MW4W53VbOHw8XU/9i2+x3TB9daud1gcoKRQ5BQBz3N5rYX4lVxbjrF0J4ibVr5TtLn7lEv15b3kVsmf/1V3F3uZjAvzG030YKnTopnNGPVXPxsV1ug/aki+YxaQCh5oOOOgYfhFxQvw9HcCOri0OhU0Dk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726668634; c=relaxed/simple; bh=3C11Kerkn3JS+3RbT9u+i6u+X/3/5jlEtqqKJ7FtdUM=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=QT/akZ9UMsV5/YlRh9TpEJcI7zIJDj6z+zkByUthXgJQse5C28PP5wJTmoWSs82Loiq22ZDicy4e6dOMXE6kSmtk4S0ngIrhk+gTBrba9IDBSiqA5I/zS7i0IZKOfMDgN6eA/iKtlW/vqUW44fZ9mK0pFg50ljfrYV105QPHZLE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=web.de; spf=pass smtp.mailfrom=web.de; dkim=pass (2048-bit key) header.d=web.de header.i=markus.elfring@web.de header.b=gP7FDy/F; arc=none smtp.client-ip=212.227.15.4 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=web.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=web.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=web.de header.i=markus.elfring@web.de header.b="gP7FDy/F" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=web.de; s=s29768273; t=1726668623; x=1727273423; i=markus.elfring@web.de; bh=xAF+viKco1ExEDRgTKCRdN6qb3Zg2cFwCgxgzdw6C5s=; h=X-UI-Sender-Class:Message-ID:Date:MIME-Version:Subject:From:To: Cc:References:In-Reply-To:Content-Type:Content-Transfer-Encoding: cc:content-transfer-encoding:content-type:date:from:message-id: mime-version:reply-to:subject:to; b=gP7FDy/FqBlxA0ipHExjNeeRdZevc8cUBYy25ONkVvC0LvQ4L+OOAdB3e32Yi1Ox qZ/jvYzZLV2jS4aSh5OXUvZXj9i8T4NlWNH6t7ME4deJk1+NZwG5DNQgrnXnDaxuM RIzHZoo5FVnmnjMbahdTmRMHQp4yAmEjxijL3RAlXn7rqXDjugm9pVfp0Hg7I/SzG lMtrwVFdvaCUnNu+zVjplpKS7Igp0YJsTOWk/NtZOwH6zps1adjslKbUioXN1/+57 ZnDDAIFOotFEKKMdRUbPhDZczdEm6iaED775v0uCjJ3/7aOpypiUkHAfOA2Z2rsN/ 9DOK1bW1p8eSo1Jmdg== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Received: from [192.168.178.21] ([94.31.85.95]) by smtp.web.de (mrweb006 [213.165.67.108]) with ESMTPSA (Nemesis) id 1McIki-1sGTdT0Q4i-00oU3u; Wed, 18 Sep 2024 16:10:23 +0200 Message-ID: <4b4379e6-e341-46da-a951-57b31edf3997@web.de> Date: Wed, 18 Sep 2024 16:10:22 +0200 Precedence: bulk X-Mailing-List: dm-devel@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: [PATCH 2/2] dm-crypt: Use common error handling code in crypt_set_keyring_key() From: Markus Elfring To: dm-devel@lists.linux.dev, kernel-janitors@vger.kernel.org, Alasdair Kergon , Mike Snitzer , Mikulas Patocka , Ondrej Kozina , Waiman Long Cc: LKML References: <68ee5a00-888c-420f-a3a9-a556c19ee6eb@web.de> Content-Language: en-GB In-Reply-To: <68ee5a00-888c-420f-a3a9-a556c19ee6eb@web.de> X-Provags-ID: V03:K1:w5sHG1W3937bh0DSYJK74Szc821M+zZ4Rw2ntxxIeCFCxP87JxB nr7dwCiFo1hcByuH72ANjxTEsIGcTzFKs5SF85MTkYFXTmxCF+8SHv2hBaXDR2/80np7LWx Z6sZXggO3QhkQlhO8BBjpXxAOJTXBD2LK0Ns4FjgX6/ZaDF/6DGSjB15hCKYCPI3TJvE6/G ALq5tXc/+VMo9uOpQuGVQ== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:IFEZA55noi0=;5rQ29AH+BopRb54oFLdibqhwmv1 LDPCdKnhSd5vraldLehnUEWgTq25jQoOzHHTGn3GHUr2iwnIgy3ZHPPVTD1J27ieWrPRlySPJ yV6DE5yKOWuF2iEAoSdkMRpzXGSJjpfdMqTdTYy19UKSafLYqWiKqtbd7x5HJYp5fZ9kvJXRY wuDjnMe8dpuGAifK9BIA6h4j9dLKURdNanY3poflL/WySuY/4g3pylb5P8trw2TZSWbZn5YJw a4nghBe5YC2YX8SLk80HKtxHYWvpLiI0qUTugKBGVpb8hY7RS81LyX4PGf7lmher5U2QFLkJG oHIQGqBuMV4V6IHx7tkfIh69SnBgHBWtkATmkKpHg8qyqY81bKzkxuQhC8E6SlYboToKV9jGL jhMYqSEUDOrr87eZRVYFwpDjekNctJ42NrmuoRYX8eIWQmQYJ4eR9WtXzRaelalBj5tvynWT7 JEEFKIl5ZupPMYDrdG8ipz4Q0+7SvwPkXBDzT4O/vlmWDNynmTWoR4YOyz8t4PFZA01fEW2QR 81K+AnNLLTdT7nhhL9qLhIvA1gONtj6plxGwGYJasnMNYx0VY1ExKvmsEDYy2twcoPFgSuerd 5Di3H0JfVj2nz071k8SAQtd5GzHumxGFdbZKLJZ5ym5FIg0hSJQZ8MRDLkFxompXTaDasqN9l s/PB9dvz3xCB5NJJMBMC9+X6ZCPKB2JvoksFdyqwMWjSYn6+1OtKHkepFuKm8RsK/r75M/1/y FCXqv48SzzMAQjgIwz8cSyP41TWqIPW8QqJix6XSGw07okiAhaNq5GmcQ12bOZPvMoqG+82tk Y1lYbEwmlt4hcHHwtrzaT7ag== From: Markus Elfring Date: Wed, 18 Sep 2024 15:34:45 +0200 Add a jump target so that a bit of exception handling can be better reused at the end of this function implementation. Signed-off-by: Markus Elfring --- drivers/md/dm-crypt.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) -- 2.46.0 diff --git a/drivers/md/dm-crypt.c b/drivers/md/dm-crypt.c index dae2fe3cb182..3d2247cfd42b 100644 --- a/drivers/md/dm-crypt.c +++ b/drivers/md/dm-crypt.c @@ -2614,8 +2614,8 @@ static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string key = request_key(type, key_desc + 1, NULL); if (IS_ERR(key)) { - kfree_sensitive(new_key_string); - return PTR_ERR(key); + ret = PTR_ERR(key); + goto free_new_key_string; } down_read(&key->sem); @@ -2623,23 +2623,23 @@ static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string ret = set_key(cc, key); up_read(&key->sem); key_put(key); - if (ret < 0) { - kfree_sensitive(new_key_string); - return ret; - } + if (ret < 0) + goto free_new_key_string; /* clear the flag since following operations may invalidate previously valid key */ clear_bit(DM_CRYPT_KEY_VALID, &cc->flags); ret = crypt_setkey(cc); + if (ret) + goto free_new_key_string; - if (!ret) { - set_bit(DM_CRYPT_KEY_VALID, &cc->flags); - kfree_sensitive(cc->key_string); - cc->key_string = new_key_string; - } else - kfree_sensitive(new_key_string); + set_bit(DM_CRYPT_KEY_VALID, &cc->flags); + kfree_sensitive(cc->key_string); + cc->key_string = new_key_string; + return 0; +free_new_key_string: + kfree_sensitive(new_key_string); return ret; }