Re: Kernel Modules

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

 



Hi,

I intercepted the existing socketcall function with my LKM to allow me to
communicate with
kernel-based code.

Build your LKM defining -mpreferred-stack-boundary=2 (If I don't do this
things go boom).

In your LKM:

extern int printk(const char* fmt, ...);

extern void * sys_call_table[];

int (*pfnOldSocketCall)(int    call,
      unsigned long * args);


/* Well, not all ports of linux deal with syscalls in the same manner. Some
*/
/* such as ARM and MIPS use bias values which are used to indicate a ssys
call */
/* however, the bias is subtracted out prior to using the Syscall as an
index   */
/* into an array of syscall function pointers.         */


static int CorrectSysCall(int iSysCall)
{
 static int iSysCallTableBias = 0;

#ifdef __NR_SYSCALL_BASE
 iSysCallTableBias = __NR_SYSCALL_BASE;
#endif
#ifdef __NR_LINUX
 iSysCallTableBias = __NR_LINUX
#endif
 return (iSysCall - iSysCallTableBias);
}

/* Our hook. */
static int OurEntryPoint (int call, unsigned long *pArgs)
{
 if (call == FS_CALL_ID)
 {
/* Its for us. pArgs should contain a sanity check field just to be certain.
Provide a PROBE function so that User Space can see if we are here. */
}
else
     return(pfnOldSocketCall(call, pArgs));  /* not for us. Call original */
}


int init_module( void)
{
 printk(KERN_INFO"lkm: sys_call_table @ %p. Idx= %d, Adj Idx= %d.\n",
   sys_call_table, __NR_socketcall, CorrectSysCall(__NR_socketcall));
 pfnOldSocketCall = sys_call_table[CorrectSysCall(__NR_socketcall)];
 sys_call_table[CorrectSysCall(__NR_socketcall)] = (void*)FsEntryPoint;
 printk(KERN_INFO"init_module success.\n");
 return(0);
}

void cleanup_module(void)
{
  sys_call_table[CorrectSysCall(__NR_socketcall)] = (void*)
pfnOldSocketCall;
 printk(KERN_INFO "Cleanup done.\n");
}


#endif /* MODULE */

In a shared header:

#define FS_CALL_ID  0x55AA1234
#define FS_SANITY 0xFEEDDADA

In User space within a shared library:


/* This is how I call the LKM. The LKM, if present, will hook the
sys_socketcall   */
/* function.                  */

_syscall2(long,  socketcall,
  int, call,
  void *, pvArgs);


inline static int LkmApi(int iFnNum,  /* Our function number */
        PARGS pArgs) /* pointer to our args.*/
{
 pArgs->iFunction  = iFnNum;
 pArgs->iSanity   = FS_SANITY;
 return(socketcall(FS_CALL_ID, pArgs));
}

Anyways, thats how I did it and it seems to work very well.

Stuart





----- Original Message -----
From: "Amit Purohit" <whoami_t@yahoo.com>
To: <kernelnewbies@nl.linux.org>
Sent: Tuesday, November 20, 2001 5:06 PM
Subject: Kernel Modules


> I am writing a kernel module.
>
> Currently the entry point to my module is a SYSTEM
> CALL.
>
> My problem is I have to compile the whole kernel each
> time I make any changes to my code. So I want to
> convert my code to a lodable module. So that I don't
> need to recompile the kernel each time.
>
> I am not sure how should I provide an entry point to
> the user processes to my module. I don't want to use
> ioctl as an entry point to my module.
>
> For Eg. My module has defined function "foo". How
> should I make this function available to the user
> process ?
>
> Thanks
> --Amit
>
> __________________________________________________
> Do You Yahoo!?
> Yahoo! GeoCities - quick and easy web site hosting, just $8.95/month.
> http://geocities.yahoo.com/ps/info1
> --
> Kernelnewbies: Help each other learn about the Linux kernel.
> Archive:       http://mail.nl.linux.org/kernelnewbies/
> IRC Channel:   irc.openprojects.net / #kernelnewbies
> Web Page:      http://www.kernelnewbies.org/

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
IRC Channel:   irc.openprojects.net / #kernelnewbies
Web Page:      http://www.kernelnewbies.org/



[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