anbox / anbox-modules

Anbox kernel modules
327 stars 224 forks source link

Error while compile kernel modules: modpost: "kallsyms_lookup_name" undefined! #84

Closed raffaem closed 2 years ago

raffaem commented 3 years ago

I am trying to follow these instructions to get Anbox running on a Fedora 34 Workstation.

However I get errors when I try to build the kernel modules. In particular:

$ sudo dkms install anbox-ashmem/1

Kernel preparation unnecessary for this kernel.  Skipping...

Building module:
cleaning build area...
make -j12 KERNELRELEASE=5.13.12-200.fc34.x86_64 all KERNEL_SRC=/lib/modules/5.13.12-200.fc34.x86_64/build...(bad exit status: 2)
Error! Bad return status for module build on kernel: 5.13.12-200.fc34.x86_64 (x86_64)
Consult /var/lib/dkms/anbox-ashmem/1/build/make.log for more information.

and that log file contains:

DKMS make.log for anbox-ashmem-1 for kernel 5.13.12-200.fc34.x86_64 (x86_64)
Tue Aug 31 08:08:57 PM CEST 2021
make -C /lib/modules/5.13.12-200.fc34.x86_64/build V=0 M=$PWD
make[1]: Entering directory '/usr/src/kernels/5.13.12-200.fc34.x86_64'
  CC [M]  /var/lib/dkms/anbox-ashmem/1/build/deps.o
  CC [M]  /var/lib/dkms/anbox-ashmem/1/build/ashmem.o
  LD [M]  /var/lib/dkms/anbox-ashmem/1/build/ashmem_linux.o
  MODPOST /var/lib/dkms/anbox-ashmem/1/build/Module.symvers
ERROR: modpost: "kallsyms_lookup_name" [/var/lib/dkms/anbox-ashmem/1/build/ashmem_linux.ko] undefined!
make[2]: *** [scripts/Makefile.modpost:150: /var/lib/dkms/anbox-ashmem/1/build/Module.symvers] Error 1
make[2]: *** Deleting file '/var/lib/dkms/anbox-ashmem/1/build/Module.symvers'
make[1]: *** [Makefile:1778: modules] Error 2
make[1]: Leaving directory '/usr/src/kernels/5.13.12-200.fc34.x86_64'
make: *** [Makefile:12: all] Error 2
sickcodes commented 2 years ago

Hey @raffaem, @choff has got this working up to 5.13 on his fork: https://github.com/choff/anbox-modules

goisneto commented 2 years ago

I solved with this -> https://github.com/xcellerator/linux_kernel_hacking/issues/3#issue-782815541 I created this script to solve this automatically:

#!/bin/bash
cd /var/lib/dkms/;
cat > patch.c <<EOF
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/kprobes.h>
#include "patch.h"

#define KPROBE_PRE_HANDLER(fname) static int __kprobes fname(struct kprobe *p, struct pt_regs *regs)

long unsigned int kln_addr = 0;
unsigned long (*kln_pointer)(const char* name) = NULL;

static struct kprobe kp0, kp1;

KPROBE_PRE_HANDLER(handler_pre0) {
    kln_addr = (--regs->ip);

    return 0;
}

KPROBE_PRE_HANDLER(handler_pre1) {
    return 0;
}

static int do_register_kprobe(struct kprobe* kp, char* symbol_name, void* handler) {
    int ret;

    kp->symbol_name = symbol_name;
    kp->pre_handler = handler;

    ret = register_kprobe(kp);
    if (ret < 0) {
        pr_err("do_register_kprobe: failed to register for symbol %s, returning %d\n", symbol_name, ret);
        return ret;
    }

    pr_info("Planted krpobe for symbol %s at %p\n", symbol_name, kp->addr);

    return ret;
}

// this is the function that I have modified, as the name suggests it returns a pointer to the extracted kallsyms_lookup_name function
kln_p get_kln_p(void) {
    int status;

    status = do_register_kprobe(&kp0, "kallsyms_lookup_name", handler_pre0);

    if (status < 0) return NULL;

    status = do_register_kprobe(&kp1, "kallsyms_lookup_name", handler_pre1);

    if (status < 0) {
        // cleaning initial krpobe
        unregister_kprobe(&kp0);
        return NULL;
    }

    unregister_kprobe(&kp0);
    unregister_kprobe(&kp1);

    pr_info("kallsyms_lookup_name address = 0x%lx\n", kln_addr);

    kln_pointer = (unsigned long (*)(const char* name)) kln_addr;

    return kln_pointer;
}
EOF
cat > patch.h <<EOF
#ifndef PATCH_H
#define PATCH_H

typedef unsigned long (*kln_p)(const char*);
kln_p get_kln_p(void);

#endif
EOF
for path in /var/lib/dkms/anbox/{ashmem,binder}/1/source/ /var/lib/dkms/anbox-{ashmem,binder}/1/source/
do
    [ ! -d ${path} ] && continue
    cp patch.* ${path}

    if [ -f ${path}deps.c ]; then
        sed -i '0,/#include/s//#include "patch.h" \n#include/' ${path}deps.c
        sed -i 's/kallsyms_lookup_name/\(\(kln_p\)get_kln_p\(\)\)/g' ${path}deps.c
    fi
    if [[ "$(echo ${path} | grep ashmem)" != "" ]]; then
        mod=ashmem
    else
        mod=binder
        [ -f ${path}binder.c ] &&  sed -i 's/mmap_sem/mmap_lock/g' ${path}binder.c
    fi
    [ -f ${path}Makefile ] && sed -i "0,/${mod}_linux-y :=/s//${mod}_linux-y := patch.o/" ${path}Makefile

done
rm patch.*

dkms install anbox-ashmem/1
dkms install anbox-binder/1
sickcodes commented 2 years ago

This fork works in 5.14 https://github.com/choff/anbox-modules

AUR too https://aur.archlinux.org/packages/anbox-modules-dkms/

cobalt2727 commented 2 years ago

Hi there - half a year later and I'm having the same problem on Pop!_OS 21.10, running kernel 5.16.11-76051611-generic.

This fork works in 5.14 https://github.com/choff/anbox-modules

Is there a PR for this anywhere?

FaeWulf commented 2 years ago

Hi there - half a year later and I'm having the same problem on Pop!_OS 21.10, running kernel 5.16.11-76051611-generic.

This fork works in 5.14 https://github.com/choff/anbox-modules

Is there a PR for this anywhere?

I also have this issue on Pop!_ 21.10

cobalt2727 commented 2 years ago

@raffaem did you get this fixed on your setup somehow?