Skip to content

Introduce KMOD_STRERROR to ensure usage is safer. #336

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions libkmod/libkmod-builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ static ssize_t get_strings(struct kmod_builtin_info *info, const char *modname,
if (n == -1) {
if (!feof(info->fp)) {
count = -errno;
ERR(info->ctx, "get_string: %s\n", strerror(errno));
ERR(info->ctx, "get_string: %m\n");
}
break;
}
Expand Down Expand Up @@ -182,7 +182,7 @@ ssize_t kmod_builtin_get_modinfo(struct kmod_ctx *ctx, const char *modname,
*modinfo = strbuf_to_vector(&buf, (size_t)count);
if (*modinfo == NULL) {
count = -errno;
ERR(ctx, "strbuf_to_vector: %s\n", strerror(errno));
ERR(ctx, "strbuf_to_vector: %m\n");
}
}

Expand Down
2 changes: 1 addition & 1 deletion libkmod/libkmod-config.c
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ static int kmod_config_parse_kcmdline(struct kmod_config *config)
close(fd);
if (err < 0) {
ERR(config->ctx, "could not read from '/proc/cmdline': %s\n",
strerror(-err));
KMOD_STRERROR(-err));
return err;
}

Expand Down
6 changes: 3 additions & 3 deletions libkmod/libkmod-file-xz.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static void xz_uncompress_belch(struct kmod_file *file, lzma_ret ret)
{
switch (ret) {
case LZMA_MEM_ERROR:
ERR(file->ctx, "xz: %s\n", strerror(ENOMEM));
ERR(file->ctx, "xz: %s\n", KMOD_STRERROR(ENOMEM));
break;
case LZMA_FORMAT_ERROR:
ERR(file->ctx, "xz: File format not recognized\n");
Expand Down Expand Up @@ -128,13 +128,13 @@ int kmod_file_load_xz(struct kmod_file *file)

ret = dlopen_lzma();
if (ret < 0) {
ERR(file->ctx, "xz: can't load and resolve symbols (%s)", strerror(-ret));
ERR(file->ctx, "xz: can't load and resolve symbols (%s)", KMOD_STRERROR(-ret));
return -EINVAL;
}

lzret = sym_lzma_stream_decoder(&strm, UINT64_MAX, LZMA_CONCATENATED);
if (lzret == LZMA_MEM_ERROR) {
ERR(file->ctx, "xz: %s\n", strerror(ENOMEM));
ERR(file->ctx, "xz: %s\n", KMOD_STRERROR(ENOMEM));
return -ENOMEM;
} else if (lzret != LZMA_OK) {
ERR(file->ctx, "xz: Internal error (bug)\n");
Expand Down
2 changes: 1 addition & 1 deletion libkmod/libkmod-file-zlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ int kmod_file_load_zlib(struct kmod_file *file)
ret = dlopen_zlib();
if (ret < 0) {
ERR(file->ctx, "zlib: can't load and resolve symbols (%s)",
strerror(-ret));
KMOD_STRERROR(-ret));
return -EINVAL;
}

Expand Down
2 changes: 1 addition & 1 deletion libkmod/libkmod-file-zstd.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ int kmod_file_load_zstd(struct kmod_file *file)
ret = dlopen_zstd();
if (ret < 0) {
ERR(file->ctx, "zstd: can't load and resolve symbols (%s)",
strerror(-ret));
KMOD_STRERROR(-ret));
return -EINVAL;
}

Expand Down
7 changes: 7 additions & 0 deletions libkmod/libkmod-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@

#define KMOD_EXPORT __attribute__((visibility("default")))


#define KCMD_LINE_SIZE 4096
#ifdef HAVE_STRERRORDESC_NP
#define KMOD_STRERROR(errnum) strerrordesc_np(errnum)
#else
#define KMOD_ERRNO_BUF_LEN 1024
#define KMOD_STRERROR(errnum) strerror_r(errnum, (char[KMOD_ERRNO_BUF_LEN]){}, KMOD_ERRNO_BUF_LEN)
#endif

#ifndef HAVE_SECURE_GETENV
#warning secure_getenv is not available
Expand Down
33 changes: 16 additions & 17 deletions libkmod/libkmod-module.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ void kmod_module_parse_depline(struct kmod_module *mod, char *line)

err = kmod_module_new_from_path(ctx, path, &depmod);
if (err < 0) {
ERR(ctx, "ctx=%p path=%s error=%s\n", ctx, path, strerror(-err));
ERR(ctx, "ctx=%p path=%s error=%s\n", ctx, path, KMOD_STRERROR(-err));
goto fail;
}

Expand Down Expand Up @@ -333,7 +333,7 @@ KMOD_EXPORT int kmod_module_new_from_path(struct kmod_ctx *ctx, const char *path
err = stat(abspath, &st);
if (err < 0) {
err = -errno;
DBG(ctx, "stat %s: %s\n", path, strerror(errno));
DBG(ctx, "stat %s: %m\n", path);
free(abspath);
return err;
}
Expand Down Expand Up @@ -663,8 +663,7 @@ static int do_init_module(struct kmod_module *mod, unsigned int flags, const cha

stripped = kmod_elf_strip(elf, flags);
if (stripped == NULL) {
ERR(mod->ctx, "Failed to strip version information: %s\n",
strerror(errno));
ERR(mod->ctx, "Failed to strip version information: %m\n");
return -errno;
}
mem = stripped;
Expand Down Expand Up @@ -713,7 +712,7 @@ KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod, unsigned int
err = do_init_module(mod, flags, args);

if (err < 0)
INFO(mod->ctx, "Failed to insert module '%s': %s\n", path, strerror(-err));
INFO(mod->ctx, "Failed to insert module '%s': %s\n", path, KMOD_STRERROR(-err));

return err;
}
Expand Down Expand Up @@ -892,7 +891,7 @@ static int __kmod_module_fill_softdep(struct kmod_module *mod, struct kmod_list

err = kmod_module_get_softdeps(mod, &pre, &post);
if (err < 0) {
ERR(mod->ctx, "could not get softdep: %s\n", strerror(-err));
ERR(mod->ctx, "could not get softdep: %s\n", KMOD_STRERROR(-err));
goto fail;
}

Expand Down Expand Up @@ -1361,7 +1360,7 @@ KMOD_EXPORT int kmod_module_new_from_loaded(struct kmod_ctx *ctx, struct kmod_li
fp = fopen("/proc/modules", "re");
if (fp == NULL) {
int err = -errno;
ERR(ctx, "could not open /proc/modules: %s\n", strerror(errno));
ERR(ctx, "could not open /proc/modules: %m\n");
return err;
}

Expand All @@ -1375,7 +1374,7 @@ KMOD_EXPORT int kmod_module_new_from_loaded(struct kmod_ctx *ctx, struct kmod_li
err = kmod_module_new_from_name(ctx, name, &m);
if (err < 0) {
ERR(ctx, "could not get module from name '%s': %s\n", name,
strerror(-err));
KMOD_STRERROR(-err));
goto eat_line;
}

Expand Down Expand Up @@ -1434,7 +1433,7 @@ KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod)
if (fd < 0) {
err = -errno;

DBG(mod->ctx, "could not open '%s': %s\n", path, strerror(-err));
DBG(mod->ctx, "could not open '%s': %s\n", path, KMOD_STRERROR(-err));

if (pathlen > (int)sizeof("/initstate") - 1) {
struct stat st;
Expand All @@ -1443,14 +1442,14 @@ KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod)
return KMOD_MODULE_COMING;
}

DBG(mod->ctx, "could not open '%s': %s\n", path, strerror(-err));
DBG(mod->ctx, "could not open '%s': %s\n", path, KMOD_STRERROR(-err));
return err;
}

err = read_str_safe(fd, buf, sizeof(buf));
close(fd);
if (err < 0) {
ERR(mod->ctx, "could not read from '%s': %s\n", path, strerror(-err));
ERR(mod->ctx, "could not read from '%s': %s\n", path, KMOD_STRERROR(-err));
return err;
}

Expand Down Expand Up @@ -1498,7 +1497,7 @@ KMOD_EXPORT long kmod_module_get_size(const struct kmod_module *mod)
fp = fopen("/proc/modules", "re");
if (fp == NULL) {
int err = -errno;
ERR(mod->ctx, "could not open /proc/modules: %s\n", strerror(errno));
ERR(mod->ctx, "could not open /proc/modules: %m\n");
close(dfd);
return err;
}
Expand Down Expand Up @@ -1550,15 +1549,15 @@ KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod)
fd = open(path, O_RDONLY | O_CLOEXEC);
if (fd < 0) {
err = -errno;
DBG(mod->ctx, "could not open '%s': %s\n", path, strerror(errno));
DBG(mod->ctx, "could not open '%s': %m\n", path);
return err;
}

err = read_str_long(fd, &refcnt, 10);
close(fd);
if (err < 0) {
ERR(mod->ctx, "could not read integer from '%s': '%s'\n", path,
strerror(-err));
KMOD_STRERROR(-err));
return err;
}

