Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Path sanitization in C++

I'm writing a small read-only FTP-like server. Client says "give me that file" and my server sends it.

Is there any standard way (a library function?!?) to make sure that the file requested is not "../../../../../etc/passwd" or any other bad thing? It would be great if I could limit all queries to a directory (and its subdirectories).

Thank you!

like image 955
zakk Avatar asked Nov 27 '22 15:11

zakk


2 Answers

Chroot is probably the best way to go, but you can use realpath(3) to determine the canonical path to a given filename. From the man page:

 char *realpath(const char *file_name, char *resolved_name);

The realpath() function resolves all symbolic links, extra '/' characters, and references to /./ and /../ in filename, and copies the resulting absolute pathname into the memory referenced by resolved name. The resolved_name argument must refer to a buffer capable of storing at least PATH_MAX characters.

From there you can restrict the request in any additional way you like.

like image 89
Peter Kovacs Avatar answered Dec 06 '22 01:12

Peter Kovacs


Also take a look at chroot

like image 31
Duck Avatar answered Dec 06 '22 01:12

Duck