#include <common/time.h>
#include <common/utils.h>
#include <common/uuid.h>
+#include <common/compat/path.h>
#include <urcu/rculist.h>
#include <sys/stat.h>
return ret;
}
+/*
+ * Check if a name is safe to use in a path.
+ *
+ * A name that is deemed "path-safe":
+ * - Does not contains a path separator (/ or \, platform dependant),
+ * - Does not start with a '.' (hidden file/folder),
+ * - Is not empty.
+ */
+static bool is_name_path_safe(const char *name)
+{
+ const size_t name_len = strlen(name);
+
+ /* Not empty. */
+ if (name_len == 0) {
+ WARN("An empty name is not allowed to be used in a path");
+ return false;
+ }
+ /* Does not start with '.'. */
+ if (name[0] == '.') {
+ WARN("Name \"%s\" is not allowed to be used in a path since it starts with '.'", name);
+ return false;
+ }
+ /* Does not contain a path-separator. */
+ if (strchr(name, LTTNG_PATH_SEPARATOR)) {
+ WARN("Name \"%s\" is not allowed to be used in a path since it contains a path separator", name);
+ return false;
+ }
+
+ return true;
+}
+
/*
* Create a new session by assigning a new session ID.
*
assert(hostname);
assert(base_path);
- if (strstr(session_name, ".")) {
- ERR("Illegal character in session name: \"%s\"",
- session_name);
+ if (!is_name_path_safe(session_name)) {
+ ERR("Refusing to create session as the provided session name is not path-safe");
+ goto error;
+ }
+ if (!is_name_path_safe(hostname)) {
+ ERR("Refusing to create session as the provided hostname is not path-safe");
goto error;
}
if (strstr(base_path, "../")) {
base_path);
goto error;
}
- if (strstr(hostname, ".")) {
- ERR("Invalid character in hostname: \"%s\"",
- hostname);
- goto error;
- }
session = zmalloc(sizeof(*session));
if (!session) {
--- /dev/null
+/*
+ * Copyright (C) 2020 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License, version 2 only,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+/* Build platform's preferred path separator. */
+#if defined(_WIN32) || defined(__CYGWIN__)
+#define LTTNG_PATH_SEPARATOR '\\'
+#else
+#define LTTNG_PATH_SEPARATOR '/'
+#endif