Skip to content

Commit

Permalink
Fix executable file ownership check
Browse files Browse the repository at this point in the history
If we check ownership for a file we find in
the system PATH then we might need a .exe suffix
on the full path we identified, because although
MSYS will confirm the files existence even if the
suffix is missing, we need the "correct" path
for the actual ownership check.
  • Loading branch information
Wolfgang Pupp committed Feb 16, 2024
1 parent 4903343 commit 581db4e
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/pacman/query.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ static int search_path(char **filename, struct stat *bufptr)
path[--plen] = '\0';
}

fullname = malloc(plen + flen + 2);
/* Reserve 4 additional bytes for a potential ".exe" extension
* This is not always needed, but avoids reallocation.
*/
fullname = malloc(plen + 1 + flen + 4 + 1);
if(!fullname) {
free(envpath);
return -1;
Expand All @@ -70,6 +73,32 @@ static int search_path(char **filename, struct stat *bufptr)

if(lstat(fullname, bufptr) == 0) {
free(*filename);

/* We check if the file actually has an .exe suffix by lstat'ing
* the same path with .exe suffix and checking if the inode
* numbers are the same: If they are, we return the name with
* .exe appended, because otherwise the package ownership check
* breaks/fails.
*/
strcat(fullname, ".exe");
struct stat sbExe;
if (lstat(fullname, &sbExe) == 0) {
if (sbExe.st_ino == bufptr->st_ino) {
/* file is the same with and without .exe suffix
* => we assume that the .exe suffix is needed for the
* ownership check.
*/
*filename = fullname;
*bufptr = sbExe;
free(envpath);
return 0;
}
}
/* undo the .exe suffix, because there is no such file or it's
* different from the one without .exe suffix.
*/
fullname[plen + 1 + flen] = '\0';

*filename = fullname;
free(envpath);
return 0;
Expand Down

0 comments on commit 581db4e

Please sign in to comment.