I tried to use ftrace to hook kernel functions in arm64 kernel, but failed. Some important parts of my code are as follows:
static void notrace ftrace_hook_handler(size_t ip, size_t parent_ip, struct ftrace_ops *ops, struct ftrace_regs *fregs)
{
pr_info("ftrace hooking");
return;
}
int my_ftrace_hook {
struct ftrace_ops fops;
fops.func = ftrace_hook_handler;
fops.flags = FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED;
pr_info("symbol: %s, addr: %px\n", "security_path_unlink", addr_security_path_unlink);
err = ftrace_set_filter_ip(&fops, addr_security_path_unlink, 0, 0);
if (err)
{
pr_err("ftrace_set_filter_ip() failed: %d\n", err);
return err;
}
}
but got an error:
# dmesg
[ 945.791221] ftrace_module: loading out-of-tree module taints kernel.
[ 945.791464] ftrace_module: module verification failed: signature and/or required key missing - tainting kernel
[ 945.820649] [ftrace_module] symbol: security_path_unlink, addr: ffffa13fc4fafd10
[ 945.820663] [my_ftrace_hook] ftrace_set_filter_ip() failed: -22
system information is:
# uname -a
Linux ubuntu2204 5.15.0-43-generic #46-Ubuntu SMP Wed Jul 13 06:42:04 UTC 2022 aarch64 aarch64 aarch64 GNU/Linux
# cat /etc/os-release
PRETTY_NAME="Ubuntu 22.04 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04 (Jammy Jellyfish)"
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=jammy
How can I fix this? Is there an open source project to reference?
Any help will be appreciated.
In arm64, use ftrace, before calling function: ftrace_set_filter_ip
Below is sample code:
faddr = ftrace_location_range(hookaddr, hookaddr + AARCH64_INSN_SIZE);
ret = ftrace_set_filter_ip(hook->fops, faddr, 1, 0);
Related
I want to read the value of a 64bit register in the Linux Kernel driver
I tried the below code, but I get 0 always.
reg_addr = ioremap(0xffe602000, 0x20000);
if (!reg_addr) {
printk(KERN_ALERT " ioremap failed \n");
}else{
printk(KERN_ALERT " ioremap success reg_addr[0x%X]\n", reg_addr);
}
the ioremap was successful in the above code.
after the ioremap, I call read as below:
unsigned long long val;
val = ioread64(reg_addr+0x300);
printk(KERN_ALERT " ### REG READ returning [%lx] ###\n",val);
the above code prints
### REG READ returning [0] ###
I checked using Trace32, the value of register is not 0, the real value of the register is 0x00000000e506a904.
did I miss something?
kernel version:
4.19.33 #12 SMP ppc64 GNU/Linux
I have a kernel module where I'm trying to use mtrr_add() function. When I compile my code, I'm getting WARNING: "mtrr_add" [/path/to/module.ko] undefined!. I found a mail list message: https://patchwork.ozlabs.org/patch/510277/ which mentions the removal of mtrr_add() and replace with arch_phys_wc_add(). If I understood it correctly, arch_phys_wc_add() is for adding Write Combine (WC) type. Is there any replacement function for mtrr_add() that will add writeback(WB) type? Or any workaround?
My init module code is following,
#include <asm/mtrr.h>
//#include <uapi/asm/mtrr.h>
static int __init car_init(void){
printk(KERN_INFO "car module loaded\n");
int mtrr_add_r = mtrr_add(0xf7c20000, 4096, MTRR_TYPE_WRBACK,0); <-- throwing undefined warning
//int mtrr_add_r = arch_phys_wc_add(0xf7c20000, 4096);
printk( KERN_ALERT "mtrr_add_r: 0x%x\n",mtrr_add_r);
set_cpus_allowed_ptr(current,&saved_cpu_mask);
return 0;
}
dmesg shows:
module_name: Unknown symbol mtrr_add (err 0)
My kernel version : Linux xxx 4.4.0-200-generic #232-Ubuntu SMP
gcc version : gcc (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609
I am doing my own alsa project.
Having changed OS (fresh installation / no upgrade) from
PRETTY_NAME="Raspbian GNU/Linux 7 (wheezy)"
NAME="Raspbian GNU/Linux"
VERSION_ID="7"
VERSION="7 (wheezy)"
ID=raspbian
ID_LIKE=debian
4.1.19-v7+ #858 SMP Tue Mar 15 15:56:00 GMT 2016 armv7l GNU/Linux
to
PRETTY_NAME="Raspbian GNU/Linux 9 (stretch)" NAME="Raspbian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"
ID=raspbian
ID_LIKE=debian
4.14.93-v7+ #1191 SMP Wed Jan 16 11:53:33 GMT 2019 armv7l GNU/Linux
I have additionally downloaded / installed
alsa-lib-1.1.8.tar.bz2
without any errors!
Project wasn't touched inbetween - all alsa sources did compile fine under wheezy before!!!
Using the same "Makefile" under stretch I now face the following 'invalid storage class' error when compiling the project with alsa library / sources (included from /usr/include/alsa) again:
In file included from /usr/include/alsa/asoundlib.h:54:0,
from IPI2Cserver.c:3764:
/usr/include/alsa/pcm.h:507:23: error: invalid storage class for function ‘snd_pcm_abort’
static __inline__ int snd_pcm_abort(snd_pcm_t *pcm) { return snd_pcm_nonblock(pcm, 2); }
^~~~~~~~~~~~~
In file included from /usr/include/alsa/asoundlib.h:54:0,
from IPI2Cserver.c:3764:
/usr/include/alsa/pcm.h:1030:20: error: invalid storage class for function ‘snd_pcm_pack_audio_tstamp_config’
static inline void snd_pcm_pack_audio_tstamp_config(unsigned int *data,
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/alsa/pcm.h:1038:20: error: invalid storage class for function ‘snd_pcm_unpack_audio_tstamp_report’
static inline void snd_pcm_unpack_audio_tstamp_report(unsigned int data, unsigned int accuracy,
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/alsa/asoundlib.h:58:0,
from IPI2Cserver.c:3764:
/usr/include/alsa/control.h:237:23: error: invalid storage class for function ‘snd_ctl_abort’
static __inline__ int snd_ctl_abort(snd_ctl_t *ctl) { return snd_ctl_nonblock(ctl, 2); }
^~~~~~~~~~~~~
In file included from /usr/include/alsa/asoundlib.h:58:0,
from IPI2Cserver.c:3764:
/usr/include/alsa/control.h:558:23: error: invalid storage class for function ‘snd_hctl_abort’
static __inline__ int snd_hctl_abort(snd_hctl_t *hctl) { return snd_hctl_nonblock(hctl, 2); }
^~~~~~~~~~~~~~
Question now - what has changed when having migrated to stretch without any further project change?
Changes in alsa ?
Changes in stretch?
Thanks in advance - any help is very appreciated!
I am not a professional / expert, so I do not feel capable to change alsa source code headers to adapt to the new situation!
Thanks for answering!
Just an example from the calling main program (at line 3764, when the error comes up):
#include <alsa/asoundlib.h> // <- line 3764
int display_alsa() {
int val;
printf("ALSA library version: %s\n",
SND_LIB_VERSION_STR);
printf("\nPCM stream types:\n");
for (val = 0; val <= SND_PCM_STREAM_LAST; val++)
printf(" %s\n",
snd_pcm_stream_name((snd_pcm_stream_t)val));
...
Regards
I am trying to do separate compilation using CUDA 5. For this reason I set the "Generate Relocatable Device Code" to "Yes (-rdc=true)" in Visual Studio 2010. The program compiles without errors, however,
I get an invalid device symbol error when I try to initialize device constants using cudaMemcpyToSymbol.
i.e. I have the following constant
__constant__ float gdDomainOrigin[2];
and try to initialize it with
cudaMemcpyToSymbol(gdDomainOrigin, mDomainOrigin, 2*sizeof(float));
which leads to the error. The error does not occur, when I compile everything as a whole, without the aforementioned option set. Could anybody please help me with that?
I can't reproduce this. If build an application from two .cu files, one containing a __constant__ symbol and a simple kernel, and the other containing the runtime API incantations to populate that constant memory and call the kernel, it works only when relocatable device code is enabled, viz:
__constant__ float gdDomainOrigin[2];
__global__
void kernel(float *inout)
{
inout[0] = gdDomainOrigin[0];
inout[1] = gdDomainOrigin[1];
}
and
#include <cstdio>
extern __constant__ float gdDomainOrigin;
extern __global__ void kernel(float *);
inline
void gpuAssert(cudaError_t code, char * file, int line, bool Abort=true)
{
if (code != 0) {
fprintf(stderr, "GPUassert: %s %s %d\n",
cudaGetErrorString(code),file,line);
if (Abort) exit(code);
}
}
#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
int main(void)
{
const float mDomainOrigin[2] = { 1.234f, 5.6789f };
const size_t sz = sizeof(float) * size_t(2);
float * dbuf, * hbuf;
gpuErrchk( cudaFree(0) );
gpuErrchk( cudaMemcpyToSymbol(gdDomainOrigin, mDomainOrigin, sz) );
gpuErrchk( cudaMalloc((void **)&dbuf, sz) );
kernel<<<1,1>>>(dbuf);
gpuErrchk( cudaPeekAtLastError() );
hbuf = new float[2];
gpuErrchk( cudaMemcpy(hbuf, dbuf, sz, cudaMemcpyDeviceToHost) );
fprintf(stdout, "%f %f\n", hbuf[0], hbuf[1]);
return 0;
}
Compiling and running these in CUDA 5 on a 64 bit linux system with a Kepler GPU produces the following:
$ nvcc -arch=sm_30 -o shared shared.cu shared_dev.cu
$ ./shared
GPUassert: invalid device symbol shared.cu 23
$ nvcc -arch=sm_30 -rdc=true -o shared shared.cu shared_dev.cu
$ ./shared
1.234000 5.678900
You can see that in the first compilation, without relocatable GPU code generation, the symbol isn't found. In the second case, with relocatable GPU code generation, it is found, and the elf header in the object file looks just as you would expect:
$ nvcc -arch=sm_30 -rdc=true -c shared_dev.cu
$ cuobjdump -symbols shared_dev.o
Fatbin elf code:
================
arch = sm_30
code version = [1,6]
producer = cuda
host = linux
compile_size = 64bit
identifier = shared_dev.cu
symbols:
STT_SECTION STB_LOCAL .text._Z6kernelPf
STT_SECTION STB_LOCAL .nv.constant3
STT_SECTION STB_LOCAL .nv.constant0._Z6kernelPf
STT_CUDA_OBJECT STB_LOCAL _param
STT_SECTION STB_LOCAL .nv.callgraph
STT_FUNC STB_GLOBAL _Z6kernelPf
STT_CUDA_OBJECT STB_GLOBAL gdDomainOrigin
Fatbin ptx code:
================
arch = sm_30
code version = [3,1]
producer = cuda
host = linux
compile_size = 64bit
compressed
identifier = shared_dev.cu
ptxasOptions = --compile-only
Perhaps you could try my code and compilation/diagnostic steps and see what happens with your Windows toolchain.
I am doing some reverse engineering with a binary executable without sources. On Windows what I can do is load an executable file (EXE) with LoadLibrary, just as it was a DLL file. If the loaded file is not relocatable I can simply relocate my loader code to "make space" for the other module. When I have the binary loaded, I can call it's functions (assuming I where where they are, of course), and do other stuff.
Is there some way to do the same or similar on Mac? I have a mach-o executable, and I'd like to load it as it was a dynamic library (DYLIB). Or is there some way to convert an executable into a DYLIB? What are the real differences between an executable and a DYLIB?
OK, so I did some experiments, and see this. File "bin1.c" contains:
#include <stdio.h>
int main() {
printf("I am bin1.\n");
return 0;
}
and "bin2.c" is:
#include <stdio.h>
#include <dlfcn.h>
int main() {
printf("I am bin2.\n");
void *l = dlopen("bin1", RTLD_NOW);
if (l == NULL) {
printf("dlopen failed: %s\n", dlerror());
return -1;
}
void *f = dlsym(l, "main");
if (f == NULL) {
printf("dlsym failed: %s\n", dlerror());
return -1;
}
int (*main)() = f;
main();
return 0;
}
On my Mac, all compiles fine and indeed loads the other executable as it was a loadable library, and I can call the main function in the other binary:
Johanka:Desktop newacc$ uname -a
Darwin Johanka.local 11.3.0 Darwin Kernel Version 11.3.0: Thu Jan 12 18:47:41 PST 2012; root:xnu-1699.24.23~1/RELEASE_X86_64 x86_64
Johanka:Desktop newacc$ gcc bin1.c -o bin1 && ./bin1
I am bin1.
Johanka:Desktop newacc$ gcc bin2.c -o bin2 && ./bin2
I am bin2.
I am bin1.
Not sure though, whether there are limitations on this and if this can be done with non-relocatable binaries. But this example show that at least in some cases, it's possible.