Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/cli/file-cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <ftw.h>
#include <stdbool.h>
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
Expand Down Expand Up @@ -57,7 +58,8 @@ static int ftw_add_list_append(const char *fpath,
int typeflag,
struct FTW *ftwbuf __attribute__ ((unused)))
{
if (typeflag == FTW_F) {
switch (typeflag) {
case FTW_F:
if (S_ISREG(sb->st_mode)) {
char *tmp = strdup(fpath);
if (!tmp) {
Expand All @@ -72,6 +74,28 @@ static int ftw_add_list_append(const char *fpath,
} else {
msg(LOG_INFO, "Skipping non regular file: %s", fpath);
}
break;
case FTW_SL:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block needs to be in { } in order to correctly set the scope of variables declared bellow.

case FTW_SL: {
    // declarations and code
    break;
}

char target[PATH_MAX];
ssize_t len = readlink(fpath, target, sizeof (target) - 1);
if (len == -1) {
msg(LOG_ERR, "Cannot read value of symbolic link %s: %s",
fpath, strerror(errno));
break;
}
target[len] = '\0';
struct stat st;
if (stat(fpath, &st) == -1)
msg(LOG_WARNING, "Cannot stat symbolic link %s pointing to %s: %s",
fpath, target, strerror(errno));
else if (target[0] == '/')
msg(LOG_INFO, "Skipping symbolic link %s: "
"consider adding target %s", fpath, target);
else
msg(LOG_INFO, "Skipping symbolic link %s: "
"consider adding target %s/%s",
fpath, fpath, target);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$ ls -l /usr/local/bin/myscript
lrwxrwxrwx. 1 root root 11 Mar 13 15:20 /usr/local/bin/myscript -> ../myscript

$ sudo fapolicyd-cli --file add /usr/local/bin
03/13/26 15:30:12 [ INFO ]: Skipping symbolic link /usr/local/bin/myscript: consider adding target /usr/local/bin/myscript/../myscript

$ ls /usr/local/bin/myscript/../myscript
ls: cannot access '/usr/local/bin/myscript/../myscript': Not a directory

fapth is the full path, not a directory

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The suggested path for relative symbolic links is constructed incorrectly, which can be confusing for the user. It uses the full path to the symlink file as the base for the relative target, which is incorrect. For example, if /path/to/link points to target, this will suggest adding /path/to/link/target instead of the correct /path/to/target.

You should use dirname() to get the directory part of the symlink's path to construct the correct target path. This will require including <libgen.h>.

		else {
			char *fpath_copy = strdup(fpath);
			if (fpath_copy == NULL) {
				msg(LOG_ERR, "Out of memory while processing %s", fpath);
				break;
			}
			char *dir = dirname(fpath_copy);
			msg(LOG_INFO, "Skipping symbolic link %s: "
				"consider adding target %s/%s",
				fpath, dir, target);
			free(fpath_copy);
		}

break;
}
return FTW_CONTINUE;
}
Expand Down