Disallow redirecting to stdout based on config keys 94/294294/4
authorAntoni <a.adaszkiewi@samsung.com>
Thu, 15 Jun 2023 19:58:06 +0000 (21:58 +0200)
committerMichal Bloch <m.bloch@samsung.com>
Thu, 22 Jun 2023 17:30:27 +0000 (19:30 +0200)
Change-Id: I592a75d36a2915bf786ae54cc94591d606fa01c1

Makefile.am
configs/dlog-logger.conf.stdout_enable.1 [new file with mode: 0644]
configs/dlog-logger.conf.stdout_enable.2 [new file with mode: 0644]
configs/dlog-logger.conf.stdout_enable.3 [new file with mode: 0644]
configs/dlog-pipe.conf.stdout_enable.1 [new file with mode: 0644]
configs/dlog-pipe.conf.stdout_enable.2 [new file with mode: 0644]
configs/dlog-pipe.conf.stdout_enable.3 [new file with mode: 0644]
packaging/dlog.spec
src/log-redirect-stdout/lib.c
tests/dlog_test.in
tests/test_config_redirect.c [new file with mode: 0644]

index 85c67ec..94a02cf 100644 (file)
@@ -459,6 +459,24 @@ test_libredirect_multi_LDADD = \
 test_libredirect_multi_SOURCES = \
        tests/test_libredirect_multi.c
 
+usrlibexeclibdlog_PROGRAMS += test_config_redirect
+test_config_redirect_CFLAGS = \
+       $(AM_CFLAGS) \
+       -fPIE
+
+test_config_redirect_LDFLAGS = \
+       $(AM_LDFLAGS) \
+       -pie
+
+test_config_redirect_DEPENDENCIES = \
+       libdlog_redirect_stdout.la
+
+test_config_redirect_LDADD = \
+       libdlog_redirect_stdout.la
+
+test_config_redirect_SOURCES = \
+       tests/test_config_redirect.c
+
 
 check_PROGRAMS = \
        src/tests/test_ptrs_list_foreach_pos \
@@ -907,7 +925,13 @@ data_DATA = \
        configs/dlog-pipe.conf.plog \
        configs/dlog-logger.conf.plog \
        configs/dlog-pipe.conf.autodetect \
-       configs/dlog-logger.conf.autodetect
+       configs/dlog-logger.conf.autodetect \
+       configs/dlog-logger.conf.stdout_enable.1 \
+       configs/dlog-logger.conf.stdout_enable.2 \
+       configs/dlog-logger.conf.stdout_enable.3 \
+       configs/dlog-pipe.conf.stdout_enable.1 \
+       configs/dlog-pipe.conf.stdout_enable.2 \
+       configs/dlog-pipe.conf.stdout_enable.3
 
 unitdir = $(prefix)/lib/systemd/system
 nodist_unit_DATA = units/dlog_logger.service \
diff --git a/configs/dlog-logger.conf.stdout_enable.1 b/configs/dlog-logger.conf.stdout_enable.1
new file mode 100644 (file)
index 0000000..4e91553
--- /dev/null
@@ -0,0 +1,42 @@
+##### Settings used by the logging library
+#backend=logger
+main=/dev/log_main
+apps=/dev/log_apps:/dev/log_events:
+system=/dev/log_system:
+radio=/dev/log_radio:
+
+main_size=1048576
+apps_size=1048576
+system_size=1048576
+radio_size=1048576
+
+plog=1
+debugmode=1
+limiter=1
+limiter|*|*=allow
+kmsg=:
+syslog=:
+
+kmsg_conn_sock=/var/lib/dlog-tests/kmsg.conn
+syslog_conn_sock=/var/lib/dlog-tests/syslog.conn
+
+kmsg_ctl_sock=/var/lib/dlog-tests/kmsg.ctl
+syslog_ctl_sock=/var/lib/dlog-tests/syslog.ctl
+
+kmsg_conn_sock_rights=0222
+syslog_conn_sock_rights=0222
+
+kmsg_ctl_sock_rights=0222
+syslog_ctl_sock_rights=0222
+
+kmsg_size=1048576
+syslog_size=1048576
+
+util_sorting_time_window=1000
+util_sorting_buffer_size=8192
+dynamic_config_path=/tmp/dlog-filters/
+
+stdout_enable_main=0
+stdout_enable_radio=0
+stdout_enable_system=0
+stdout_enable_apps=0
diff --git a/configs/dlog-logger.conf.stdout_enable.2 b/configs/dlog-logger.conf.stdout_enable.2
new file mode 100644 (file)
index 0000000..a163a3c
--- /dev/null
@@ -0,0 +1,42 @@
+##### Settings used by the logging library
+#backend=logger
+main=/dev/log_main
+apps=/dev/log_apps:/dev/log_events:
+system=/dev/log_system:
+radio=/dev/log_radio:
+
+main_size=1048576
+apps_size=1048576
+system_size=1048576
+radio_size=1048576
+
+plog=1
+debugmode=1
+limiter=1
+limiter|*|*=allow
+kmsg=:
+syslog=:
+
+kmsg_conn_sock=/var/lib/dlog-tests/kmsg.conn
+syslog_conn_sock=/var/lib/dlog-tests/syslog.conn
+
+kmsg_ctl_sock=/var/lib/dlog-tests/kmsg.ctl
+syslog_ctl_sock=/var/lib/dlog-tests/syslog.ctl
+
+kmsg_conn_sock_rights=0222
+syslog_conn_sock_rights=0222
+
+kmsg_ctl_sock_rights=0222
+syslog_ctl_sock_rights=0222
+
+kmsg_size=1048576
+syslog_size=1048576
+
+util_sorting_time_window=1000
+util_sorting_buffer_size=8192
+dynamic_config_path=/tmp/dlog-filters/
+
+stdout_enable_main=1
+stdout_enable_radio=1
+stdout_enable_system=0
+stdout_enable_apps=0
diff --git a/configs/dlog-logger.conf.stdout_enable.3 b/configs/dlog-logger.conf.stdout_enable.3
new file mode 100644 (file)
index 0000000..8b05fd1
--- /dev/null
@@ -0,0 +1,42 @@
+##### Settings used by the logging library
+#backend=logger
+main=/dev/log_main
+apps=/dev/log_apps:/dev/log_events:
+system=/dev/log_system:
+radio=/dev/log_radio:
+
+main_size=1048576
+apps_size=1048576
+system_size=1048576
+radio_size=1048576
+
+plog=1
+debugmode=1
+limiter=1
+limiter|*|*=allow
+kmsg=:
+syslog=:
+
+kmsg_conn_sock=/var/lib/dlog-tests/kmsg.conn
+syslog_conn_sock=/var/lib/dlog-tests/syslog.conn
+
+kmsg_ctl_sock=/var/lib/dlog-tests/kmsg.ctl
+syslog_ctl_sock=/var/lib/dlog-tests/syslog.ctl
+
+kmsg_conn_sock_rights=0222
+syslog_conn_sock_rights=0222
+
+kmsg_ctl_sock_rights=0222
+syslog_ctl_sock_rights=0222
+
+kmsg_size=1048576
+syslog_size=1048576
+
+util_sorting_time_window=1000
+util_sorting_buffer_size=8192
+dynamic_config_path=/tmp/dlog-filters/
+
+stdout_enable_main=0
+stdout_enable_radio=0
+stdout_enable_system=1
+stdout_enable_apps=1
diff --git a/configs/dlog-pipe.conf.stdout_enable.1 b/configs/dlog-pipe.conf.stdout_enable.1
new file mode 100644 (file)
index 0000000..01b43d1
--- /dev/null
@@ -0,0 +1,94 @@
+# Lines starting with # are comments and are ignored.
+# Empty lines are also ignored.
+# Every other line is a config value and has to be in key=value format
+
+backend=pipe
+
+##### Settings used by the logging library
+
+# Platform logging. Whether logging happens at all.
+plog=1
+
+# Debugmode - whether write platform debug log or not
+debugmode=1
+
+# Limiter - whether it's enabled and the rules (check the documentation for how to create rules).
+limiter=1
+limiter|*|*=allow
+
+##### Settings used by the logging daemon
+
+# Sockets that all programs connect to to receive a logging pipe.
+main_conn_sock=/var/lib/dlog-tests/main.conn
+apps_conn_sock=/var/lib/dlog-tests/apps.conn
+system_conn_sock=/var/lib/dlog-tests/system.conn
+radio_conn_sock=/var/lib/dlog-tests/radio.conn
+kmsg_conn_sock=/var/lib/dlog-tests/kmsg.conn
+syslog_conn_sock=/var/lib/dlog-tests/syslog.conn
+
+# Sockets that administrative programs connect to to manipulate the daemon.
+main_ctl_sock=/var/lib/dlog-tests/main.ctl
+apps_ctl_sock=/var/lib/dlog-tests/apps.ctl
+system_ctl_sock=/var/lib/dlog-tests/system.ctl
+radio_ctl_sock=/var/lib/dlog-tests/radio.ctl
+kmsg_ctl_sock=/var/lib/dlog-tests/kmsg.ctl
+syslog_ctl_sock=/var/lib/dlog-tests/syslog.ctl
+
+# Permissions for the relevant sockets.
+main_conn_sock_rights=0222
+apps_conn_sock_rights=0222
+system_conn_sock_rights=0222
+radio_conn_sock_rights=0222
+kmsg_conn_sock_rights=0222
+syslog_conn_sock_rights=0222
+
+main_ctl_sock_rights=0222
+apps_ctl_sock_rights=0222
+system_ctl_sock_rights=0222
+radio_ctl_sock_rights=0222
+kmsg_ctl_sock_rights=0222
+syslog_ctl_sock_rights=0222
+
+main_conn_sock_owner=log
+apps_conn_sock_owner=log
+system_conn_sock_owner=log
+radio_conn_sock_owner=log
+kmsg_conn_sock_owner=log
+syslog_conn_sock_owner=log
+
+main_ctl_sock_owner=log
+apps_ctl_sock_owner=log
+system_ctl_sock_owner=log
+radio_ctl_sock_owner=log
+kmsg_ctl_sock_owner=log
+syslog_ctl_sock_owner=log
+
+main_conn_sock_group=log
+apps_conn_sock_group=log
+system_conn_sock_group=log
+radio_conn_sock_group=log
+kmsg_conn_sock_group=log
+syslog_conn_sock_group=log
+
+main_ctl_sock_group=log
+apps_ctl_sock_group=log
+system_ctl_sock_group=log
+radio_ctl_sock_group=log
+kmsg_ctl_sock_group=log
+syslog_ctl_sock_group=log
+
+main_size=1048576
+apps_size=1048576
+system_size=1048576
+radio_size=1048576
+kmsg_size=1048576
+syslog_size=1048576
+
+util_sorting_time_window=5000
+util_sorting_buffer_size=32000
+dynamic_config_path=/tmp/dlog-filters/
+
+stdout_enable_main=0
+stdout_enable_radio=0
+stdout_enable_system=0
+stdout_enable_apps=0
diff --git a/configs/dlog-pipe.conf.stdout_enable.2 b/configs/dlog-pipe.conf.stdout_enable.2
new file mode 100644 (file)
index 0000000..b602af6
--- /dev/null
@@ -0,0 +1,94 @@
+# Lines starting with # are comments and are ignored.
+# Empty lines are also ignored.
+# Every other line is a config value and has to be in key=value format
+
+backend=pipe
+
+##### Settings used by the logging library
+
+# Platform logging. Whether logging happens at all.
+plog=1
+
+# Debugmode - whether write platform debug log or not
+debugmode=1
+
+# Limiter - whether it's enabled and the rules (check the documentation for how to create rules).
+limiter=1
+limiter|*|*=allow
+
+##### Settings used by the logging daemon
+
+# Sockets that all programs connect to to receive a logging pipe.
+main_conn_sock=/var/lib/dlog-tests/main.conn
+apps_conn_sock=/var/lib/dlog-tests/apps.conn
+system_conn_sock=/var/lib/dlog-tests/system.conn
+radio_conn_sock=/var/lib/dlog-tests/radio.conn
+kmsg_conn_sock=/var/lib/dlog-tests/kmsg.conn
+syslog_conn_sock=/var/lib/dlog-tests/syslog.conn
+
+# Sockets that administrative programs connect to to manipulate the daemon.
+main_ctl_sock=/var/lib/dlog-tests/main.ctl
+apps_ctl_sock=/var/lib/dlog-tests/apps.ctl
+system_ctl_sock=/var/lib/dlog-tests/system.ctl
+radio_ctl_sock=/var/lib/dlog-tests/radio.ctl
+kmsg_ctl_sock=/var/lib/dlog-tests/kmsg.ctl
+syslog_ctl_sock=/var/lib/dlog-tests/syslog.ctl
+
+# Permissions for the relevant sockets.
+main_conn_sock_rights=0222
+apps_conn_sock_rights=0222
+system_conn_sock_rights=0222
+radio_conn_sock_rights=0222
+kmsg_conn_sock_rights=0222
+syslog_conn_sock_rights=0222
+
+main_ctl_sock_rights=0222
+apps_ctl_sock_rights=0222
+system_ctl_sock_rights=0222
+radio_ctl_sock_rights=0222
+kmsg_ctl_sock_rights=0222
+syslog_ctl_sock_rights=0222
+
+main_conn_sock_owner=log
+apps_conn_sock_owner=log
+system_conn_sock_owner=log
+radio_conn_sock_owner=log
+kmsg_conn_sock_owner=log
+syslog_conn_sock_owner=log
+
+main_ctl_sock_owner=log
+apps_ctl_sock_owner=log
+system_ctl_sock_owner=log
+radio_ctl_sock_owner=log
+kmsg_ctl_sock_owner=log
+syslog_ctl_sock_owner=log
+
+main_conn_sock_group=log
+apps_conn_sock_group=log
+system_conn_sock_group=log
+radio_conn_sock_group=log
+kmsg_conn_sock_group=log
+syslog_conn_sock_group=log
+
+main_ctl_sock_group=log
+apps_ctl_sock_group=log
+system_ctl_sock_group=log
+radio_ctl_sock_group=log
+kmsg_ctl_sock_group=log
+syslog_ctl_sock_group=log
+
+main_size=1048576
+apps_size=1048576
+system_size=1048576
+radio_size=1048576
+kmsg_size=1048576
+syslog_size=1048576
+
+util_sorting_time_window=5000
+util_sorting_buffer_size=32000
+dynamic_config_path=/tmp/dlog-filters/
+
+stdout_enable_main=1
+stdout_enable_radio=1
+stdout_enable_system=0
+stdout_enable_apps=0
diff --git a/configs/dlog-pipe.conf.stdout_enable.3 b/configs/dlog-pipe.conf.stdout_enable.3
new file mode 100644 (file)
index 0000000..ea04b0f
--- /dev/null
@@ -0,0 +1,94 @@
+# Lines starting with # are comments and are ignored.
+# Empty lines are also ignored.
+# Every other line is a config value and has to be in key=value format
+
+backend=pipe
+
+##### Settings used by the logging library
+
+# Platform logging. Whether logging happens at all.
+plog=1
+
+# Debugmode - whether write platform debug log or not
+debugmode=1
+
+# Limiter - whether it's enabled and the rules (check the documentation for how to create rules).
+limiter=1
+limiter|*|*=allow
+
+##### Settings used by the logging daemon
+
+# Sockets that all programs connect to to receive a logging pipe.
+main_conn_sock=/var/lib/dlog-tests/main.conn
+apps_conn_sock=/var/lib/dlog-tests/apps.conn
+system_conn_sock=/var/lib/dlog-tests/system.conn
+radio_conn_sock=/var/lib/dlog-tests/radio.conn
+kmsg_conn_sock=/var/lib/dlog-tests/kmsg.conn
+syslog_conn_sock=/var/lib/dlog-tests/syslog.conn
+
+# Sockets that administrative programs connect to to manipulate the daemon.
+main_ctl_sock=/var/lib/dlog-tests/main.ctl
+apps_ctl_sock=/var/lib/dlog-tests/apps.ctl
+system_ctl_sock=/var/lib/dlog-tests/system.ctl
+radio_ctl_sock=/var/lib/dlog-tests/radio.ctl
+kmsg_ctl_sock=/var/lib/dlog-tests/kmsg.ctl
+syslog_ctl_sock=/var/lib/dlog-tests/syslog.ctl
+
+# Permissions for the relevant sockets.
+main_conn_sock_rights=0222
+apps_conn_sock_rights=0222
+system_conn_sock_rights=0222
+radio_conn_sock_rights=0222
+kmsg_conn_sock_rights=0222
+syslog_conn_sock_rights=0222
+
+main_ctl_sock_rights=0222
+apps_ctl_sock_rights=0222
+system_ctl_sock_rights=0222
+radio_ctl_sock_rights=0222
+kmsg_ctl_sock_rights=0222
+syslog_ctl_sock_rights=0222
+
+main_conn_sock_owner=log
+apps_conn_sock_owner=log
+system_conn_sock_owner=log
+radio_conn_sock_owner=log
+kmsg_conn_sock_owner=log
+syslog_conn_sock_owner=log
+
+main_ctl_sock_owner=log
+apps_ctl_sock_owner=log
+system_ctl_sock_owner=log
+radio_ctl_sock_owner=log
+kmsg_ctl_sock_owner=log
+syslog_ctl_sock_owner=log
+
+main_conn_sock_group=log
+apps_conn_sock_group=log
+system_conn_sock_group=log
+radio_conn_sock_group=log
+kmsg_conn_sock_group=log
+syslog_conn_sock_group=log
+
+main_ctl_sock_group=log
+apps_ctl_sock_group=log
+system_ctl_sock_group=log
+radio_ctl_sock_group=log
+kmsg_ctl_sock_group=log
+syslog_ctl_sock_group=log
+
+main_size=1048576
+apps_size=1048576
+system_size=1048576
+radio_size=1048576
+kmsg_size=1048576
+syslog_size=1048576
+
+util_sorting_time_window=5000
+util_sorting_buffer_size=32000
+dynamic_config_path=/tmp/dlog-filters/
+
+stdout_enable_main=0
+stdout_enable_radio=0
+stdout_enable_system=1
+stdout_enable_apps=1
\ No newline at end of file
index 6d09c6a..fdacb9c 100644 (file)
@@ -316,6 +316,7 @@ chsmack -e 'System' %{_libexecdir}/dlog-log-critical
 %{_libexecdir}/libdlog/test_libredirect
 %{_libexecdir}/libdlog/test_libredirect_multi
 %{_libexecdir}/libdlog/test_filters
+%{_libexecdir}/libdlog/test_config_redirect
 %{_datadir}/dlog-pipe.conf.test
 %{_datadir}/dlog-logger.conf.test
 %{_datadir}/dlog-zero-copy.conf.test
@@ -328,6 +329,12 @@ chsmack -e 'System' %{_libexecdir}/dlog-log-critical
 %{_datadir}/dlog-logger.conf.plog
 %{_datadir}/dlog-pipe.conf.autodetect
 %{_datadir}/dlog-logger.conf.autodetect
+%{_datadir}/dlog-logger.conf.stdout_enable.1
+%{_datadir}/dlog-logger.conf.stdout_enable.2
+%{_datadir}/dlog-logger.conf.stdout_enable.3
+%{_datadir}/dlog-pipe.conf.stdout_enable.1
+%{_datadir}/dlog-pipe.conf.stdout_enable.2
+%{_datadir}/dlog-pipe.conf.stdout_enable.3
 %{_sysconfdir}/dlog.conf.d/99-dlog-logger.disable-platform-logging-for-testsuite.conf
 %dir %attr(755,log,log) /var/lib/dlog-tests
 %dir %attr(755,log,log) /usr/share/dlog-filters.conf.test
index 6aa6729..714b56d 100644 (file)
 #include <buffer_traits.h>
 #include <logcommon.h>
 #include <zero_copy_backend.h>
