From 3e174337abe8072d40c06fb168f544c0ed73c5d7 Mon Sep 17 00:00:00 2001 From: apio Date: Thu, 24 Aug 2023 15:28:08 +0200 Subject: [PATCH] init: Hostname validation --- apps/init.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/apps/init.cpp b/apps/init.cpp index 9b1f9178..2a16dea5 100644 --- a/apps/init.cpp +++ b/apps/init.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -300,6 +301,19 @@ static Result set_hostname() auto hostname = TRY(file->read_line()); hostname.trim("\n"); + if (hostname.is_empty()) + { + do_log("[init] /etc/hostname is empty or invalid, keeping the default hostname\n"); + return {}; + } + + Utf8StringDecoder decoder(hostname.chars()); + if (decoder.code_points().has_error()) + { + do_log("[init] /etc/hostname is not valid UTF-8, keeping the default hostname\n"); + return {}; + } + if (sethostname(hostname.chars(), hostname.length()) < 0) return {}; do_log("[init] successfully set system hostname to '%s'\n", hostname.chars());