Click here to Skip to main content
15,893,594 members
Articles / Programming Languages / C

Driver to hide files in Linux OS

,
Rate me:
Please Sign up or sign in to vote.
4.44/5 (13 votes)
2 Sep 2012CPOL7 min read 44.3K   777   34  
Simple driver for Linux OS that performs hiding of the selected files from the system
#include "device_file.h"
#include <linux/fs.h>       /* file stuff */
#include <linux/kernel.h>    /* printk() */
#include <linux/errno.h>     /* error codes */
#include <linux/module.h>    /* THIS_MODULE */
#include <linux/cdev.h>      /* char device stuff */
#include <asm/uaccess.h>     /* strncpy_from_user() */
#define SUCCESS 0

static int g_device_open = 0;

static ssize_t device_file_write ( struct file *file_ptr
                        , const char *buffer
                        , size_t length
                        , loff_t *offset)
{
	char* pFile_Path;

	pFile_Path=(char *)kmalloc(sizeof(char *)*length,GFP_KERNEL);

	if ( strncpy_from_user(pFile_Path,buffer,length)== -EFAULT) 
	{
	
		printk( KERN_NOTICE "Entered in fault get_user state");

		length=-1;
		goto finish;
	}

	if (strstr(pFile_Path,"\n"))
	{
		pFile_Path[length - 1] = 0;

		printk( KERN_NOTICE "Entered in end line filter");
	}
	
	printk( KERN_NOTICE "File path is %s without EOF", pFile_Path);

	if (hook_functions(pFile_Path)==-1) 
	{	
		length=-2;
	}
finish:
	kfree(pFile_Path);

	return length;
}

static int device_file_open(struct inode *inode, struct file *file)
{

   if (g_device_open)

   return -EBUSY;

   g_device_open++;
   try_module_get(THIS_MODULE);

   return SUCCESS;

}

static int device_file_release(struct inode *inode, struct file *file)
{

  g_device_open--;

  module_put(THIS_MODULE);
  return SUCCESS;
}

/*===============================================================================================*/
static struct file_operations hide_driver_fops = 
{
   .owner   = THIS_MODULE,
   .write   = device_file_write,
   .open    = device_file_open,
   .release = device_file_release,
};

static int device_file_major_number = 0;

static const char device_name[] = "Hide_Driver";

/*===============================================================================================*/
int register_device(void)
{
      int result = 0;

      printk( KERN_NOTICE "Hide-driver: register_device() is called." );

      result = register_chrdev( 0, device_name, &hide_driver_fops );
      if( result < 0 )
      {
         printk( KERN_WARNING "Hide-driver:  can\'t register character device with errorcode = %i", result );
         return result;
      }

      device_file_major_number = result;
      printk( KERN_NOTICE "Hide-driver: registered character device with major number = %i and minor numbers 0...255"
                  , device_file_major_number );

      return 0;
}
/*-----------------------------------------------------------------------------------------------*/
void unregister_device(void)
{
   printk( KERN_NOTICE "Hide-driver: unregister_device() is called" );
   if(device_file_major_number != 0)
   {
      unregister_chrdev(device_file_major_number, device_name);
   }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer Apriorit Inc.
United States United States
ApriorIT is a software research and development company specializing in cybersecurity and data management technology engineering. We work for a broad range of clients from Fortune 500 technology leaders to small innovative startups building unique solutions.

As Apriorit offers integrated research&development services for the software projects in such areas as endpoint security, network security, data security, embedded Systems, and virtualization, we have strong kernel and driver development skills, huge system programming expertise, and are reals fans of research projects.

Our specialty is reverse engineering, we apply it for security testing and security-related projects.

A separate department of Apriorit works on large-scale business SaaS solutions, handling tasks from business analysis, data architecture design, and web development to performance optimization and DevOps.

Official site: https://www.apriorit.com
Clutch profile: https://clutch.co/profile/apriorit
This is a Organisation

33 members

Written By
Team Leader Apriorit
Ukraine Ukraine
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions