Skip to contentSkip to conversation

sys_read buffer too large

DISCUSSION Don Raikes 3 participants 3 messages Jul 20, 2013 – Jul 21, 2013

lore
Share & tools
lore

Don Raikes

author
#0 lore raw

Hello all,

I am very new to kernel programming. In fact, I have been working on it for a week now.

I am taking a computer science class, and one of our assignments is to hook some of the system calls in a 2.6.28 kernel.

I have the basic module created, and I am hooking into the sys_read function. The assignment is to print to the log what is being read using sys_read.

I have some checks in my function to limit my printing to only a certain file, so I don't get the contents of every file being read on the system.

OnceI know I have the right file, I decided to print the size of the buffer.

The function signature is:

Asmlinkage long sys_read(unsigned int fd, char __user *buf, size_t count);

So I do:

Printk(KERN_INFO "Read buffer is [%d] bytes.\n",count);

The particular file I am reading has 2 lines the first is 14 bytes long and the second is 30 bytes long.

The output from my printk statements are 8192 and 4096 respectively.

Why is the count so large?

Also I do a strlen(buf) and print the value as well, and it is 29 and 16 respectively.

Next I did copy_from_user(tbuf,buf,strlen(buf))

And tried to print the contents of the buffer but it came out garbage.

Does anyone have any ideas how I can get this to work?

BTW: the assignment is due on Sunday so any help would be aappreciated.

Signature · 30 lines
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

 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20130720/46be4713/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.gif
Type: image/gif
Size: 658 bytes
Desc: not available
Url : http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20130720/46be4713/attachment.gif 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image002.gif
Type: image/gif
Size: 356 bytes
Desc: not available
Url : http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20130720/46be4713/attachment-0001.gif 
#1 lore raw
Quoted 48 lines · Don Raikes

Hello all,****

** **

I am very new to kernel programming. In fact, I have been working on it for a week now.****

** **

I am taking a computer science class, and one of our assignments is to hook some of the system calls in a 2.6.28 kernel.****

** **

I have the basic module created, and I am hooking into the sys_read function. The assignment is to print to the log what is being read using sys_read.****

** **

I have some checks in my function to limit my printing to only a certain file, so I don?t get the contents of every file being read on the system.*
***

** **

OnceI know I have the right file, I decided to print the size of the buffer.****

** **

The function signature is:****

** **

Asmlinkage long sys_read(unsigned int fd, char __user *buf, size_t count);
****

** **

So I do:****

** **

Printk(KERN_INFO ?Read buffer is [%d] bytes.\n?,count);****

** **

Quoted 9 lines

The particular file I am reading has 2 lines the first is 14 bytes long and the second is 30 bytes long.****

** **

The output from my printk statements are 8192 and 4096 respectively.****

Why is the count so large?

I think it is the size of one PAGE.I think reading from sys_read is PAGE aligned.Just print the size of the page and i think it will be 4K.

Quoted 42 lines

****

** **

Also I do a strlen(buf) and print the value as well, and it is 29 and 16 respectively.****

** **

Next I did copy_from_user(tbuf,buf,strlen(buf))****

And tried to print the contents of the buffer but it came out garbage.****

** **

Does anyone have any ideas how I can get this to work?****

** **

BTW: the assignment is due on Sunday so any help would be aappreciated.*** *

** **

** **

** **

--
Best Regards, Donald****

[image: Oracle] <http://www.oracle.com/>
Donald raikes | Accessibility Specialist/ QA Engineer
Phone: +15202717608 | Mobile: +15202717608 Oracle Quality Assurance
| Tucson, Arizona ****

[image: Green Oracle] <http://www.oracle.com/commitment>****

Oracle is committed to developing practices and products that help protect the environment****

Quoted 8 lines

** **

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies at kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20130720/d3db021a/attachment-0001.html

#2 lore raw
Quoted 51 lines · anish singh
Quoted 46 lines · Don Raikes

Hello all,****

** **

I am very new to kernel programming. In fact, I have been working on it for a week now.****

** **

I am taking a computer science class, and one of our assignments is to hook some of the system calls in a 2.6.28 kernel.****

** **

I have the basic module created, and I am hooking into the sys_read function. The assignment is to print to the log what is being read using sys_read.****

** **

I have some checks in my function to limit my printing to only a certain file, so I don?t get the contents of every file being read on the system.
****

** **

OnceI know I have the right file, I decided to print the size of the buffer.****

** **

The function signature is:****

** **

Asmlinkage long sys_read(unsigned int fd, char __user *buf, size_t count);
****

** **

So I do:****

** **

Printk(KERN_INFO ?Read buffer is [%d] bytes.\n?,count);****

Quoted 8 lines
Quoted 7 lines

** **

The particular file I am reading has 2 lines the first is 14 bytes long and the second is 30 bytes long.****

**

What utility are you using to read files? It is better to write your own program using glibc 'read' routine. Also you can use 'strace' utility to trace read system call and its arguments from user space.

Quoted 10 lines

**

Quoted 5 lines

The output from my printk statements are 8192 and 4096 respectively.****

Why is the count so large?

I think it is the size of one PAGE.I think reading from sys_read is PAGE aligned.Just print the size of the page and i think it will be 4K.

I doubt it is true, sys_read will merely pass down arguments which it received from user space. My hunch is it is user mode utility which is doing buffered read.

Quoted 52 lines

****

Quoted 42 lines

** **

Also I do a strlen(buf) and print the value as well, and it is 29 and 16 respectively.****

** **

Next I did copy_from_user(tbuf,buf,strlen(buf))****

And tried to print the contents of the buffer but it came out garbage.*** *

** **

Does anyone have any ideas how I can get this to work?****

** **

BTW: the assignment is due on Sunday so any help would be aappreciated.** **

** **

** **

** **

--
Best Regards, Donald****

[image: Oracle] <http://www.oracle.com/>
Donald raikes | Accessibility Specialist/ QA Engineer
Phone: +15202717608 | Mobile: +15202717608 Oracle Quality Assurance
| Tucson, Arizona ****

[image: Green Oracle] <http://www.oracle.com/commitment>****

Oracle is committed to developing practices and products that help protect the environment****

Quoted 8 lines

** **

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies at kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

Quoted 6 lines

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies at kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20130721/8ab92818/attachment.html