* OT: kernel oops
@ Don Raikes
` Jason White
` Chris Brannon
0 siblings, 2 replies; 12+ messages in thread
From: Don Raikes @ UTC (permalink / raw)
To: Speakup is a screen review system for Linux.
Hi again,
After browsing some of both Linux Device Drivers and Linux Kernel Development, I modified my kernel module code and now I get a kernel oops.
I am trying to hook the sys_write() system call based on some code from my professor.
The section of code that is having problems looks like this:
Char *tbuf = NULL;
tbuf = kmalloc(count, GFP_ATOMIC); // count is passed in from userspace
if (!tbuf)
return -ENOMEM;
if (copy_from_user(&tbuf, buf, count))
return -EFAULT;
printk(KERN_INFO "Copy_from_user succeeded."\n");
kfree(tbuf);
I get a segmentation fault immediately after it prints copy_from_user succeeded into the log.
Oops message:
Oops: 0000 [#9] SMP
And as far as I can tell it is pointing to the kfree function at offset 0xba of 0xc0.
If there is a better place to post this (say a Linux developers list), I am happy to take my questions there, but this list is my first port of call for errors with Linux.
--
Best Regards, Donald
HYPERLINK "http://www.oracle.com/" \nOracle
Donald raikes | Accessibility Specialist/ QA Engineer
Phone: HYPERLINK "tel:+15202717608"+15202717608 | Mobile: HYPERLINK "tel:+15202717608"+15202717608
Oracle Quality Assurance
| Tucson, Arizona
HYPERLINK "http://www.oracle.com/commitment" \nGreen Oracle
Oracle is committed to developing practices and products that help protect the environment
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: OT: kernel oops
OT: kernel oops Don Raikes
@ ` Jason White
` Chris Brannon
1 sibling, 0 replies; 12+ messages in thread
From: Jason White @ UTC (permalink / raw)
To: speakup
Don Raikes <don.raikes@oracle.com> wrote:
> If there is a better place to post this (say a Linux developers list), I am happy to take my questions there, but this list is my first port of call for errors with Linux.
I think Linux Kernel Newbies (http://www.kernelnewbies.org/) have a mailing
list.
You might also get quicker responses from an IRC channel, if you can find an
appropriate one.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: OT: kernel oops
OT: kernel oops Don Raikes
` Jason White
@ ` Chris Brannon
` Don Raikes
` (2 more replies)
1 sibling, 3 replies; 12+ messages in thread
From: Chris Brannon @ UTC (permalink / raw)
To: Speakup is a screen review system for Linux.
Don Raikes <don.raikes@oracle.com> writes:
> The section of code that is having problems looks like this:
*SNIP*
> if (copy_from_user(&tbuf, buf, count))
This line is your problem. You are passing a
pointer-to-pointer-to-character (char **) as the first argument to
copy_from_user, but you should be passing char * instead. So ditch the
ampersand, and all will be right with the world!
I could give you a thorough explanation of why this is failing, if you
like, but to make a long story short, this line of code is smashing the
stack!
I'm always happy to help.
-- Chris
^ permalink raw reply [flat|nested] 12+ messages in thread
* RE: OT: kernel oops
` Chris Brannon
@ ` Don Raikes
` Don Raikes
` Jason White
2 siblings, 0 replies; 12+ messages in thread
From: Don Raikes @ UTC (permalink / raw)
To: Speakup is a screen review system for Linux.
Thanks chris,
I will give that a try later today. I need to get some "real" work done today. This class has been consuming almost all of my time this week.
-----Original Message-----
From: Chris Brannon [mailto:chris@the-brannons.com]
Sent: Thursday, July 18, 2013 9:05 AM
To: Speakup is a screen review system for Linux.
Subject: Re: OT: kernel oops
Don Raikes <don.raikes@oracle.com> writes:
> The section of code that is having problems looks like this:
*SNIP*
> if (copy_from_user(&tbuf, buf, count))
This line is your problem. You are passing a pointer-to-pointer-to-character (char **) as the first argument to copy_from_user, but you should be passing char * instead. So ditch the ampersand, and all will be right with the world!
I could give you a thorough explanation of why this is failing, if you like, but to make a long story short, this line of code is smashing the stack!
I'm always happy to help.
-- Chris
_______________________________________________
Speakup mailing list
Speakup@linux-speakup.org
http://linux-speakup.org/cgi-bin/mailman/listinfo/speakup
^ permalink raw reply [flat|nested] 12+ messages in thread
* RE: OT: kernel oops
` Chris Brannon
` Don Raikes
@ ` Don Raikes
` Jason White
2 siblings, 0 replies; 12+ messages in thread
From: Don Raikes @ UTC (permalink / raw)
To: Speakup is a screen review system for Linux.
Chris,
Thanks it worked like a charm.
No more segmentation faults and I was able to print the contents of the array I copied from userspace!
Wow I should have come here earlier in the week instead of beating my head against the wall for so long :-)
-----Original Message-----
From: Chris Brannon [mailto:chris@the-brannons.com]
Sent: Thursday, July 18, 2013 9:05 AM
To: Speakup is a screen review system for Linux.
Subject: Re: OT: kernel oops
Don Raikes <don.raikes@oracle.com> writes:
> The section of code that is having problems looks like this:
*SNIP*
> if (copy_from_user(&tbuf, buf, count))
This line is your problem. You are passing a pointer-to-pointer-to-character (char **) as the first argument to copy_from_user, but you should be passing char * instead. So ditch the ampersand, and all will be right with the world!
I could give you a thorough explanation of why this is failing, if you like, but to make a long story short, this line of code is smashing the stack!
I'm always happy to help.
-- Chris
_______________________________________________
Speakup mailing list
Speakup@linux-speakup.org
http://linux-speakup.org/cgi-bin/mailman/listinfo/speakup
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: OT: kernel oops
` Chris Brannon
` Don Raikes
` Don Raikes
@ ` Jason White
` Don Raikes
2 siblings, 1 reply; 12+ messages in thread
From: Jason White @ UTC (permalink / raw)
To: speakup
Chris Brannon <chris@the-brannons.com> wrote:
> Don Raikes <don.raikes@oracle.com> writes:
>
> > The section of code that is having problems looks like this:
> *SNIP*
> > if (copy_from_user(&tbuf, buf, count))
>
> This line is your problem. You are passing a
> pointer-to-pointer-to-character (char **) as the first argument to
> copy_from_user, but you should be passing char * instead. So ditch the
> ampersand, and all will be right with the world!
> I could give you a thorough explanation of why this is failing, if you
> like, but to make a long story short, this line of code is smashing the
> stack!
In other words, you're passing it the address of the pointer to the buffer,
rather than the pointer itself, which holds the address of the buffer.
^ permalink raw reply [flat|nested] 12+ messages in thread
* RE: OT: kernel oops
` Jason White
@ ` Don Raikes
` Jason White
0 siblings, 1 reply; 12+ messages in thread
From: Don Raikes @ UTC (permalink / raw)
To: Speakup is a screen review system for Linux.
Ok guys, I have one more question on this assignment :-)
I successfully read in the data using copy_from_user and then I did some mangling of the data which I verified worked properly.
Now I want to put it back into user space, so I use:
If (copy_to_user(buf, tbuf2, count))
Buf is defined in the parameter list of my function as:
Const char __user * buf
Tbuf2 is defined as
Char *tbuf;
And count is just an int.
I get a compile time error:
/home/seed/hw5/hookme.c:128: warning: passing argument 1 of ‘copy_to_user’ discards qualifiers from pointer target type
Any good ideas on why this occurring and how I can fix it?
Thanks in advance for any help!
-----Original Message-----
From: Jason White [mailto:jason@jasonjgw.net]
Sent: Thursday, July 18, 2013 4:43 PM
To: speakup@linux-speakup.org
Subject: Re: OT: kernel oops
Chris Brannon <chris@the-brannons.com> wrote:
> Don Raikes <don.raikes@oracle.com> writes:
>
> > The section of code that is having problems looks like this:
> *SNIP*
> > if (copy_from_user(&tbuf, buf, count))
>
> This line is your problem. You are passing a
> pointer-to-pointer-to-character (char **) as the first argument to
> copy_from_user, but you should be passing char * instead. So ditch
> the ampersand, and all will be right with the world!
> I could give you a thorough explanation of why this is failing, if you
> like, but to make a long story short, this line of code is smashing
> the stack!
In other words, you're passing it the address of the pointer to the buffer, rather than the pointer itself, which holds the address of the buffer.
_______________________________________________
Speakup mailing list
Speakup@linux-speakup.org
http://linux-speakup.org/cgi-bin/mailman/listinfo/speakup
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: OT: kernel oops
` Don Raikes
@ ` Jason White
` Don Raikes
0 siblings, 1 reply; 12+ messages in thread
From: Jason White @ UTC (permalink / raw)
To: speakup
Don Raikes <don.raikes@oracle.com> wrote:
> If (copy_to_user(buf, tbuf2, count))
>
> Buf is defined in the parameter list of my function as:
> Const char __user * buf
It's also the destination of the copy, so it shouldn't be const. You're
declaring the buffer as const, then passing it to a function that will write
to it.
This is why the compiler complains that you're discarding the const qualifier
when you pass this parameter to the function.
Does that help?
It's probably best to remove the "const" from the declaration of buf.
Alternatively, you can use a cast expression in the call to copy_to_user, but
declaring a parameter as constant and then casting that away is not a good
practice in my personal view.
^ permalink raw reply [flat|nested] 12+ messages in thread
* RE: OT: kernel oops
` Jason White
@ ` Don Raikes
` Jason White
0 siblings, 1 reply; 12+ messages in thread
From: Don Raikes @ UTC (permalink / raw)
To: Speakup is a screen review system for Linux.
Ok so I get it, but the problem is that I am hooking into the sys_write systemcall, and it is by default cast to a const.
If I leave it as is, and cast the buf in my copy_to_user to a (char*) should that work?
Sorry my knowledge of c is very limited :)
It looks at the moment like when I do the sys_write function then try calling the sys_close that I have lost the pointer to my file.
Could I have messed up the stack yet again?
-----Original Message-----
From: Jason White [mailto:jason@jasonjgw.net]
Sent: Thursday, July 18, 2013 6:34 PM
To: speakup@linux-speakup.org
Subject: Re: OT: kernel oops
Don Raikes <don.raikes@oracle.com> wrote:
> If (copy_to_user(buf, tbuf2, count))
>
> Buf is defined in the parameter list of my function as:
> Const char __user * buf
It's also the destination of the copy, so it shouldn't be const. You're declaring the buffer as const, then passing it to a function that will write to it.
This is why the compiler complains that you're discarding the const qualifier when you pass this parameter to the function.
Does that help?
It's probably best to remove the "const" from the declaration of buf.
Alternatively, you can use a cast expression in the call to copy_to_user, but declaring a parameter as constant and then casting that away is not a good practice in my personal view.
_______________________________________________
Speakup mailing list
Speakup@linux-speakup.org
http://linux-speakup.org/cgi-bin/mailman/listinfo/speakup
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: OT: kernel oops
` Don Raikes
@ ` Jason White
` Don Raikes
0 siblings, 1 reply; 12+ messages in thread
From: Jason White @ UTC (permalink / raw)
To: speakup
Don Raikes <don.raikes@oracle.com> wrote:
> Ok so I get it, but the problem is that I am hooking into the sys_write systemcall, and it is by default cast to a const.
>
> If I leave it as is, and cast the buf in my copy_to_user to a (char*) should that work?
>
Try (void __user *)
which according to Linux Device Drivers is the correct type for copy_to_user.
> Sorry my knowledge of c is very limited :)
I would suggest reading a good book on the subject in that case.
A good knowledge of C is a prerequisite for what you are doing.
^ permalink raw reply [flat|nested] 12+ messages in thread
* RE: OT: kernel oops
` Jason White
@ ` Don Raikes
` Jason White
0 siblings, 1 reply; 12+ messages in thread
From: Don Raikes @ UTC (permalink / raw)
To: Speakup is a screen review system for Linux.
I agree that a good knowledge of c is a prerequisite for this kind of programming, but this is my first semester in theis masters program, and they threw us into the deep end pretty quickly.
I had a c class about 8 years ago and have forgotten a lot of it.
I am also taking a c++ class this summer, but it isn't getting into any of this stuff yet.
-----Original Message-----
From: Jason White [mailto:jason@jasonjgw.net]
Sent: Thursday, July 18, 2013 10:51 PM
To: speakup@linux-speakup.org
Subject: Re: OT: kernel oops
Don Raikes <don.raikes@oracle.com> wrote:
> Ok so I get it, but the problem is that I am hooking into the sys_write systemcall, and it is by default cast to a const.
>
> If I leave it as is, and cast the buf in my copy_to_user to a (char*) should that work?
>
Try (void __user *)
which according to Linux Device Drivers is the correct type for copy_to_user.
> Sorry my knowledge of c is very limited :)
I would suggest reading a good book on the subject in that case.
A good knowledge of C is a prerequisite for what you are doing.
_______________________________________________
Speakup mailing list
Speakup@linux-speakup.org
http://linux-speakup.org/cgi-bin/mailman/listinfo/speakup
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: OT: kernel oops
` Don Raikes
@ ` Jason White
0 siblings, 0 replies; 12+ messages in thread
From: Jason White @ UTC (permalink / raw)
To: speakup
Don Raikes <don.raikes@oracle.com> wrote:
> I am also taking a c++ class this summer, but it isn't getting into any of this stuff yet.
This may be helpful:
http://mindview.net/Books/TICPP/ThinkingInCPP2e.html
^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~ UTC | newest]
Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
OT: kernel oops Don Raikes
` Jason White
` Chris Brannon
` Don Raikes
` Don Raikes
` Jason White
` Don Raikes
` Jason White
` Don Raikes
` Jason White
` Don Raikes
` Jason White
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).