Note that there are some explanatory texts on larger screens.

plurals
  1. POPreventing a routine that deletes a set of files from being destructive
    primarykey
    data
    text
    <p>The following is a small excerpt of a program I have written. The routine goes through <code>filenamelist</code> (permission 0600) which contains a list of line separated file names stored that are all stored in <code>directory</code> (like <code>FileOne\nAnotherFile\nThird file\n</code>) with the file extension <code>EXTENSION</code>. The list of file names comes from another part of my program.</p> <p>I am a bit afraid that the <code>filenamelist</code> can be abused to delete other files on the system. Is there a better way of locking it down? Originally I stored the full path to the files that where to be deleted, but later separated and hardcoded the directory and file extension in an attempt to obfuscate it.</p> <p>I am possibly being paranoid, but a malicious user (!) could poison the <code>filenamelist</code> with other file paths, somehow. For example <code>../../another-directory/donotdeleteme.sys</code> or escape it someway that I cannot even think of.</p> <p>So can the below deletion routine be misused to delete files outside <code>directory</code>? Any suggestions on how I can lock it down further to prevent cat·astrophe?</p> <p>PS: The program must <em>run as root</em> as part of it’s operation is modifying files outside the user’s home directory.</p> <pre><code>#define EXTENSION ".stuff" char *directory = calloc( 28 ); directory = "/usr/local/share/stuffings/"; char *filenamelist = calloc( 24 ); filenamelist = "/etc/stuffing/files.lst"; void delete_files( char* filenamelist, char* directory ) { if ( access( filenamelist, F_OK | R_OK ) == 0 ) { FILE *filenamelist_fp = fopen( filenamelist, "r" ); char filename[200]; while( fgets( filename, 200, filenamelist_fp ) != NULL ) { char *pos; char *path = calloc( ( strlen( directory ) + strlen( filename ) + strlen( EXTENSION ) + 1 ), sizeof( char ) ); if ( ( pos=strchr( filename, '\n' ) ) != NULL ) *pos = '\0'; strcat( path, directory ); strcat( path, filename ); strcat( path, EXTENSION ); if ( access( path, F_OK | W_OK ) == 0 ) unlink( path ); free( path ); } fclose( filenamelist_fp ); unlink( filenamelist ); } } /* © */ </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload