From c3e16c2c49161bfa537bb1004eb4ad754c23f1d0 Mon Sep 17 00:00:00 2001
From: Marios Makassikis <mmakassikis@freebox.fr>
Date: Mon, 7 Nov 2022 14:05:45 +0100
Subject: [PATCH 16/17] ksmbd-tools: mountd: set larger maximum size for domain
 string

Currently, domain_str in struct lsarpc_names_info has a fixed size of
NAME_MAX (255 bytes). This is insufficient, as domain_str can be set
from a SID, whose string representation is 256 bytes per Microsoft docs.

Adjust domain_str size to avoid truncation and failure in this case.

Signed-off-by: Marios Makassikis <mmakassikis@freebox.fr>
---
 include/rpc_lsarpc.h | 3 ++-
 mountd/smbacl.c      | 5 +++--
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/include/rpc_lsarpc.h b/include/rpc_lsarpc.h
index 8ce56c7680e4..d947d8bc458e 100644
--- a/include/rpc_lsarpc.h
+++ b/include/rpc_lsarpc.h
@@ -11,6 +11,7 @@
 #include <smbacl.h>
 
 #define HANDLE_SIZE	20
+#define DOMAIN_STR_SIZE	257
 
 struct ksmbd_rpc_command;
 struct ksmbd_rpc_pipe;
@@ -23,7 +24,7 @@ struct policy_handle {
 struct lsarpc_names_info {
 	unsigned int index;
 	int type;
-	char domain_str[NAME_MAX];
+	char domain_str[DOMAIN_STR_SIZE];
 	struct smb_sid sid;
 	struct ksmbd_user *user;
 };
diff --git a/mountd/smbacl.c b/mountd/smbacl.c
index d23deadd58cf..e71e64b3b627 100644
--- a/mountd/smbacl.c
+++ b/mountd/smbacl.c
@@ -9,6 +9,7 @@
 #include <smbacl.h>
 #include <tools.h>
 #include <glib.h>
+#include <rpc_lsarpc.h>
 
 static const struct smb_sid sid_domain = {1, 1, {0, 0, 0, 0, 0, 5},
 	{21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} };
@@ -162,13 +163,13 @@ int set_domain_name(struct smb_sid *sid, char *domain, size_t domain_len,
 		    int *type)
 {
 	int ret = 0;
-	char domain_string[NAME_MAX] = {0};
+	char domain_string[DOMAIN_STR_SIZE] = {0};
 	g_autofree char *domain_name = NULL;
 
 	if (!smb_compare_sids(sid, &sid_domain) &&
 	    !memcmp(&sid->sub_auth[1], global_conf.gen_subauth,
 		    sizeof(__u32) * 3)) {
-		if (gethostname(domain_string, NAME_MAX))
+		if (gethostname(domain_string, DOMAIN_STR_SIZE))
 			return -ENOMEM;
 
 		domain_name = g_ascii_strup(domain_string, -1);
-- 
2.25.1

