Fix retrieve user value in IOCTL_VALSET_NUM case#359
Fix retrieve user value in IOCTL_VALSET_NUM case#359applepwc wants to merge 1 commit intosysprog21:masterfrom
Conversation
jserv
left a comment
There was a problem hiding this comment.
Remove "fix: " in the subject of git commit messages.
Refine git commit message, making it more informative. See https://chris.beams.io/git-commit
Don't close this pull request. Use git force-push instead.
examples/ioctl.c
Outdated
|
|
||
| case IOCTL_VALSET_NUM: | ||
| ioctl_num = arg; | ||
| int user_val; |
There was a problem hiding this comment.
To be compatible with the old version, which might still use the C89 standard.
Don't declare the variable inside the switch statement.
Or, you should declare the variable within the block scope.
Also, if you want to declare the variable inside the switch statement, please fix this:
ioctl.c:78:9: error: typename in expression
ioctl.c:78:13: error: Expected ; at end of statement
ioctl.c:78:13: error: got user_val
ioctl.c:78:9: error: undefined identifier 'int'
ioctl.c:79:13: error: undefined identifier 'user_val'
ioctl.c:81:21: error: undefined identifier 'user_val'
|
Hello, I force-pushed a new commit. Please review it. |
Github notifies the relevant people automatically, so there's no need to leave a comment like this. |
|
Thanks for the patch. While this addresses the pointer-to-value mismatch, should we consider whether Currently, the macro is defined as: Using |
jserv
left a comment
There was a problem hiding this comment.
Ensure consistent code style with 'clang-format' in examples directory.
The original implementation of IOCTL_VALSET_NUM incorrectly treated the ioctl 'arg' parameter as the integer value itself, when it's actually a user-space pointer to an integer. This caused the global ioctl_num variable to be set to the memory address of the user-space pointer rather than the intended integer value, leading to incorrect behavior and potential security issues. Fix by properly dereferencing the user pointer using get_user() to safely copy the integer value from user space to kernel space.
|
Yes, using _IO with pass-by-value is more idiomatic and avoids unnecessary memory copying. |
|
Wouldn't the proposed patch also change the existing ABI? |
|
Hello, I'm uncertain whether to add more code as shown below or simply rewrite the original. #define IOCTL_VALSET_NUM2 _IO(IOC_MAGIC, 4)
#define IOCTL_VAL_MAXNR 4
...
case IOCTL_VALSET_NUM2:
ioctl_num = (int)arg;
break; |
The test program is:
Summary by cubic
Fix IOCTL_VALSET_NUM to read the integer via get_user from the user pointer instead of using arg directly. Returns -EFAULT on bad pointers, prevents invalid memory access, and makes the set/get test pass.
Written for commit a507630. Summary will update on new commits.