Comprehensive Secure Deletion for ext4
Subsystems (2)
RFC PATCH 1/1 Comprehensive Secure Deletion for ext4
Hello All,
As promised a few weeks ago, we have composed a complete patch to add secure deletion functionality in ext4. With this patch, we can ensure that data, meta-data and the filename of any file marked with the special attribute "s", are overwritten right after it is deleted.
The patch performs N overwrites with configurable patterns. It can handle separate overwrite configurations for each of the data, meta-data and directory entry. In order to handle partial file truncations, our patch will intercept truncate requests in addition to unlink requests (something that user-mode programs such as shred cannot handle). Moreover, to make the data overwrites and file truncation an atomic operation, we add the file to an orphan list right before we begin to overwrite and remove it from the orphan list after truncation has finished. The patch works in all journaling modes.
As far as the journal is concerned, we leave it untouched. There are a few reasons behind this approach. First, performance will be noticeably hindered. This is because the journal will have to postpone all further transactions until the necessary blocks are overwritten - which will take time. Secondly, by the time we want to overwrite the necessary blocks, the journal will have already done this for us by committing all further transactions, especially in full journaling mode. That said, if the community will appear to also want secure-deletion of journal records, then we can add that (probably as a separate feature one can turn on independently).
Secure deletion arguments during mount time are of the form:
mount -t ext4dev -o secdel="args" [dev] [mnt]
where args is a 6-way list delimited by colons as such:
secdel="N1:S1:N2:S2:N3:S3"
N1,N2,N3 denote the number of overwrites for data,meta-data and the filename respectively.
S1,S2,S3 denote the character sequence used to overwrite data,meta-data and the filename respectively. Random character sequences are denoted by 'R'.
Any item that is left empty is not securely deleted.
In addition, if one wants to secure-delete all portions of a file, an abbreviated format can be used as such:
secdel="N:S"
N denotes the number of overwrites chosen
S denotes the character sequence to use for overwriting
For example, the mount option:
secdel="2:Rc0:::1:\0"
Means that data will be overwritten once using a random character, then once using the ASCII character 'c', and finally once more using the ASCII character 0. Finally this whole sequence will be written 2 times. Meta-data will not be touched and the filename will be overwritten 1 time with the value zero.
Any comments, help or feedback will be greatly appreciated.
Thank you,
Harry Papaxenopoulos, Nikolai Joukov, and Erez Zadok
--------------------------------------
File systems and Storage Laboratory
Stony Brook University
--------------------------------------
fs/ext4/inode.c +319 -2
@@ -37,8 +37,10 @@#include <linux/mpage.h> #include <linux/uio.h> #include <linux/bio.h> +#include <linux/random.h> #include "xattr.h" #include "acl.h" +#include "secrm.h" /* * Test whether an inode is a fast symlink.
@@ -201,8 +203,19 @@if (IS_SYNC (inode)) handle->h_sync =1 ; inode->i_size =0 ; +#ifndef CONFIG_EXT4DEV_FS_SECRM if (inode->i_blocks)ext4_truncate (inode); +#else +if (EXT4_I (inode)->i_flags &EXT4_SECRM_FL ) +ext4_journal_stop (handle); +if (inode->i_blocks) +ext4_truncate (inode); +if (EXT4_I (inode)->i_flags &EXT4_SECRM_FL ) { +ext4_secrm_metadata (inode); + handle =start_transaction (inode); + } +#endif /* * Kill off the orphan record which ext4_truncate created. * AKPM: I think this can be inside the above `if'.
@@ -1082,6 +1095,272 @@return NULL ; } +#ifdef CONFIG_EXT4DEV_FS_SECRM +static int ext4_ovwt_blocks (handle_t *handle,struct inode *inode,char param, +sector_t first,sector_t last) +{ +int retval =0 ; +sector_t sector; +sector_t batch =DIO_CREDITS ; +struct buffer_head *bh =NULL ; +struct buffer_head tmp; +struct super_block *sb; +J_ASSERT (handle !=NULL ); + + tmp.b_state =0 ; + tmp.b_blocknr =0 ; + sb = inode->i_sb; + +J_ASSERT ( last >= first ); + +if (IS_ERR (handle)) { + retval =PTR_ERR (handle); + goto out; + } + +for (sector = first; sector <= last; sector++) { +if (handle->h_buffer_credits <=EXT4_RESERVE_TRANS_BLOCKS ) { + retval =ext4_journal_extend (handle,DIO_CREDITS ); +if (retval) + retval =ext4_journal_restart (handle, +DIO_CREDITS ); +if (retval) + goto out; + } + +ext4_get_block (inode, sector, &tmp,0 ); + bh =sb_getblk (sb, tmp.b_blocknr); +if (!bh) +continue ; + +if (ext4_should_journal_data (inode)) { + retval =ext4_journal_get_write_access (handle, bh); +if (retval) + goto out; + } + +lock_buffer (bh); +if (param ==RANDOM ) +get_random_bytes (bh->b_data, bh->b_size); +else +memset (bh->b_data, param, bh->b_size); +unlock_buffer (bh); + +if (ext4_should_journal_data (inode)) { +set_buffer_uptodate (bh); + retval =ext4_journal_dirty_metadata (handle, bh); +if (retval) + goto out; + }else { +if (ext4_should_order_data (inode)) { + retval =ext4_journal_dirty_data (handle, bh); +if (retval) + goto out; + } +mark_buffer_dirty (bh); + } +brelse (bh); + } + bh =NULL ; +out: +if (retval) +ext4_journal_abort_handle (NULL ,NULL , bh, handle, retval); +if (bh) +brelse (bh); +return retval; +} + +static int ext4_ovwt_metadata (handle_t *handle,struct inode *inode,char param) +{ +int retval =0 ; +int param_int =0 ; + +if (param ==RANDOM ) +get_random_bytes ((char *)¶m_int,sizeof (int )); +else + param_int = param | param<<8 | param<<16 | param<<24 ; + + inode->i_uid = param_int; + inode->i_gid = param_int; + inode->i_atime.tv_sec = param_int; + inode->i_atime.tv_nsec = param_int; + inode->i_mtime.tv_sec = param_int; + inode->i_mtime.tv_nsec = param_int; + inode->i_ctime.tv_sec = param_int; + inode->i_ctime.tv_nsec = param_int; + +return retval; +} + +int ext4_secure_truncate (struct inode *inode,sector_t first,sector_t last) +{ + +struct ext4_sb_info *sbi =EXT4_SB (inode->i_sb); +int err =0 ; +int num_ovwt =0 ; +int char_pos =0 ; +handle_t *handle; + +/* +* Copy all secure deletion options so I don't have to de-reference +* every time. +*/ +int data_num = sbi->s_data_num_ovwt; +int data_size = sbi->s_data_char_size; +char *data_c = sbi->s_data_char; + +/* +* I'm adding the inode to the orphan in a dirty way (making it a +* transaction of it's own), since I don't want to add it inside the +* loop. I should put it within the first transaction since +* I'll probably have a performance hit if I keep it here. +*/ + handle =ext4_journal_start (inode,DIO_CREDITS ); + +if (IS_ERR (handle)) { + err =PTR_ERR (handle); + goto out; + } + +/* +* Ok start secure truncation. Add the inode to the orhpan list +* for any possible crashes during the operation. +*/ +if (ext4_orphan_add (handle, inode)) + goto out; + +ext4_journal_stop (handle); + + handle =NULL ; + +next_ovwt: + char_pos =0 ; +while (char_pos < data_size) { + + handle =ext4_journal_start (inode,DIO_CREDITS ); + +if (IS_ERR (handle)) { + err =PTR_ERR (handle); + goto del_orphan_out; + } +if (char_pos < data_size && num_ovwt < data_num) { + err =ext4_ovwt_blocks (handle, inode, + *(data_c + char_pos), first, last); +if (err) + goto stop_out; + } + +ext4_journal_stop (handle); + +jbd2_journal_lock_updates (EXT4_SB (inode->i_sb)->s_journal); + err =jbd2_journal_flush (EXT4_SB (inode->i_sb)->s_journal); +jbd2_journal_unlock_updates (EXT4_SB (inode->i_sb)->s_journal); +if (err) + goto del_orphan_out; + err =sync_blockdev (inode->i_sb->s_bdev); +if (err) + goto del_orphan_out; + + char_pos++; + + + } + num_ovwt++; + +if (num_ovwt < data_num) + goto next_ovwt; + +return err; +stop_out: +ext4_journal_stop (handle); +del_orphan_out: +ext4_orphan_del (handle, inode); +out: +return err; +} + +int ext4_secrm_metadata (struct inode *inode) +{ +struct ext4_sb_info *sbi =EXT4_SB (inode->i_sb); +int err =0 ; +int num_ovwt =0 ; +int char_pos =0 ; +handle_t *handle; + +/* +* Copy all secure deletion options so I don't have to dereference +* every time. +*/ +int meta_num = sbi->s_meta_num_ovwt; +int meta_size = sbi->s_meta_char_size; +char *meta_c = sbi->s_meta_char; + +/* I'm adding the inode to the orphan in a dirty way (making it a +* transaction of it's own), since I don't want to add it inside the +* loop. Normally I should put it within the first transaction since +* I'll probably have a performance hit if I keep it here. +*/ + handle =ext4_journal_start (inode,DIO_CREDITS ); + +if (IS_ERR (handle)) { + err =PTR_ERR (handle); + goto out; + } + +if (ext4_orphan_add (handle, inode)) + goto out; + +ext4_journal_stop (handle); + + handle =NULL ; + + +next_ovwt: + char_pos =0 ; +while (char_pos < meta_size) { + + handle =ext4_journal_start (inode,DIO_CREDITS ); + +if (IS_ERR (handle)) { + err =PTR_ERR (handle); + goto del_orphan_out; + } + +if (char_pos < meta_size && num_ovwt < meta_num) { + err =ext4_ovwt_metadata (handle, inode, + *(meta_c + char_pos)); +if (err) + goto stop_out; +mark_inode_dirty (inode); + } + +ext4_journal_stop (handle); + +jbd2_journal_lock_updates (EXT4_SB (inode->i_sb)->s_journal); + err =jbd2_journal_flush (EXT4_SB (inode->i_sb)->s_journal); +jbd2_journal_unlock_updates (EXT4_SB (inode->i_sb)->s_journal); +if (err) + goto del_orphan_out; + err =sync_blockdev (inode->i_sb->s_bdev); +if (err) + goto del_orphan_out; + char_pos++; + } + num_ovwt++; + +if (num_ovwt < meta_num) + goto next_ovwt; + +return err; +stop_out: +ext4_journal_stop (handle); +del_orphan_out: +ext4_orphan_del (handle,inode); +out: +return err; +} +#endif + static int walk_page_buffers( handle_t *handle, struct buffer_head *head, unsigned from,
@@ -2239,6 +2518,11 @@long last_block;unsigned blocksize = inode->i_sb->s_blocksize;struct page *page; +__u32 i_flags; +#ifdef CONFIG_EXT4DEV_FS_SECRM +sector_t first; +sector_t last; +#endif if (!(S_ISREG (inode->i_mode) ||S_ISDIR (inode->i_mode) ||S_ISLNK (inode->i_mode)))
@@ -2262,6 +2546,20 @@return ; } +#ifdef CONFIG_EXT4DEV_FS_SECRM + last_block = (inode->i_size + blocksize-1 ) + >>EXT4_BLOCK_SIZE_BITS (inode->i_sb); + + first = last_block; + last = (ei->i_disksize + blocksize-1 ) + >>EXT4_BLOCK_SIZE_BITS (inode->i_sb); + +if (EXT4_I (inode)->i_flags &EXT4_SECRM_FL ) { +if (ext4_secure_truncate (inode, first, last)) +return ; + } +#endif +if (EXT4_I (inode)->i_flags &EXT4_EXTENTS_FL )return ext4_ext_truncate (inode, page);
@@ -2276,8 +2574,10 @@return ;/* AKPM: return what? */ } +#ifndef CONFIG_EXT4DEV_FS_SECRM last_block = (inode->i_size + blocksize-1 ) >>EXT4_BLOCK_SIZE_BITS (inode->i_sb); +#endif if (page)ext4_block_truncate_page (handle, page, mapping, inode->i_size);
@@ -2286,7 +2586,22 @@if (n ==0 ) goto out_stop;/* error */ +#ifdef CONFIG_EXT4DEV_FS_SECRM + i_flags =EXT4_I (inode)->i_flags; +#else +/* +* Get rid of the SECRM_FL just in case a user set it. +*/ + i_flags =EXT4_I (inode)->i_flags & ~EXT4_SECRM_FL ; +#endif +/* +* If secure removal is compiled in the file-system, we have +* already added the inode to the orphan list because we need to +* make the overwrites and the truncation an atomic operation. +* Otherwise under any system crash our inode will not be correctly +* deleted. +* * OK. This truncate is going to happen. We add the inode to the * orphan list, so that if this truncate spans multiple transactions, * and we crash, we will resume the truncate when the filesystem
@@ -2295,8 +2610,10 @@* Implication: the file must always be in a sane, consistent * truncatable state while each transaction commits. */ -if (ext4_orphan_add (handle, inode)) - goto out_stop; +if (!i_flags &EXT4_SECRM_FL ) { +if (ext4_orphan_add (handle, inode)) + goto out_stop; + }/* * The orphan list entry will now protect us from any crash which
fs/ext4/namei.c +107 -1
152 lines not on this page · raw message
fs/ext4/secrm.h +16 -0
@@ -0,0 +1,16 @@ +/* linux/fs/ext3/secrm.h +* +* Copyright (C) 2006 Stony Brook University +* +*/ + +/*Special character for random bit generation */ +#define RANDOM 'R' + +/* inode.c */ +extern int ext4_secrm_metadata (struct inode *inode); +extern int ext4_secure_truncate (struct inode *inode,sector_t first, +sector_t last); +/* super.c */ +extern int ext4_store_secrm_options (struct ext4_sb_info *sbi,char *args); +extern void ext4_secrm_put_super (struct ext4_sb_info *sbi);
fs/ext4/super.c +227 -1
276 lines not on this page · raw message
fs/Kconfig +12 -0
@@ -207,6 +207,18 @@ If you are not using a security module that requires using extended attributes for file security labels, say N. +config EXT4DEV_FS_SECRM + bool "Ext4dev Secure Deletion" + default n + depends on EXT4DEV_FS + help + Secure deletion overwrites a file's data, metadata and + directory entry before deleting. This option is useful for + making sure that sensitive documents will unretrievable once + deleted. + + If unsure, say N. + config JBD tristate help
include/linux/ext4_fs_sb.h +17 -0
@@ -89,6 +89,23 @@unsigned long s_ext_blocks;unsigned long s_ext_extents;#endif + +#ifdef CONFIG_EXT4DEV_FS_SECRM +/* Number of overwrites for each field */ +u32 s_data_num_ovwt; +u32 s_meta_num_ovwt; +u32 s_file_num_ovwt; + +/* Characters to be written to each field */ +char *s_data_char; +char *s_meta_char; +char *s_file_char; + +/* Size of above character array for each field */ +u32 s_data_char_size; +u32 s_meta_char_size; +u32 s_file_char_size; +#endif };#endif /* _LINUX_EXT4_FS_SB */