RE: memory allocation

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Better solution i can think off is:

when ioctl is called in user space initialize len argument to size of
string. 
In the kernel, verify len. If len (passed from user level) is more than
lenght of the string, then copy the string otherwise send error.

Regards,
Madhu

On Thu, 16 Jan 2003, Mandeep S. Brar wrote:

> Thanks, but that is the very problem, I do not know the size of the data in
> the user space.
> One probable solution could be that i make two ioctl calls wherein i get the
> length of the data in the first call and then the data in the second call.
> But isnt there a better solution then this one!!!
> 
> Regards
> Mandeep
> 
> -----Original Message-----
> From: T.L.Madhu [mailto:madhu.tarikere@wipro.com]
> Sent: Thursday, January 16, 2003 5:19 PM
> To: Mandeep S. Brar
> Cc: kernelnewbies@nl.linux.org
> Subject: Re: memory allocation
> 
> 
> 
> As i know, you have to allocate memory either statically or dynamically  
> in user space for var.string and pass pointer thro. ioctl.
> 
> copy_to_user() is not going to allocate memory, it just copies data from
> kernel space to user space.
> 
> Ensure that allocated memory for string member will always be more than
> len+1 (of kernel) to avoid memory corruption.
> 
> Regards,
> Madhu
> 
> 
> On Thu, 16 Jan 2003, Mandeep S. Brar wrote:
> 
> > hi,
> > I am a newbie into linux programming. Ps help me with a small problem i am
> > facing while writing a sample program for a driver module.
> > I have a structure like
> > struct devstruct {
> > 	char* string;
> > 	int len;
> > };
> > I want to return information from the driver to a user process which
> queries
> > me with ioctl. The code has the following outline...
> > 
> > USER PROCESS
> > ...........
> > struct devstruct var;
> > ioctl(fd, READ_COMMAND, &var);
> > printf("%s",var.string);
> > .....................
> > 
> > 
> > DRIVER Module
> > 
> > char* data="hello";
> > int len=5;
> > 
> > int myioctl (struct inode *inode, struct file *filp, unsigned int cmd,
> > unsigned long arg){
> > 	switch(cmd) {
> > 		.............
> > 
> > 		case READ_COMMAND: 
> > 			{
> > 				.....
> > 				struct devstruct* var = (struct
> > devstruct*)arg;
> > 				var->len = len;
> > 				/*********
> > 					allocate memory in user space?
> > 				**********/
> > 				var->string = (allocate memory = len+1 in
> > user space)
> > 				copy_to_user(var->string,data,len+1);
> > 				.....
> > 			}
> > 			break;		
> > 		.............
> > 	}
> > }
> > My question is that how do i allocate memory in user space when i am in
> > kernel space or will copy_to_user do it automatically?. is there a variant
> > of kmalloc or what??
> > 
> > 
> > Thanks in advance
> > Mandeep
> > 
> > 
> > --
> > Kernelnewbies: Help each other learn about the Linux kernel.
> > Archive:       http://mail.nl.linux.org/kernelnewbies/
> > FAQ:           http://kernelnewbies.org/faq/
> > 
> 

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/



[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux