Secure file deletion - userland solution here

Pavel Machek (pavel@bug.ucw.cz)
Sat, 12 Sep 1998 17:46:59 +0200


Hi!

This is a prototype of safe deletion library. LD_PRELOAD= it and
enjoy. (This is really prototype: I _should_ parse some env variable,
getcwd() and move files to directory where they belong so that this
works on setup with multiple partitions etc.

Also, this shows pretty much weaknesses of ext2: ext2 just does not
like one big directory. Anyway, here it is, if someone wants to
improve it, write garbage collector for it etc., feel free to do so.

Pavel

PS: I see this is a bit offtopic; anyway, it might at least show that
this is easy to do outside kernel.

/*
* So you want undeletion?
*
* Copyright 1998 Pavel Machek <pavel@ucw.cz>,
* distribute under Library General Public License, version 2
*
* To use: gcc safedel.c -shared -o libsafedel.so
* then either export LD_PRELOAD=/full_path/libsafedel.so
* (which will not help suid programs, but suid programs are mostly
* safe these days)
* or if you are root and *DARING*
* echo "/full_path/libsafedel.so" >> /etc/ld.so.preload
*
* Version 0.0.1 Should work on Linux, should be possible to make it
* work on other systems.
*/

#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <syslog.h>
#include <unistd.h>

int
unlink( const char *pathname )
{
int res;
char newname[1024] = "/tmp/deleted-";

{
char *s = newname + strlen(newname);
const char *t = pathname;

while (*t) {
char c = *t++;
if (c=='/')
c='_';
*s++ = c;
}
strcpy( s, "XXXXXX" );
}
if (!mktemp(newname))
goto fallback;

if ((res = rename(pathname, newname))==-1)
goto fallback;

return res;

fallback:
return __unlink(pathname);
}

-- 
I'm really pavel@atrey.karlin.mff.cuni.cz. 	   Pavel
Look at http://atrey.karlin.mff.cuni.cz/~pavel/ ;-).

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu Please read the FAQ at http://www.tux.org/lkml/faq.html