diff mbox series

[v1,1/2] net: rfkill: Fix a wrongly handling error case

Message ID 1717771212-30723-2-git-send-email-quic_zijuhu@quicinc.com (mailing list archive)
State Changes Requested
Delegated to: Johannes Berg
Headers show
Series net: rfkill: Fix 2 bugs within rfkill_set_hw_state_reason() | expand

Commit Message

quic_zijuhu June 7, 2024, 2:40 p.m. UTC
Kernel API rfkill_set_hw_state_reason() does not return current combined
block state when its parameter @reason is invalid, that is wrong according
to its comments.

Fixed by returning API required value, also use pr_err() instead of WARN()
for this error case handling.

Fixes: 14486c82612a ("rfkill: add a reason to the HW rfkill state")
Signed-off-by: Zijun Hu <quic_zijuhu@quicinc.com>
---
 net/rfkill/core.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

Comments

Johannes Berg June 12, 2024, 8:15 a.m. UTC | #1
> 
> use pr_err() instead of WARN()
> for this error case handling.

I don't see anything wrong with the WARN here, it's the user/driver
calling it completely incorrectly.

I also don't really think this is a *fix*, if you used the API
incorrectly you can't necessarily expect a correct return value, I
guess, but anyway it shouldn't happen in the first place.

I'm happy to take the return value change (only) as a cleanup, if you
wish to resend that.

> Fixed by

Please also read
https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches

johannes
quic_zijuhu June 12, 2024, 10:12 a.m. UTC | #2
On 6/12/2024 4:15 PM, Johannes Berg wrote:
>>
>> use pr_err() instead of WARN()
>> for this error case handling.
> 
> I don't see anything wrong with the WARN here, it's the user/driver
> calling it completely incorrectly.
> 
the function is a kernel API and it is handing invalid user input.
below comments for WARN() seems say that pr_err() is better than WARN()
for this case.

include/asm-generic/bug.h:
/*
 * WARN(), WARN_ON(), WARN_ON_ONCE(), and so on can be used to report
 * significant kernel issues that need prompt attention if they should ever
 * appear at runtime.
 *
 * Do not use these macros when checking for invalid external inputs
 * (e.g. invalid system call arguments, or invalid data coming from
 * network/devices), and on transient conditions like ENOMEM or EAGAIN.
 * These macros should be used for recoverable kernel issues only.
 * For invalid external inputs, transient conditions, etc use
 * pr_err[_once/_ratelimited]() followed by dump_stack(), if necessary.
 * Do not include "BUG"/"WARNING" in format strings manually to make these
 * conditions distinguishable from kernel issues.
 *
 * Use the versions with printk format strings to provide better
diagnostics.
 */

> I also don't really think this is a *fix*, if you used the API
> incorrectly you can't necessarily expect a correct return value, I
> guess, but anyway it shouldn't happen in the first place.
> 
okay, will remove term fix and fix tag. the API returns type bool for
block state, the type bool can't cover case for invalid user input.

> I'm happy to take the return value change (only) as a cleanup, if you
> wish to resend that.
> 
i am pleasure to resend it after code review done.
>> Fixed by
> 
> Please also read
> https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches
> 
okay, thank you
> johannes
Johannes Berg June 12, 2024, 10:14 a.m. UTC | #3
On Wed, 2024-06-12 at 18:12 +0800, quic_zijuhu wrote:
> On 6/12/2024 4:15 PM, Johannes Berg wrote:
> > > 
> > > use pr_err() instead of WARN()
> > > for this error case handling.
> > 
> > I don't see anything wrong with the WARN here, it's the user/driver
> > calling it completely incorrectly.
> > 
> the function is a kernel API

Sure.

> and it is handing invalid user input.

No.

johannes
diff mbox series

Patch

diff --git a/net/rfkill/core.c b/net/rfkill/core.c
index c3feb4f49d09..0dc982b4fce6 100644
--- a/net/rfkill/core.c
+++ b/net/rfkill/core.c
@@ -543,13 +543,15 @@  bool rfkill_set_hw_state_reason(struct rfkill *rfkill,
 {
 	unsigned long flags;
 	bool ret, prev;
+	const unsigned long reason_mask = RFKILL_HARD_BLOCK_SIGNAL |
+		RFKILL_HARD_BLOCK_NOT_OWNER;
 
 	BUG_ON(!rfkill);
 
-	if (WARN(reason &
-	    ~(RFKILL_HARD_BLOCK_SIGNAL | RFKILL_HARD_BLOCK_NOT_OWNER),
-	    "hw_state reason not supported: 0x%lx", reason))
-		return blocked;
+	if (reason & ~reason_mask) {
+		pr_err("hw_state reason not supported: 0x%lx\n", reason);
+		return rfkill_blocked(rfkill);
+	}
 
 	spin_lock_irqsave(&rfkill->lock, flags);
 	prev = !!(rfkill->hard_block_reasons & reason);