Verified Commit b3e2f834 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 f91fd6ce
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -62,7 +62,7 @@ extern hg_id_t rpc_get_dirents_id;

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
@@ -32,10 +32,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
@@ -82,18 +93,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)));