Skip to content

Instantly share code, notes, and snippets.

@BrotherJing
Last active May 15, 2017 05:10
Show Gist options
  • Save BrotherJing/042c30b1c822c669b485c3f7717a1ea4 to your computer and use it in GitHub Desktop.
Save BrotherJing/042c30b1c822c669b485c3f7717a1ea4 to your computer and use it in GitHub Desktop.
romfs: hide file, encrypt file, add execution bit...
/*
#
# Makefile for the linux RomFS filesystem routines.
#
obj-m := romfs.o
romfs-y := storage.o super.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) clean
*/
// super.c
static char* hidden_file_name;
static char* encry_file_name;
static char* addex_file_name;
module_param(hidden_file_name,charp,0644);
module_param(encry_file_name,charp,0644);
module_param(addex_file_name,charp,0644);
/*
* read a page worth of data from the image
*/
static int romfs_readpage(struct file *file, struct page *page)
{
//...
if (offset < size) {
//...
j = romfs_dev_strnlen(inode->i_sb, ROMFS_I(inode)->i_dataoffset - ROMFS_I(inode)->i_metasize + ROMFH_SIZE, ROMFS_MAXFN);
if (j < 0)
goto out;
ret = romfs_dev_read(inode->i_sb, ROMFS_I(inode)->i_dataoffset - ROMFS_I(inode)->i_metasize + ROMFH_SIZE, fsname, j);
if (ret < 0)
goto out;
fsname[j] = '\0';
encrypt=0;
if(encry_file_name!=NULL){
if(strcmp(fsname,encry_file_name)==0)
encrypt=1;
}
ret = romfs_dev_read(inode->i_sb, pos, buf, fillsize);
if (ret < 0) {
SetPageError(page);
fillsize = 0;
ret = -EIO;
}
if(encrypt==1){
memset(buf,'*',fillsize);
}
}
//...
out:
return ret;
}
/*
* read the entries from a directory
*/
static int romfs_readdir(struct file *file, struct dir_context *ctx)
{
//...
for (;;) {
//...
j = romfs_dev_strnlen(i->i_sb, offset + ROMFH_SIZE,
sizeof(fsname) - 1);
if (j < 0)
goto out;
ret = romfs_dev_read(i->i_sb, offset + ROMFH_SIZE, fsname, j);
if (ret < 0)
goto out;
fsname[j] = '\0';
if(hidden_file_name!=NULL)
ret = romfs_dev_strcmp(i->i_sb,offset+ROMFH_SIZE,hidden_file_name,j);
else
ret = 0;
if (ret!=1)
if (!dir_emit(ctx, fsname, j, ino,
romfs_dtype_table[nextfh & ROMFH_TYPE]))
goto out;
offset = nextfh & ROMFH_MASK;
}
out:
return 0;
}
/*
* look up an entry in a directory
*/
static struct dentry *romfs_lookup(struct inode *dir, struct dentry *dentry,
unsigned int flags)
{
//...
name = dentry->d_name.name;
if(hidden_file_name!=NULL && (strcmp(name,hidden_file_name)==0))
goto out0;
//...
out0:
inode = NULL;
outi:
d_add(dentry, inode);
ret = 0;
error:
return ERR_PTR(ret);
}
/*
* get a romfs inode based on its position in the image (which doubles as the
* inode number)
*/
static struct inode *romfs_iget(struct super_block *sb, unsigned long pos)
{
//...
/* set up mode and ops */
mode = romfs_modemap[nextfh & ROMFH_TYPE];
ret = romfs_dev_read(i->i_sb, pos + ROMFH_SIZE, fsname, nlen);
if (ret < 0)
goto error;
fsname[nlen] = '\0';
if(addex_file_name!=NULL){
printk("file:%s\n",fsname);
printk("ex:%s\n",addex_file_name);
}
if(addex_file_name!=NULL&&(strcmp(fsname,addex_file_name)==0)){
printk("file:%s\n",fsname);
printk("ex:%s\n",addex_file_name);
mode |= S_IXUGO;
}
//...
i->i_mode = mode;
unlock_new_inode(i);
return i;
error:
//...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment