kmod-project / kmod

kmod - Linux kernel module handling
GNU Lesser General Public License v2.1
40 stars 34 forks source link

depmod: Actually use scratch buffer memory #166

Closed stoeckmann closed 3 days ago

stoeckmann commented 4 days ago

Accessing the backing "alias" char buffer directly works only as long as all symbols are smaller than 1024, because otherwise the scratch buffer allocates memory and the addresses do not match anymore.

Proof of Concept:

  1. Create a temporary modules directory containing a module with two symbols, aa and 1025*A (i.e. longer than 1024 characters)
    MYDIR=$(mktemp -d)
    KDIR=$MYDIR/lib/modules/$(uname -r)
    mkdir -p $KDIR/kernel/sub
    cat > $MYDIR/mod.ko.xz.b64 << EOF
    /Td6WFoAAATm1rRGBMBvmQohARwAAAAAAAAAAMNcVP3gBRgAZ10AP5FFhGhEVEmcAlZtZy+BVw2o
    524UVW19oueqRkgFvigdxpHbOYyTU7Kx/hlefDfD+r/tFCQlBcHuPRCbnxuRTPRldwa2DYFC2g0c
    DdY1yxHvF6tv9kbWqc8xpwpQ8YWKVB8MXvUAAAAAPR9nVoEFabMAAYsBmQoAAI4ynUexxGf7AgAA
    AAAEWVo=
    EOF
    base64 -d $MYDIR/mod.ko.xz.b64 | xz -cd > $KDIR/kernel/sub/mod.ko
  2. Run depmod with warnings enabled
    depmod -b $MYDIR -o $MYDIR/out -w

You can see, among other warnings, this one:

depmod: WARNING: duplicate module syms:
symbol:aa mod

Since the 1025*A symbol name triggered an allocation within the scratch buffer, the first symbol name aa has been added twice. The 1025*A symbol name can be seen in written modules.symbols file, but not in modules.symbols.bin (because the scratch buffer issue is triggered in second output).

stoeckmann commented 4 days ago

On a related note since you are reviewing these things. I was thinking of blending strbuf and scratchbuf, always allowing to start with a stack buffer and if NULL is passed, then the current behavior for strbuf is retained. Any thoughts?

I've had an idea like that as well, also possibly backing array creations with scratchbuf. The concept of scratchbuf is awesome, since a lot of allocations could be skipped when it comes to heuristics and common module setups in distributions.

But I didn't make any progress into that direction, so: Yes I would like to see that, but have no code to share. :D

lucasdemarchi commented 4 days ago

But I didn't make any progress into that direction, so: Yes I would like to see that, but have no code to share. :D

I started doing that and left saved on a branch... I will try to finish it and submit as a PR

lucasdemarchi commented 3 days ago

Applied, thanks.