Re: [PATCH] kmod/depmod: explicitly include the header for basename()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Apr 08, 2024 at 08:22:01PM +0000, Laurent Bercot wrote:
Hello list,

(Please Cc: me on the replies.)

Recent versions of the musl libc declare basename() (and dirname())
exclusively in <libgen.h>, as specified by POSIX. If this header is
not properly included, when building kmod with musl, basename() is an
unknown symbol and will be assumed as returning int, which causes
problems as soon as kmod performs a printf("%s", basename(argv[0])),
e.g. in kmod_help(). (On x86_64, int is 32 bit, so the pointer address
is truncated, which causes a segfault on access.)

Simply including libgen.h wherever basename() is used, i.e. depmod.c

that is the wrong basename though.
https://man7.org/linux/man-pages/man3/basename.3.html

       There are two different versions of basename() - the POSIX
       version described above, and the GNU version, which one gets
       after

               #define _GNU_SOURCE         /* See feature_test_macros(7) */
               #include <string.h>

       The GNU version never modifies its argument, and returns the
       empty string when path has a trailing slash, and in particular
       also when it is "/".  There is no GNU version of dirname().

We only ever use and want the gnu behavior, that doesn't modify the
argument. Which is the proper thing to do for a basename()
implementation.

There's a pending patch I need to review:
https://github.com/kmod-project/kmod/pull/32

does that fix it for you?

thanks
Lucas De Marchi

and kmod.c, fixes the issue. It will print warnings because you store
the result in a const char *, but these are harmless and can be fixed
later.

None of the kmod files seems to use dirname(), but several of them use
dirname as a symbol, including depmod.c, where it will shadow the libc's
dirname symbol. This does not cause a problem right now, but it might be
a good idea to rename the dirname variables at some point.


diff -rNU3 kmod-32.old/tools/depmod.c kmod-32/tools/depmod.c
--- kmod-32.old/tools/depmod.c  2023-12-06 16:34:31.000000000 +0100
+++ kmod-32/tools/depmod.c      2024-04-08 20:55:03.998592078 +0200
@@ -22,6 +22,7 @@
#include <dirent.h>
#include <errno.h>
#include <getopt.h>
+#include <libgen.h>
#include <limits.h>
#include <regex.h>
#include <stdio.h>
diff -rNU3 kmod-32.old/tools/kmod.c kmod-32/tools/kmod.c
--- kmod-32.old/tools/kmod.c    2024-02-20 23:10:55.000000000 +0100
+++ kmod-32/tools/kmod.c        2024-04-08 21:55:03.888577992 +0200
@@ -19,6 +19,7 @@

#include <errno.h>
#include <getopt.h>
+#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>





[Index of Archives]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Big List of Linux Books]

  Powered by Linux