Expand All @@ -1579,7 +1578,7 @@ KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module *

d = opendir(dname);
if (d == NULL) {
ERR(mod->ctx, "could not open '%s': %s\n", dname, strerror(errno));
ERR(mod->ctx, "could not open '%s': %m\n", dname);
return NULL;
}

Expand All @@ -1597,7 +1596,7 @@ KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module *
err = kmod_module_new_from_name(mod->ctx, dent->d_name, &holder);
if (err < 0) {
ERR(mod->ctx, "could not create module for '%s': %s\n",
dent->d_name, strerror(-err));
dent->d_name, KMOD_STRERROR(-err));
goto fail;
}

Expand Down Expand Up @@ -1645,7 +1644,7 @@ KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module

d = opendir(dname);
if (d == NULL) {
ERR(mod->ctx, "could not open '%s': %s\n", dname, strerror(errno));
ERR(mod->ctx, "could not open '%s': %m\n", dname);
return NULL;
}

Expand Down
14 changes: 7 additions & 7 deletions libkmod/libkmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ static enum kmod_file_compression_type get_kernel_compression(struct kmod_ctx *c
err = read_str_safe(fd, buf, sizeof(buf));
close(fd);
if (err < 0) {
ERR(ctx, "could not read from '%s': %s\n", path, strerror(-err));
ERR(ctx, "could not read from '%s': %s\n", path, KMOD_STRERROR(-err));
return KMOD_FILE_COMPRESSION_NONE;
}

Expand Down Expand Up @@ -385,7 +385,7 @@ static int kmod_lookup_alias_from_alias_bin(struct kmod_ctx *ctx,
err = kmod_module_new_from_alias(ctx, name, realname->value, &mod);
if (err < 0) {
ERR(ctx, "Could not create module for alias=%s realname=%s: %s\n",
name, realname->value, strerror(-err));
name, realname->value, KMOD_STRERROR(-err));
goto fail;
}

Expand Down Expand Up @@ -492,7 +492,7 @@ int kmod_lookup_alias_from_builtin_file(struct kmod_ctx *ctx, const char *name,
err = kmod_module_new_from_name(ctx, name, &mod);
if (err < 0) {
ERR(ctx, "Could not create module from name %s: %s\n", name,
strerror(-err));
KMOD_STRERROR(-err));
return err;
}

Expand Down Expand Up @@ -544,7 +544,7 @@ int kmod_lookup_alias_from_moddep_file(struct kmod_ctx *ctx, const char *name,
n = kmod_module_new_from_name(ctx, name, &mod);
if (n < 0) {
ERR(ctx, "Could not create module from name %s: %s\n", name,
strerror(-n));
KMOD_STRERROR(-n));
goto finish;
}

Expand Down Expand Up @@ -586,7 +586,7 @@ int kmod_lookup_alias_from_config(struct kmod_ctx *ctx, const char *name,
if (err < 0) {
ERR(ctx,
"Could not create module for alias=%s modname=%s: %s\n",
name, modname, strerror(-err));
name, modname, KMOD_STRERROR(-err));
goto fail;
}

Expand Down Expand Up @@ -628,7 +628,7 @@ int kmod_lookup_alias_from_commands(struct kmod_ctx *ctx, const char *name,
err = kmod_module_new_from_name(ctx, modname, &mod);
if (err < 0) {
ERR(ctx, "Could not create module from name %s: %s\n",
modname, strerror(-err));
modname, KMOD_STRERROR(-err));
return err;
}

Expand Down Expand Up @@ -665,7 +665,7 @@ int kmod_lookup_alias_from_commands(struct kmod_ctx *ctx, const char *name,
err = kmod_module_new_from_name(ctx, modname, &mod);
if (err < 0) {
ERR(ctx, "Could not create module from name %s: %s\n",
modname, strerror(-err));
modname, KMOD_STRERROR(-err));
return err;
}

Expand Down
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ cdata.set10('_GNU_SOURCE', true)

_funcs = [
'open64', 'stat64', 'fopen64', '__stat64_time64',
'secure_getenv',
'secure_getenv', 'strerrordesc_np',
]
foreach func : _funcs
if cc.has_function(func, args : '-D_GNU_SOURCE')
Expand Down
Loading