Skip to content

Commit

Permalink
clang: fail when the kheaders ownership is wrong (#4928) (#4985)
Browse files Browse the repository at this point in the history
file_exists_and_ownedby() returns -1 when the file exists but its
ownership is unexpected, which is very misleading since anything non
zero is interpreted as true and a function with such a name is
expected to return a boolean. So currently all this does, is write a
warning message, and continues as if nothing is wrong.

Make file_exists_and_ownedby() returns false when the ownership is
wrong and have get_proc_kheaders() fails when this happen. Also have
all the *exists* functions return bool to avoid such issues in the
future.

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
  • Loading branch information
jeromemarchand committed May 17, 2024
1 parent 01bd3e4 commit 5bc97bb
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/cc/frontends/clang/kbuild_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -140,20 +140,26 @@ int KBuildHelper::get_flags(const char *uname_machine, vector<string> *cflags) {
return 0;
}

static inline int file_exists_and_ownedby(const char *f, uid_t uid)
static inline bool file_exists(const char *f)
{
struct stat buffer;
return (stat(f, &buffer) == 0);
}

static inline bool file_exists_and_ownedby(const char *f, uid_t uid)
{
struct stat buffer;
int ret = stat(f, &buffer) == 0;
if (ret) {
if (buffer.st_uid != uid) {
std::cout << "ERROR: header file ownership unexpected: " << std::string(f) << "\n";
return -1;
return false;
}
}
return ret;
}

static inline int proc_kheaders_exists(void)
static inline bool proc_kheaders_exists(void)
{
return file_exists_and_ownedby(PROC_KHEADERS_PATH, 0);
}
Expand Down Expand Up @@ -231,8 +237,14 @@ int get_proc_kheaders(std::string &dirpath)
uname_data.release);
dirpath = std::string(dirpath_tmp);

if (file_exists_and_ownedby(dirpath_tmp, 0))
return 0;
if (file_exists(dirpath_tmp)) {
if (file_exists_and_ownedby(dirpath_tmp, 0))
return 0;
else
// The path exists, but is owned by a non-root user
// Something fishy is going on
return -EEXIST;
}

// First time so extract it
return extract_kheaders(dirpath, uname_data);
Expand Down

0 comments on commit 5bc97bb

Please sign in to comment.