Getting 'bad file number' error writing to device driver
I am trying to write to mydriver but get a 'Bad file number' error. int fd = open("/dev/mydriver",O_RDWR); this opens successfully as fd is not -1. ssize_t bytesToWrite = strlen(buf); value is: bytesToWrite 28 ssize_t bytesWritten = write(fd,buf,bytesToWrite); value is: bytesWritten -1 __android_log_print(ANDROID_LOG_INFO,__FILE__,"ANIL strerror(errno) %s", strerror(errno)); value is: strerror(errno) Bad file number Any help appreciated. Anil
Hello,
I am trying to write to mydriver but get a 'Bad file number' error.
int fd = open("/dev/mydriver",O_RDWR);
this opens successfully as fd is not -1.
ssize_t bytesToWrite = strlen(buf);
value is: bytesToWrite 28
ssize_t bytesWritten = write(fd,buf,bytesToWrite);
value is: bytesWritten -1
__android_log_print(ANDROID_LOG_INFO,__FILE__,"ANIL strerror(errno) %s", strerror(errno));
value is: strerror(errno) Bad file number
Please use strace and let us know the full trace of system calls. Also, if possible could you paste the 'write' function of your driver? thanks, Daniel.
I am calling the driver from an Android program (OMAP4/Blaze). It calls a c++ program via JNI which then calls the device driver. Someone suggested it might be a permissions problem - the program is running in user mode. on Blaze board, /system/bin # ls -l -rwxrwxrwx system system 7636 2011-09-30 03:53 mydriver Will strace still be useful? In general, in Linux, how does one enable a user program to call a custom device driver? Anil -----Original Message----- From: Daniel Baluta [mailto:daniel.baluta@gmail.com] Sent: Thu 10/13/2011 5:09 PM To: Philip Anil-QBW348 Cc: kernelnewbies@kernelnewbies.org Subject: Re: Getting 'bad file number' error writing to device driver Hello,
I am trying to write to mydriver but get a 'Bad file number' error.
int fd = open("/dev/mydriver",O_RDWR);
this opens successfully as fd is not -1.
ssize_t bytesToWrite = strlen(buf);
value is: bytesToWrite 28
ssize_t bytesWritten = write(fd,buf,bytesToWrite);
value is: bytesWritten -1
__android_log_print(ANDROID_LOG_INFO,__FILE__,"ANIL strerror(errno) %s", strerror(errno));
value is: strerror(errno) Bad file number
Please use strace and let us know the full trace of system calls. Also, if possible could you paste the 'write' function of your driver? thanks, Daniel.
On Sat, Oct 15, 2011 at 2:42 AM, Philip Anil-QBW348 <anil.philip@motorolasolutions.com> wrote:
I am calling the driver from an Android program (OMAP4/Blaze). It calls a c++ program via JNI which then calls the device driver. Someone suggested it might be a permissions problem - the program is running in user mode.
on Blaze board, /system/bin # ls -l -rwxrwxrwx system system 7636 2011-09-30 03:53 mydriver
Will strace still be useful? In general, in Linux, how does one enable a user program to call a custom device driver?
Please don't top post! :) strace will be useful to check the parameters for open, write system calls. EBADF fd is not a valid file descriptor or is not open for writing. Ok, so either open fails, or you don't have the permission to write into /dev/mydriver file. Another thing that I can think of, is that your driver's write function is wrong, and returns -EBADF in some case ? Can you give us a pointer to your driver? thanks, Daniel.
-----Original Message----- From: Daniel Baluta [mailto:daniel.baluta@gmail.com] Sent: Saturday, October 15, 2011 3:34 AM To: Philip Anil-QBW348 Cc: kernelnewbies@kernelnewbies.org Subject: Re: Getting 'bad file number' error writing to device driver On Sat, Oct 15, 2011 at 2:42 AM, Philip Anil-QBW348 <anil.philip@motorolasolutions.com> wrote:
I am calling the driver from an Android program (OMAP4/Blaze). It calls a c++ program via JNI which then calls the device driver. Someone suggested it might be a permissions problem - the program is running in user mode.
on Blaze board, /system/bin # ls -l -rwxrwxrwx system system 7636 2011-09-30 03:53 mydriver
Will strace still be useful? In general, in Linux, how does one enable a user program to call a custom device driver?
Please don't top post! :) strace will be useful to check the parameters for open, write system calls. EBADF fd is not a valid file descriptor or is not open for writing. Ok, so either open fails, or you don't have the permission to write into /dev/mydriver file. ------ I apologize for 'top-posting' (I did not know that was undesirable - most email clients and also Google newsgroups put one's reply at the top.). Yes, the /dev/mydriver had permissions 600. I did a chmod to 666 and it worked. I am performing security testing and want to see if a program running in user mode can elevate its privileges to call the device driver which has permissions 600. Any ideas how it can? Anil
On Mon, Oct 17, 2011 at 6:49 PM, Philip Anil-QBW348 <anil.philip@motorolasolutions.com> wrote:
-----Original Message----- From: Daniel Baluta [mailto:daniel.baluta@gmail.com] Sent: Saturday, October 15, 2011 3:34 AM To: Philip Anil-QBW348 Cc: kernelnewbies@kernelnewbies.org Subject: Re: Getting 'bad file number' error writing to device driver
On Sat, Oct 15, 2011 at 2:42 AM, Philip Anil-QBW348 <anil.philip@motorolasolutions.com> wrote:
I am calling the driver from an Android program (OMAP4/Blaze). It calls a c++ program via JNI which then calls the device driver. Someone suggested it might be a permissions problem - the program is running in user mode.
on Blaze board, /system/bin # ls -l -rwxrwxrwx system system 7636 2011-09-30 03:53 mydriver
Will strace still be useful? In general, in Linux, how does one enable a user program to call a custom device driver?
Please don't top post! :)
strace will be useful to check the parameters for open, write system calls.
EBADF fd is not a valid file descriptor or is not open for writing. Ok, so either open fails, or you don't have the permission to write into /dev/mydriver file.
------ I apologize for 'top-posting' (I did not know that was undesirable - most email clients and also Google newsgroups put one's reply at the top.). Yes, the /dev/mydriver had permissions 600. I did a chmod to 666 and it worked. I am performing security testing and want to see if a program running in user mode can elevate its privileges to call the device driver which has permissions 600. Any ideas how it can? Anil
Maybe setuid bit can help you ([1]). When this bit is set, a certain program can run with the privileges of its owner. thanks, Daniel. [1] http://en.wikipedia.org/wiki/Setuid
-----Original Message----- Maybe setuid bit can help you ([1]). When this bit is set, a certain program can run with the privileges of its owner. thanks, Daniel. [1] http://en.wikipedia.org/wiki/Setuid -------- Daniel, Thanks for the link; but this bit has to be set on the original executable. Is there any way a program in user mode can get around this or set the bit himself? Anil
Thanks for the link; but this bit has to be set on the original executable. Is there any way a program in user mode can get around this or set the bit himself?
Ummm...now that would be a security violation, don't you think? If a program can elevate it's own privileges, then whats the point of having access control! :) I think a user-space program will always need _some_ help from a privileged user/program. Eg in the case of setuid, typically the root user will be setting this bit (on a executable) so that an unprivileged user can execute with temporarily elevated privileges. CMIIW. HTH, -mandeep
Anil
_______________________________________________ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
Thanks for the link; but this bit has to be set on the original executable. Is there any way a program in user mode can get around this or set the bit himself?
Ummm...now that would be a security violation, don't you think? If a program can elevate it's own privileges, then whats the point of having access control! :) I think a user-space program will always need _some_ help from a privileged user/program. Eg in the case of setuid, typically the root user will be setting this bit (on a executable) so that an unprivileged user can execute with temporarily elevated privileges. CMIIW. HTH, -mandeep ---------- Good point, Mandeep :) Since I am doing security testing, I want to see if there is any way - security loopholes - especially in the Android environment (which runs a branch of Linux) that a program can access the device driver. Anil
participants (3)
-
Daniel Baluta -
Mandeep Sandhu -
Philip Anil-QBW348