Verified Commit 55c75585 authored by Tommaso Tocci's avatar Tommaso Tocci
Browse files

Prevent random data on stat output

Some of the variables of stat struct are not initialized, thus they will
remains will unpredictable junks data from memory. This random values
could induct strange behaviours on user applications.

Even if we don't support some of the fields in the struct stat, it is
necessary to set them to some default value.
parent f75dad97
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -87,7 +87,7 @@ int get_fd_idx();

bool is_fs_path(const char* path);

int db_val_to_stat(std::string path, std::string db_val, struct stat& attr);
int db_val_to_stat(const std::string& path, std::string db_val, struct stat& attr);

int get_daemon_pid();

+13 −8
Original line number Diff line number Diff line
@@ -64,10 +64,21 @@ bool is_fs_path(const char* path) {
 * @param attr
 * @return
 */
int db_val_to_stat(const std::string path, std::string db_val, struct stat& attr) {
int db_val_to_stat(const std::string& path, std::string db_val, struct stat& attr) {

    attr.st_ino = std::hash<std::string>{}(path);
    /* Populate default values */
    attr.st_dev = makedev(0, 0);
    attr.st_ino = std::hash<std::string>{}(path);
    attr.st_nlink = 1;
    attr.st_uid = CTX->fs_conf()->uid;
    attr.st_gid = CTX->fs_conf()->gid;
    attr.st_rdev = 0;
    attr.st_blksize = BLOCKSIZE;
    attr.st_blocks = 0;

    memset(&attr.st_atim, 0, sizeof(timespec));
    memset(&attr.st_mtim, 0, sizeof(timespec));
    memset(&attr.st_ctim, 0, sizeof(timespec));

    auto pos = db_val.find(dentry_val_delim);
    if (pos == std::string::npos) { // no delimiter found => no metadata enabled. fill with dummy values
@@ -114,18 +125,12 @@ int db_val_to_stat(const std::string path, std::string db_val, struct stat& attr
        pos = db_val.find(dentry_val_delim);
        attr.st_uid = static_cast<uid_t>(stoul(db_val.substr(0, pos)));
        db_val.erase(0, pos + 1);
    } else {
        attr.st_uid = CTX->fs_conf()->uid;
    }

    if (CTX->fs_conf()->gid_state) {
        pos = db_val.find(dentry_val_delim);
        attr.st_gid = static_cast<uid_t>(stoul(db_val.substr(0, pos)));
        db_val.erase(0, pos + 1);
    } else {
        attr.st_gid = CTX->fs_conf()->gid;
    }

    if (CTX->fs_conf()->inode_no_state) {
        pos = db_val.find(dentry_val_delim);
        attr.st_ino = static_cast<ino_t>(stoul(db_val.substr(0, pos)));