Skip to content
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

chore(driver): properly use strscpy. #1632

Merged
merged 3 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion driver/API_VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8.0.1
8.0.2
32 changes: 32 additions & 0 deletions driver/ppm_events.c
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,37 @@ int val_to_ring(struct event_filler_arguments *args, uint64_t val, uint32_t val_
}
else
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0)
// strscpy is available since kernel 4.3.0: https://github.com/torvalds/linux/commit/30035e45753b708e7d47a98398500ca005e02b86
len = (int)strscpy(args->buffer + args->arg_data_offset,
(const char *)(unsigned long)val,
max_arg_size);
/* WARNING: `strscpy` returns the length of the string it creates or -E2BIG in case
* the resulting string would not fit inside the destination string.
* (see https://elixir.bootlin.com/linux/latest/source/lib/string.c#L122 and
* https://lwn.net/Articles/659214/)
*
* The copied string is always null terminated but the returned `len` doesn't
* take account for it.
*
* Two possible cases here:
*
* 1. `len < max_arg_size`, the terminator is always there, but `len` doesn't take it into account,
* so we need to increment the `len`.
*
* 2. `len == -E2BIG`, the source string is >= than `max_arg_size`. `strscpy` copied
* `max_arg_size - 1` and added the `\0` at the end, so our final copied `len` is `max_arg_size`.
*/
if (len == -E2BIG)
{
len = max_arg_size;
}
else
{
len++;
}
#else
// Use old `strlcpy`.
len = (int)strlcpy(args->buffer + args->arg_data_offset,
(const char *)(unsigned long)val,
max_arg_size);
Expand All @@ -672,6 +703,7 @@ int val_to_ring(struct event_filler_arguments *args, uint64_t val, uint32_t val_
{
len = max_arg_size;
}
#endif
}
break;

Expand Down
Loading