-
Notifications
You must be signed in to change notification settings - Fork 594
Make slcan brin the interface up/down with the appropiate commands #3059
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
[Experimental Bot, please feedback here] Yes, this PR appears to meet the NuttX requirements, although some sections could be more explicit. Strengths:
Areas for Improvement:
Example of Improved Impact Section:
By being more thorough and explicit in the Impact and Testing sections, the PR will be easier to review and merge. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @csanchezdll :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoops, some code formatting issues, please fix (see https://nuttx.apache.org/docs/latest/contributing/coding_style.html). Thanks :-)
…racters. Signed-off-by: Carlos Sanchez <[email protected]>
A recent change (apache/nuttx#16199) has made the bitrate setting no longer bring the interface up. Moreover, it is now no longer possible to change bitrate of a CAN interface if it is up. Therefore, slcan needs to bring the interface down to change it. Fortunately, it already had commands to open and close the interface which map nicely to this. Signed-off-by: Carlos Sanchez <[email protected]>
81610c3
to
670badf
Compare
I fixed the format errors and used checkpatch from the nuttx repo. I found the initialization of ifr = {0} that I had fails because format rules want the braces on separate lines. Too much code for little meaning, so I remove the initialization altogether and used = instead of |= when assigning flags. The result is the same. |
@@ -137,8 +137,7 @@ static int caninit(char *candev, int *s, struct sockaddr_can *addr, | |||
syslog(LOG_ERR, "Error opening CAN socket\n"); | |||
return -1; | |||
} | |||
strncpy(ifr.ifr_name, candev, 4); | |||
ifr.ifr_name[4] = '\0'; | |||
strlcpy(ifr.ifr_name, candev, IFNAMSIZ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@csanchezdll suggestion: include the ifr.irf_name[IFNAMSIZ] = '\0'; for safety reason, please also include it to line 365
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was here on my first version of the PR, with the original strncpy. Changing to strlcpy and removing the explicit \0 addition was suggested by @xiaoxiang781216 just above #3059 (comment).
I agree with him, strlcpy is safer, as it is guaranteed to add a \0 (strncpy will only copy the original \0 if it fits in the buffer). Therefore, the explicit \0 is no longer needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@csanchezdll my fault, I ran "man strlcpy" but only take a look at function description. In fact it seems a good solution.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@xiaoxiang781216 do you think these BUGS reported in the man pages aren't a concern:
BUGS
All catenation functions share the same performance problem: Shlemiel the painter.
As a mitigation, compilers are able to transform some calls to catenation functions
into normal copy functions, since strlen(dst) is usually a byproduct of the previous
copy.
strlcpy(3) and strlcat(3) need to read the entire src string, even if the destination
buffer is small. This makes them vulnerable to Denial of Service (DoS) attacks if an
attacker can control the length of the src string. And if not, they're still unnec‐
essarily slow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
strlcpy always add \0 to the destination buffer, so you don't need add '\0' again. DoS is another problem, if you want to address this problem, you need more safer api(strscpy):
https://staticthinking.wordpress.com/2023/10/30/strcpy-strncpy-strlcpy-and-strscpy/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@xiaoxiang781216 @csanchezdll after reading this article I think strscpy is the "way to go". As the author points: new code should use strscpy and strscpy_pad. Unfortunately NuttX doesn't support strscpy() yet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can port safec library from:
https://sourceforge.net/p/safeclib/code/ci/master/tree/
https://github.com/intel/safestringlib
struct ifreq ifr; | ||
|
||
strlcpy(ifr.ifr_name, argv[1], IFNAMSIZ); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please include the null termination here too. It will protect in cases where users pass a longer name than IFNAMSIZ.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, strlcpy is garanteed to ad \0 even if the string source of the copy is longer than the destination buffer.
Summary
A recent change (apache/nuttx#16199) has made the bitrate setting no longer bring the interface up. Moreover, it is now no longer possible to change bitrate of a CAN interface if it is up. Therefore, slcan needs to bring the interface down to change it. Fortunately, it already had commands to open and close the interface which map nicely to this.
Impact
slcan would be unable to change the interface bitrate after the aforementioned (already merged) Nuttx patch, without this change.
Testing
Tested using a second serial line (/dev/gsmtty2) and CAN interface (named can_6_14) on a custom board. The CAN bus is attacked using an adapter talking socketcand protocol:
Before this patch (and with Nuttx updated so the patch there is applied) changing bitrate no longer brings the interface up, so no frames can be seen. After this patch, I can explicitly bring the interface up with the "O" command and see the traffic:
I also checked bringing the interface down with "C" stops receiving frames.
I had to make another small fix to slcan.c (in a separate commit also in this patchset) because it originally only supported CAN interface names of up to 4 characters.