[PATCH v2] sysctl: fix the problem that no error is reported when access fails

zhuyan (M) zhuyan34 at huawei.com
Sat May 16 10:30:55 UTC 2020


When an general user without authority sets the system configuration,
0 is returned, but the modification is not successful. It is unreasonable.

Such as, general user didn't have /proc/sys/fs/protected_hardlinks permissions, but run the command 'busybox sysctl fs.protected_symlinks',
0 is returned.

Signed-off-by: Yan Zhu <zhuyan34 at huawei.com>
---
 procps/sysctl.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/procps/sysctl.c b/procps/sysctl.c index 6d77185..0ffa9fe 100644
--- a/procps/sysctl.c
+++ b/procps/sysctl.c
@@ -53,6 +53,7 @@ enum {
 	FLAG_QUIET           = 1 << 6,
 };
 #define OPTION_STR "neAapwq"
+bool sysctl_input_dir = false;
 
 static void sysctl_dots_to_slashes(char *name)  { @@ -170,6 +171,10 @@ static int sysctl_act_on_setting(char *setting)
 	if (fd < 0) {
 		switch (errno) {
 		case EACCES:
+			if (!sysctl_input_dir) {
+				bb_perror_msg("error reading key '%s'", outname);
+				break;
+			}
 			/* Happens for write-only settings, e.g. net.ipv6.route.flush */
 			goto end;
 		case ENOENT:
@@ -310,6 +315,7 @@ int sysctl_main(int argc UNUSED_PARAM, char **argv)  {
 	int retval;
 	int opt;
+	struct stat buf;
 
 	opt = getopt32(argv, "+" OPTION_STR); /* '+' - stop on first non-option */
 	argv += optind;
@@ -331,6 +337,7 @@ int sysctl_main(int argc UNUSED_PARAM, char **argv)
 	}
 	xchdir("/proc/sys");
 	if (opt & (FLAG_TABLE_FORMAT | FLAG_SHOW_ALL)) {
+		sysctl_input_dir = true;
 		return sysctl_act_recursive(".");
 	}
 
@@ -338,6 +345,12 @@ int sysctl_main(int argc UNUSED_PARAM, char **argv)
 
 	retval = 0;
 	while (*argv) {
+		if (stat(*argv, &buf) == 0
+		 && S_ISDIR(buf.st_mode))
+			sysctl_input_dir = true;
+		else
+			sysctl_input_dir = false;
+
 		sysctl_dots_to_slashes(*argv);
 		retval |= sysctl_act_recursive(*argv);
 		argv++;
--
2.12.3



More information about the busybox mailing list