* ot memory allocation question
@ Don Raikes
` Chris Brannon
0 siblings, 1 reply; 12+ messages in thread
From: Don Raikes @ UTC (permalink / raw)
To: Speakup is a screen review system for Linux.
Sorry folks, but I still have a question about all this kernel programming stuff.
Is it possible from within a kernel module to allocate memory in userspace?
The reason I am asking is the function I am hooking into sys_write takes a buffer as input, but the buffer is defined as const __user *.
To me this means that if I modify the text that I was given in the buffer and want it to get written to the file, then I need to update the buffer.
However since the buffer is a const, I shuldn't write to it, plus in my case I am enlarging the amount of data, so if I were to cast the buffer to a writable __user *, then I am in danger of overwriting something I shouldn't in the user process (which in my case is the file pointer).
After completing the sys_write by passing the updated buffer to the original sys_write, I try to close the file and get an error.
My solution:
Allocate a new larger buffer inside of the userspace and copy_to_user into the new buffer and then when I pass control to the "real" sys_write function point it to the new buffer.
But the problem is how do I allocate this new buffer?
--
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 memory allocation question
ot memory allocation question Don Raikes
@ ` Chris Brannon
` covici
` (2 more replies)
0 siblings, 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:
> My solution:
>
> Allocate a new larger buffer inside of the userspace and copy_to_user
> into the new buffer and then when I pass control to the "real"
> sys_write function point it to the new buffer.
> But the problem is how do I allocate this new buffer?
There's no easy way to do this. You can't just pass your kernel
buffer to the system call you are intercepting, since the intercepted
call expects a user-space buffer.
Have a look at this link for some inspiration:
http://web.cs.wpi.edu/~cs4513/b05/proj1note2.txt
Also be careful about the return value of the real sys_write system
call. If you're passing it a buffer larger than the n bytes passed in
from userspace, its return value can be greater than n. Don't just use
it unmodified as the return value for your function.
-- Chris
^ permalink raw reply [flat|nested] 12+ messages in thread* Re: ot memory allocation question
` Chris Brannon
@ ` covici
` Chris Brannon
` Don Raikes
` Don Raikes
2 siblings, 1 reply; 12+ messages in thread
From: covici @ UTC (permalink / raw)
To: Speakup is a screen review system for Linux.
So could we use this inspeakup to write and read from a serial device --
thus circumventing most of our serial problems?
Chris Brannon <chris@the-brannons.com> wrote:
> Don Raikes <don.raikes@oracle.com> writes:
> > My solution:
> >
> > Allocate a new larger buffer inside of the userspace and copy_to_user
> > into the new buffer and then when I pass control to the "real"
> > sys_write function point it to the new buffer.
> > But the problem is how do I allocate this new buffer?
>
> There's no easy way to do this. You can't just pass your kernel
> buffer to the system call you are intercepting, since the intercepted
> call expects a user-space buffer.
> Have a look at this link for some inspiration:
> http://web.cs.wpi.edu/~cs4513/b05/proj1note2.txt
>
> Also be careful about the return value of the real sys_write system
> call. If you're passing it a buffer larger than the n bytes passed in
> from userspace, its return value can be greater than n. Don't just use
> it unmodified as the return value for your function.
>
> -- Chris
> _______________________________________________
> Speakup mailing list
> Speakup@linux-speakup.org
> http://linux-speakup.org/cgi-bin/mailman/listinfo/speakup
--
Your life is like a penny. You're going to lose it. The question is:
How do
you spend it?
John Covici
covici@ccs.covici.com
^ permalink raw reply [flat|nested] 12+ messages in thread* Re: ot memory allocation question
` covici
@ ` Chris Brannon
` covici
` Don Raikes
0 siblings, 2 replies; 12+ messages in thread
From: Chris Brannon @ UTC (permalink / raw)
To: Speakup is a screen review system for Linux.
covici@ccs.covici.com writes:
> So could we use this inspeakup to write and read from a serial device --
> thus circumventing most of our serial problems?
No. We discussed this several years ago, and we concluded that it won't
work. It's pretty much infeasible to use the tty devices (/dev/ttyS*)
from kernel space. That discussion might be available in the list
archives. I've been told that the solution to our serial problems in
Speakup is to write a tty line discipline. I don't really understand
how to do it.
-- Chris
^ permalink raw reply [flat|nested] 12+ messages in thread* Re: ot memory allocation question
` Chris Brannon
@ ` covici
` Brian Buhrow
` Don Raikes
1 sibling, 1 reply; 12+ messages in thread
From: covici @ UTC (permalink / raw)
To: Speakup is a screen review system for Linux.
What is a tty line discipline? Lets figure this out and do it -- ask
the kernel people for some documentation if necessary.
Chris Brannon <chris@the-brannons.com> wrote:
> covici@ccs.covici.com writes:
>
> > So could we use this inspeakup to write and read from a serial device --
> > thus circumventing most of our serial problems?
>
> No. We discussed this several years ago, and we concluded that it won't
> work. It's pretty much infeasible to use the tty devices (/dev/ttyS*)
> from kernel space. That discussion might be available in the list
> archives. I've been told that the solution to our serial problems in
> Speakup is to write a tty line discipline. I don't really understand
> how to do it.
>
> -- Chris
> _______________________________________________
> Speakup mailing list
> Speakup@linux-speakup.org
> http://linux-speakup.org/cgi-bin/mailman/listinfo/speakup
--
Your life is like a penny. You're going to lose it. The question is:
How do
you spend it?
John Covici
covici@ccs.covici.com
^ permalink raw reply [flat|nested] 12+ messages in thread* Re: ot memory allocation question
` covici
@ ` Brian Buhrow
0 siblings, 0 replies; 12+ messages in thread
From: Brian Buhrow @ UTC (permalink / raw)
To: Speakup is a screen review system for Linux.; +Cc: buhrow
Hello. A tty line discipline is a layer of code above the serial
driver that implements most of the common functions that all serial ports
support. I'm more familiar with tty line disciplines in BSD than Linux,
but the tty line discipline, for example, is what allows you to log into a
terminal via ssh or telnet and use the same terminal based programs that
you do when you log in via a hard serial line. It provides the terminal
processing you get when you press control characters, set line length and
row count parameters and the like. A line discipline can be as complex as
the tty discipline, which is very sophisticated and has many features grown
over many years, or it can be a simple layer that simply passes buffers of
data up and down the tty stack to allow you to talk to the serial ports.
Examples of line disciplines are: the tty discipline already mentioned,
PPP, Tablet, SLIP and one for the old Metricom Metro radios called Strip
(Star Mode IP). My guess is the easiest way to fix the serial port problem
is to write a simple tty discipline which mostly calls the internal
functions of the tty line discipline code so as to allow Speakup to
manipulate the underlying serial port in a way that it expects regardless
of the underlying serial port type, i.e. whether it's USB, a hard serial
line or a pseudo terminal that pumps characters over the net to a remote
speech synthesizer.
Hope that helps.
-Brian
On Jul 19, 11:26am, covici@ccs.covici.com wrote:
} Subject: Re: ot memory allocation question
} What is a tty line discipline? Lets figure this out and do it -- ask
} the kernel people for some documentation if necessary.
}
} Chris Brannon <chris@the-brannons.com> wrote:
}
} > covici@ccs.covici.com writes:
} >
} > > So could we use this inspeakup to write and read from a serial device --
} > > thus circumventing most of our serial problems?
} >
} > No. We discussed this several years ago, and we concluded that it won't
} > work. It's pretty much infeasible to use the tty devices (/dev/ttyS*)
} > from kernel space. That discussion might be available in the list
} > archives. I've been told that the solution to our serial problems in
} > Speakup is to write a tty line discipline. I don't really understand
} > how to do it.
} >
} > -- Chris
} > _______________________________________________
} > Speakup mailing list
} > Speakup@linux-speakup.org
} > http://linux-speakup.org/cgi-bin/mailman/listinfo/speakup
}
} --
} Your life is like a penny. You're going to lose it. The question is:
} How do
} you spend it?
}
} John Covici
} covici@ccs.covici.com
} _______________________________________________
} Speakup mailing list
} Speakup@linux-speakup.org
} http://linux-speakup.org/cgi-bin/mailman/listinfo/speakup
>-- End of excerpt from covici@ccs.covici.com
^ permalink raw reply [flat|nested] 12+ messages in thread
* RE: ot memory allocation question
` Chris Brannon
` covici
@ ` Don Raikes
1 sibling, 0 replies; 12+ messages in thread
From: Don Raikes @ UTC (permalink / raw)
To: Speakup is a screen review system for Linux.
Hi Chris and all those who were of such great help last week.
Just wanted to let you all know that with your help and support, plus a link from a fellow classmate I was able to get the assignment finished and turned in 3 hours early.
Of course after spending over 60 hours on this assignment, my prof sent out a note this morning saying that he wasn't expecting for the students to spend more than 10 hours on an assignment :-)
-----Original Message-----
From: Chris Brannon [mailto:chris@the-brannons.com]
Sent: Friday, July 19, 2013 8:19 AM
To: Speakup is a screen review system for Linux.
Subject: Re: ot memory allocation question
covici@ccs.covici.com writes:
> So could we use this inspeakup to write and read from a serial device
> -- thus circumventing most of our serial problems?
No. We discussed this several years ago, and we concluded that it won't work. It's pretty much infeasible to use the tty devices (/dev/ttyS*) from kernel space. That discussion might be available in the list archives. I've been told that the solution to our serial problems in Speakup is to write a tty line discipline. I don't really understand how to do it.
-- 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 memory allocation question
` Chris Brannon
` covici
@ ` Don Raikes
` Don Raikes
2 siblings, 0 replies; 12+ messages in thread
From: Don Raikes @ UTC (permalink / raw)
To: Speakup is a screen review system for Linux.
Chris,
Could I do something like:
Mm_segment_t cur_fs = get_fs();
Set_fs(get_ds()); // which puts me in userspace
Char * newbuf[required_size];
Set_fs(cur_fs);
If (copy_to_user(newbuf,srcbuf,required_size);
Ret = sys_write_orig(fd, newbuf,required_size);
-----Original Message-----
From: Chris Brannon [mailto:chris@the-brannons.com]
Sent: Friday, July 19, 2013 5:38 AM
To: Speakup is a screen review system for Linux.
Subject: Re: ot memory allocation question
Don Raikes <don.raikes@oracle.com> writes:
> My solution:
>
> Allocate a new larger buffer inside of the userspace and copy_to_user
> into the new buffer and then when I pass control to the "real"
> sys_write function point it to the new buffer.
> But the problem is how do I allocate this new buffer?
There's no easy way to do this. You can't just pass your kernel buffer to the system call you are intercepting, since the intercepted call expects a user-space buffer.
Have a look at this link for some inspiration:
http://web.cs.wpi.edu/~cs4513/b05/proj1note2.txt
Also be careful about the return value of the real sys_write system call. If you're passing it a buffer larger than the n bytes passed in from userspace, its return value can be greater than n. Don't just use it unmodified as the return value for your function.
-- 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 memory allocation question
` Chris Brannon
` covici
` Don Raikes
@ ` Don Raikes
` Chris Brannon
` Jason White
2 siblings, 2 replies; 12+ messages in thread
From: Don Raikes @ UTC (permalink / raw)
To: Speakup is a screen review system for Linux.
Chris,
I gave that code segmentthat I sent a try, and it gives me an error in the copy_to_user call. It looks like none of the bytes were copied to the userspace buffer.
I also get incompatible pointer types in the call to sys_write
-----Original Message-----
From: Chris Brannon [mailto:chris@the-brannons.com]
Sent: Friday, July 19, 2013 5:38 AM
To: Speakup is a screen review system for Linux.
Subject: Re: ot memory allocation question
Don Raikes <don.raikes@oracle.com> writes:
> My solution:
>
> Allocate a new larger buffer inside of the userspace and copy_to_user
> into the new buffer and then when I pass control to the "real"
> sys_write function point it to the new buffer.
> But the problem is how do I allocate this new buffer?
There's no easy way to do this. You can't just pass your kernel buffer to the system call you are intercepting, since the intercepted call expects a user-space buffer.
Have a look at this link for some inspiration:
http://web.cs.wpi.edu/~cs4513/b05/proj1note2.txt
Also be careful about the return value of the real sys_write system call. If you're passing it a buffer larger than the n bytes passed in from userspace, its return value can be greater than n. Don't just use it unmodified as the return value for your function.
-- 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 memory allocation question
` Don Raikes
@ ` Chris Brannon
` Don Raikes
` Jason White
1 sibling, 1 reply; 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:
> Chris,
>
> I gave that code segmentthat I sent a try, and it gives me an error in
> the copy_to_user call. It looks like none of the bytes were copied to
> the userspace buffer.
To be honest, this is way out of my league. I can give you another
pointer to more reading. I had to really dig for this one.
http://www.linux.it/kerneldocs/ksys/
Intercepting syscalls isn't (yet) part of my experience, so I can't be
of much further help.
-- Chris
^ permalink raw reply [flat|nested] 12+ messages in thread* RE: ot memory allocation question
` Chris Brannon
@ ` Don Raikes
0 siblings, 0 replies; 12+ messages in thread
From: Don Raikes @ UTC (permalink / raw)
To: Speakup is a screen review system for Linux.
Thanks for all the help. Unfortunately my professor has been of limited help in the assignment. So I have posted a message on the kernelnewbies list.
-----Original Message-----
From: Chris Brannon [mailto:chris@the-brannons.com]
Sent: Friday, July 19, 2013 11:45 AM
To: Speakup is a screen review system for Linux.
Subject: Re: ot memory allocation question
Don Raikes <don.raikes@oracle.com> writes:
> Chris,
>
> I gave that code segmentthat I sent a try, and it gives me an error in
> the copy_to_user call. It looks like none of the bytes were copied to
> the userspace buffer.
To be honest, this is way out of my league. I can give you another pointer to more reading. I had to really dig for this one.
http://www.linux.it/kerneldocs/ksys/
Intercepting syscalls isn't (yet) part of my experience, so I can't be of much further 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 memory allocation question
` Don Raikes
` Chris Brannon
@ ` Jason White
1 sibling, 0 replies; 12+ messages in thread
From: Jason White @ UTC (permalink / raw)
To: speakup
Don Raikes <don.raikes@oracle.com> wrote:
> Chris,
>
> I gave that code segmentthat I sent a try, and it gives me an error in the copy_to_user call. It looks like none of the bytes were copied to the userspace buffer.
> I also get incompatible pointer types in the call to sys_write
You said earlier this was for some kind of course; surely you should be asking
your lecturer or tutor in the course for help with it, as they're in the best
position to offer advice in the context of the project they've asked you to
complete.
At this point I really can't help any further, as I don't have a background in
kernel development.
^ 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 memory allocation question Don Raikes
` Chris Brannon
` covici
` Chris Brannon
` covici
` Brian Buhrow
` Don Raikes
` Don Raikes
` Don Raikes
` Chris Brannon
` 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).