+#include <logconfig.h>
 
 #include "internal.h"
 
+static bool config_allows_redirection(log_id_t buffer)
+{
+       struct log_config conf = {};
+       if (log_config_read(&conf) < 0)
+               return false;
+
+       char key[MAX_CONF_KEY_LEN];
+       snprintf(key, sizeof key, "stdout_enable_%s", log_name_by_id(buffer));
+
+       bool ret = log_config_get_boolean(&conf, key, true);
+       log_config_free(&conf);
+       return ret;
+}
+
 EXPORT_API int dlog_connect_fd(int buffer, int fileno, const char *tag, int prio)
 {
        if (!is_buffer_valid(buffer))
@@ -63,6 +78,9 @@ EXPORT_API int dlog_connect_fd(int buffer, int fileno, const char *tag, int prio
        else if (fileno == STDERR_FILENO)
                setenv(DLOG_ENV_STDERR_LINE_BUFFERED, "1", 1);
 
+       if (!config_allows_redirection((log_id_t)buffer))
+               return -EOPNOTSUPP;
+
        return 0;
 }
 
index 347e2d8..52c0b63 100644 (file)
@@ -1183,6 +1183,23 @@ dlogsend -b main -t DLOG_TS_TEST "The future is now, old man"
 LOG_DETAILS="testing if the timestamps are correct"
 test_verifytime && ok || fail
 
+DLOG_PREV_PATH=$DLOG_CONFIG_PATH
+
+LOG_DETAILS="testing if config has control over redirecting to stdout (1/3)"
+DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.stdout_enable.1"
+
+test_config_redirect 1 1 1 1 && ok || fail
+
+LOG_DETAILS="testing if config has control over redirecting to stdout (2/3)"
+DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.stdout_enable.2"
+test_config_redirect 0 0 1 1 && ok || fail
+
+LOG_DETAILS="testing if config has control over redirecting to stdout (3/3)"
+DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.stdout_enable.3"
+test_config_redirect 1 1 0 0 && ok || fail
+
+DLOG_CONFIG_PATH=$DLOG_PREV_PATH
+
 if [ "$quick" -ne 1 ]; then
        if [ "$type" != "zero-copy" ]; then # Backend autodection and logger daemon stuff don't really work on zero-copy
                PREAUTODETECT_CONFIG_PATH=$DLOG_CONFIG_PATH
diff --git a/tests/test_config_redirect.c b/tests/test_config_redirect.c
new file mode 100644 (file)
index 0000000..2b74f5a
--- /dev/null
@@ -0,0 +1,53 @@
+/* DLOG
+ * Copyright (c) 2023 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <unistd.h>
+#include <string.h>
+
+#include <dlog-redirect-stdout.h>
+#include <dlog.h>
+
+int main(int argc, char** argv)
+{
+       int log_count = argc - 1;
+       bool* expected_results = (bool*)malloc(sizeof(bool) * (log_count));
+
+       for (int i = 1; i < argc; i++) {
+               if (strcmp(argv[i], "1") == 0) {
+                       expected_results[i - 1] = true;
+               } else if (strcmp(argv[i], "0") == 0) {
+                       expected_results[i - 1] = false;
+               } else {
+                       free(expected_results);
+                       return EXIT_FAILURE;
+               }
+       }
+
+       for (int i = 0; i < log_count; i++) {
+               int result = dlog_connect_fd(i, STDOUT_FILENO, "tag", DLOG_INFO);
+               bool is_eopnotsupp = (result == -EOPNOTSUPP);
+               if (is_eopnotsupp != expected_results[i]) {
+                       free(expected_results);
+                       return EXIT_FAILURE;
+               }
+       }
+
+       free(expected_results);
+       return EXIT_SUCCESS;
+}
\ No newline at end of file