can: j1939: return 0 instead of user value in j1939_sk_setsockopt_flag()
Subsystems (5)
PATCH 1/1 can: j1939: return 0 instead of user value in j1939_sk_setsockopt_flag()
In j1939_sk_setsockopt_flag(), after copying the integer option value tmp from userspace and updating jsk->state, the function returns tmp instead of 0.
If a caller passes a negative integer (e.g., -1) to enable SO_J1939_BROADCAST or SO_J1939_PROMISC, if (tmp) sets jsk->state |= flag and then returns the negative value as an error code from setsockopt(). If a caller passes a positive non-zero integer (e.g., 1), setsockopt() violates POSIX by returning 1 instead of 0.
Return 0 on success in j1939_sk_setsockopt_flag().
--- net/can/j1939/socket.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/can/j1939/socket.c +1 -1
@@ -688,7 +688,7 @@ static int j1939_sk_setsockopt_flag(struct j1939_sock *jsk, sockptr_t optval,else jsk->state &= ~flag;release_sock (&jsk->sk); -return tmp; +return 0 ; } static int j1939_sk_setsockopt(struct socket *sock, int level, int optname,
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
Quoted 10 lines · Hui Peng
In
j1939_sk_setsockopt_flag(), after copying the integer option valuetmpfrom userspace and updatingjsk->state, the function returnstmpinstead of0.If a caller passes a negative integer (e.g.,
-1) to enableSO_J1939_BROADCASTorSO_J1939_PROMISC,if (tmp)setsjsk->state |= flagand then returns the negative value as an error code fromsetsockopt(). If a caller passes a positive non-zero integer (e.g.,1),setsockopt()violates POSIX by returning1instead of0.
Quoted 5 lines
Return
0on success inj1939_sk_setsockopt_flag().Fixes 9d71dd0c7009 ("can: add support of SAE J1939 protocol")Assisted-by LLMSigned-off-by Hui Peng <[email protected]>
I agree that the problem described exists, and that your patch addresses it. That the but was introduced in the cited commit. And that the corrected
behaviour matches that described in Documentation/networking/j1939.rst for SO_J1939_ERRQUEUE and SO_J1939_PROMISC, the two users of j1939_sk_setsockopt_flag().
"The acceptable value size for this option is ``sizeof(int)``, and the
value is only differentiated between 0 and non-zero."