In LKM, filp_open returns permission denied

2,087 views
Skip to first unread message

Mani557

unread,
Nov 4, 2010, 1:22:47 PM11/4/10
to Android Linux Kernel Development
Hi all,

I developed an LKM and loaded it into android emulator. At first, I
called filp_open("/data/", O_RDONLY, 0) from kernel module init
function (called during insmod). The function filp_open() ran
successfully. Then, I wrote ioctl(), to command kernel module from
usermode, but filp_open("/data/", O_RDONLY, 0) here is failing on
directory "/data/", and returned -13 (permission denied) error.
However, flip_open() on full filepath, say "/data/data/
com.android.music/shared_prefs/Music.xml" ran successfully.

I am not able to understand why this error came when filp_open() is
called inside dev_ioctl(), but ran successfully when called inside
kernel module init function. Please go through the code attached
below, and help me resolve this error.

test.c (user-mode code)
^^^^^^^^^^^^^^^^^^^^^^^
void Java_com_example_test_TestingJNI( JNIEnv* env, jobject thiz )
{
char buf[256];
fd = open ("/dev/mydev", O_RDONLY);
if (fd > 0 ) {

strcpy (buf, "/data/");

ioctl (fd, IOCTL_CMD, buf);

close (fd);
}
}

driver.c (Kernel-mode code)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
int dev_ioctl(struct inode *Inode, struct file *filp, unsigned int
cmd, char* ubuffer )
{
char dirname[250];

switch (cmd) {

case IOCTL_CMD:

mm_segment_t old_fs = get_fs();
set_fs(KERNEL_DS);

memset (dirname, 0, 250);
if(copy_from_user(dirname, ubuffer, 250))
return -EFAULT;

struct file *filp = filp_open(dirname, O_RDONLY, 0);

if (IS_ERR(filp) || (filp==NULL)) {

printk("filp_open returned error %ld\n",
PTR_ERR(filp));

} else {
printk ("flip_open success\n");
filp_close(filp);
}

set_fs(old_fs);
break;

default:
return -ENOTTY;
};
return 0;
}

Thanks & Regards

Greg KH

unread,
Nov 4, 2010, 3:05:05 PM11/4/10
to android...@googlegroups.com
On Thu, Nov 4, 2010 at 10:22 AM, Mani557 <subrama...@gmail.com> wrote:
> Hi all,
>
> I developed an LKM and loaded it into android emulator. At first, I
> called filp_open("/data/", O_RDONLY, 0) from kernel module init
> function (called during insmod).

Do not do kernel open/close/reading/etc from within the kernel, it
is not allowed and bad things will happen if you try to do it.

See the kernelnewbies.org wiki for details about why this is if
you are interested.

good luck,

greg k-h

Subramanyam GV

unread,
Nov 5, 2010, 1:42:44 AM11/5/10
to android...@googlegroups.com
I agree that the idea is bad....I have no choice as I can't access the entire file-system from Android user-mode app, due to Android security permissions. I don't want to root the device to mount file-system in read-write mode. The only choice is through Kernel Module. 

Please clarify me, if I misunderstand anything. And also please help me resolve the error I described in my 1st mail.

Thanks. 

Reply all
Reply to author
Forward
0 new messages