mirror of
https://git.proxmox.com/git/mirror_zfs.git
synced 2026-05-22 10:37:35 +03:00
Cleanup: Switch to strlcpy from strncpy
Coverity found a bug in `zfs_secpolicy_create_clone()` where it is
possible for us to pass an unterminated string when `zfs_get_parent()`
returns an error. Upon inspection, it is clear that using `strlcpy()`
would have avoided this issue.
Looking at the codebase, there are a number of other uses of `strncpy()`
that are unsafe and even when it is used safely, switching to
`strlcpy()` would make the code more readable. Therefore, we switch all
instances where we use `strncpy()` to use `strlcpy()`.
Unfortunately, we do not portably have access to `strlcpy()` in
tests/zfs-tests/cmd/zfs_diff-socket.c because it does not link to
libspl. Modifying the appropriate Makefile.am to try to link to it
resulted in an error from the naming choice used in the file. Trying to
disable the check on the file did not work on FreeBSD because Clang
ignores `#undef` when a definition is provided by `-Dstrncpy(...)=...`.
We workaround that by explictly including the C file from libspl into
the test. This makes things build correctly everywhere.
We add a deprecation warning to `config/Rules.am` and suppress it on the
remaining `strncpy()` usage. `strlcpy()` is not portably avaliable in
tests/zfs-tests/cmd/zfs_diff-socket.c, so we use `snprintf()` there as a
substitute.
This patch does not tackle the related problem of `strcpy()`, which is
even less safe. Thankfully, a quick inspection found that it is used far
more correctly than strncpy() was used. A quick inspection did not find
any problems with `strcpy()` usage outside of zhack, but it should be
said that I only checked around 90% of them.
Lastly, some of the fields in kstat_t varied in size by 1 depending on
whether they were in userspace or in the kernel. The origin of this
discrepancy appears to be 04a479f706 where
it was made for no apparent reason. It conflicts with the comment on
KSTAT_STRLEN, so we shrink the kernel field sizes to match the userspace
field sizes.
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: Ryan Moeller <ryan@iXsystems.com>
Signed-off-by: Richard Yao <richard.yao@alumni.stonybrook.edu>
Closes #13876
This commit is contained in:
@@ -85,7 +85,7 @@ typedef int kid_t; /* unique kstat id */
|
||||
typedef int kstat_update_t(struct kstat_s *, int); /* dynamic update cb */
|
||||
|
||||
typedef struct kstat_module {
|
||||
char ksm_name[KSTAT_STRLEN+1]; /* module name */
|
||||
char ksm_name[KSTAT_STRLEN]; /* module name */
|
||||
struct list_head ksm_module_list; /* module linkage */
|
||||
struct list_head ksm_kstat_list; /* list of kstat entries */
|
||||
struct proc_dir_entry *ksm_proc; /* proc entry */
|
||||
@@ -98,8 +98,8 @@ typedef struct kstat_raw_ops {
|
||||
} kstat_raw_ops_t;
|
||||
|
||||
typedef struct kstat_proc_entry {
|
||||
char kpe_name[KSTAT_STRLEN+1]; /* kstat name */
|
||||
char kpe_module[KSTAT_STRLEN+1]; /* provider module name */
|
||||
char kpe_name[KSTAT_STRLEN]; /* kstat name */
|
||||
char kpe_module[KSTAT_STRLEN]; /* provider module name */
|
||||
kstat_module_t *kpe_owner; /* kstat module linkage */
|
||||
struct list_head kpe_list; /* kstat linkage */
|
||||
struct proc_dir_entry *kpe_proc; /* procfs entry */
|
||||
@@ -111,7 +111,7 @@ struct kstat_s {
|
||||
hrtime_t ks_crtime; /* creation time */
|
||||
hrtime_t ks_snaptime; /* last access time */
|
||||
int ks_instance; /* provider module instance */
|
||||
char ks_class[KSTAT_STRLEN+1]; /* kstat class */
|
||||
char ks_class[KSTAT_STRLEN]; /* kstat class */
|
||||
uchar_t ks_type; /* kstat data type */
|
||||
uchar_t ks_flags; /* kstat flags */
|
||||
void *ks_data; /* kstat type-specific data */
|
||||
@@ -177,7 +177,7 @@ typedef struct kstat_io {
|
||||
} kstat_io_t;
|
||||
|
||||
typedef struct kstat_timer {
|
||||
char name[KSTAT_STRLEN+1]; /* event name */
|
||||
char name[KSTAT_STRLEN]; /* event name */
|
||||
u_longlong_t num_events; /* number of events */
|
||||
hrtime_t elapsed_time; /* cumulative elapsed time */
|
||||
hrtime_t min_time; /* shortest event duration */
|
||||
|
||||
Reference in New Issue
Block a user