Discussion:
[android-kernel] Android NDK can't run fanotify: fanotify_mark: Invalid argument (kernel problem?)
'Fabian B' via Android Linux Kernel Development
2017-04-19 10:41:31 UTC
Permalink
I'm stuck with the fanotify
<http://man7.org/linux/man-pages/man7/fanotify.7.html>systemcall to get
access control for file-access events, when a file on the device gets
opened. My specs:

- Samsung i9300 ARM with LineageOS 14.1
<https://wiki.lineageos.org/devices/i9300/>
- Linux kernel 3.0.101 (fanotify is available in kernel > 2.6.36 /
android 5)
- NDK r14b
- rooted


I have build an Android native application (written in C) based on fsmon
<https://github.com/nowsecure/fsmon>to handle the file access. After
compiling I pushed the executable armeabi-v7 file with ADB on the device
(/data/local/tmp/). Then I tried to execute (as su) the file and got
following error message:


fanotify_mark: Invalid argument.

I had changed the kernel config by editing the
/lineage/kernel/samsung/smdk4412/arch/arm/configs/lineageos_i9300_defconfig
to:

CONFIG_FANOTIFY=y
CONFIG_FANOTIFY_ACCESS_PERMISSIONS=y


Also after flashing the new kernel and running

cat /proc/config.gz | gunzip > running.config

from the device to get the kernel config, I can see that both config-params
are activated. Following is the code (the calling function, if more is
necessary, I will extend it) which crashes:

static bool fm_begin (FileMonitor *fm) {
// Define event mask which fanotify should signal.
// Important: the FAN_ALL_PERM_EVENTS flag to enable access control
uint64_t fan_mask = FAN_OPEN | FAN_CLOSE | FAN_ACCESS | FAN_MODIFY |
FAN_ALL_PERM_EVENTS;
// Set which type of operation it should do on fanotify_mark() - adding
the flaggs
unsigned int mark_flags = FAN_MARK_ADD, init_flags = 0;
struct sigaction sa;
int res = 0;


// Set function to close the file descriptor
fm->control_c = fm_control_c;
sa.sa_flags = SA_SIGINFO | SA_RESTART;
sigemptyset (&sa.sa_mask);
sa.sa_sigaction = usr1_handler;
if (sigaction (SIGUSR1, &sa, NULL) == -1) {
printf ("Cannot set SIGUSR1 signal handler\n");
return false;
}
fan_mask |= FAN_ONDIR;
fan_mask |= FAN_EVENT_ON_CHILD;
// Walk into subdirectories
mark_flags |= FAN_MARK_MOUNT;

// Important: Set FAN_CLASS_CONTENT to allow/deny access to files
init_flags |= (fan_mask & FAN_ALL_PERM_EVENTS)
? FAN_CLASS_CONTENT
: FAN_CLASS_NOTIF;
if (!fm->root) {
fm->root = "/";
}


// Call the fanotify_init systemcall with read only and enable for large
files
fan_fd = fanotify_init (init_flags, O_RDONLY | O_LARGEFILE);
if (fan_fd < 0) {
perror ("fanotify_init");
return false;
}

// Call the fanotify_mark systemcall with flags
// CRASHES HERE!!!!!!!!!!!!!!! res = -1
res = fanotify_mark (fan_fd, mark_flags, fan_mask, AT_FDCWD, fm->root);
if (res != 0) {
printf("Result: %i\n", res); // == -1
perror ("fanotify_mark");
return false;
}

FD_ZERO (&rfds);
FD_SET (fan_fd, &rfds);
return true;
}

I logged the flags and they seemed to be valid, no null value or something
else. I also tried different combinations of the mask, no result...

The fanotify_init() function works properly and doesn't throw an error
(before I set the CONFIG in the kernel config-file, the application crashed
here).

I tried the same code on Ubuntu and on a x86 Stock Android emulator
(compiled AOSP) with success - the application get's started and I can
control the access. But on the emulator the goldfish kernel is running.

Running "adb shell logcat" and "adb shell dmesg" doesn't show me an error
on execution...

It's a little bit strange because the return value 1 (fanotify negate the
value) of the fanotify_mark() call is on the errno.h list:

#define EPERM 1 /* Operation not permitted */

I think it's a kernel problem (not LineageOs) so this could be the right
place to ask...

Some suggestions?...
--
--
unsubscribe: android-kernel+***@googlegroups.com
website: http://groups.google.com/group/android-kernel
---
You received this message because you are subscribed to the Google Groups "Android Linux Kernel Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-kernel+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...