Skip to content

Commit ad46faf

Browse files
Paulo Alcantarasmfrench
authored andcommitted
smb: client: fix DFS mount against old servers with NTLMSSP
Old Windows servers will return not fully qualified DFS targets by default as specified in MS-DFSC 3.2.5.5 Receiving a Root Referral Request or Link Referral Request | Servers SHOULD<30> return fully qualified DNS host names of | targets in responses to root referral requests and link referral | requests. | ... | <30> Section 3.2.5.5: By default, Windows Server 2003, Windows | Server 2008, Windows Server 2008 R2, Windows Server 2012, and | Windows Server 2012 R2 return DNS host names that are not fully | qualified for targets. Fix this by converting all NetBIOS host names from DFS targets to FQDNs and try resolving them first if DNS domain name was provided in NTLMSSP CHALLENGE_MESSAGE message from previous SMB2_SESSION_SETUP. This also prevents the client from translating the DFS target hostnames to another domain depending on the network domain search order. Signed-off-by: Paulo Alcantara (Red Hat) <pc@manguebit.com> Signed-off-by: Steve French <stfrench@microsoft.com>
1 parent 0e8ae9b commit ad46faf

9 files changed

Lines changed: 105 additions & 50 deletions

File tree

fs/smb/client/cifsglob.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,7 @@ struct TCP_Server_Info {
828828
*/
829829
char *leaf_fullpath;
830830
bool dfs_conn:1;
831+
char dns_dom[CIFS_MAX_DOMAINNAME_LEN + 1];
831832
};
832833

833834
static inline bool is_smb1(struct TCP_Server_Info *server)
@@ -2312,4 +2313,24 @@ static inline bool cifs_ses_exiting(struct cifs_ses *ses)
23122313
return ret;
23132314
}
23142315

2316+
static inline bool cifs_netbios_name(const char *name, size_t namelen)
2317+
{
2318+
bool ret = false;
2319+
size_t i;
2320+
2321+
if (namelen >= 1 && namelen <= RFC1001_NAME_LEN) {
2322+
for (i = 0; i < namelen; i++) {
2323+
const unsigned char c = name[i];
2324+
2325+
if (c == '\\' || c == '/' || c == ':' || c == '*' ||
2326+
c == '?' || c == '"' || c == '<' || c == '>' ||
2327+
c == '|' || c == '.')
2328+
return false;
2329+
if (!ret && isalpha(c))
2330+
ret = true;
2331+
}
2332+
}
2333+
return ret;
2334+
}
2335+
23152336
#endif /* _CIFS_GLOB_H */

fs/smb/client/connect.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ static int reconn_set_ipaddr_from_hostname(struct TCP_Server_Info *server)
9797
ss = server->dstaddr;
9898
spin_unlock(&server->srv_lock);
9999

100-
rc = dns_resolve_server_name_to_ip(unc, (struct sockaddr *)&ss, NULL);
100+
rc = dns_resolve_server_name_to_ip(server->dns_dom, unc,
101+
(struct sockaddr *)&ss, NULL);
101102
kfree(unc);
102103

103104
if (rc < 0) {
@@ -1710,6 +1711,8 @@ cifs_get_tcp_session(struct smb3_fs_context *ctx,
17101711
goto out_err;
17111712
}
17121713
}
1714+
if (ctx->dns_dom)
1715+
strscpy(tcp_ses->dns_dom, ctx->dns_dom);
17131716

17141717
if (ctx->nosharesock)
17151718
tcp_ses->nosharesock = true;

fs/smb/client/dfs.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include "fs_context.h"
1010
#include "dfs.h"
1111

12+
#define DFS_DOM(ctx) (ctx->dfs_root_ses ? ctx->dfs_root_ses->dns_dom : NULL)
13+
1214
/**
1315
* dfs_parse_target_referral - set fs context for dfs target referral
1416
*
@@ -46,8 +48,9 @@ int dfs_parse_target_referral(const char *full_path, const struct dfs_info3_para
4648
if (rc)
4749
goto out;
4850

49-
rc = dns_resolve_server_name_to_ip(path, (struct sockaddr *)&ctx->dstaddr, NULL);
50-
51+
rc = dns_resolve_server_name_to_ip(DFS_DOM(ctx), path,
52+
(struct sockaddr *)&ctx->dstaddr,
53+
NULL);
5154
out:
5255
kfree(path);
5356
return rc;
@@ -59,8 +62,9 @@ static int get_session(struct cifs_mount_ctx *mnt_ctx, const char *full_path)
5962
int rc;
6063

6164
ctx->leaf_fullpath = (char *)full_path;
65+
ctx->dns_dom = DFS_DOM(ctx);
6266
rc = cifs_mount_get_session(mnt_ctx);
63-
ctx->leaf_fullpath = NULL;
67+
ctx->leaf_fullpath = ctx->dns_dom = NULL;
6468

6569
return rc;
6670
}
@@ -264,7 +268,8 @@ static int update_fs_context_dstaddr(struct smb3_fs_context *ctx)
264268
int rc = 0;
265269

266270
if (!ctx->nodfs && ctx->dfs_automount) {
267-
rc = dns_resolve_server_name_to_ip(ctx->source, addr, NULL);
271+
rc = dns_resolve_server_name_to_ip(NULL, ctx->source,
272+
addr, NULL);
268273
if (!rc)
269274
cifs_set_port(addr, ctx->port);
270275
ctx->dfs_automount = false;

fs/smb/client/dfs_cache.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1114,7 +1114,8 @@ static bool target_share_equal(struct cifs_tcon *tcon, const char *s1)
11141114
extract_unc_hostname(s1, &host, &hostlen);
11151115
scnprintf(unc, sizeof(unc), "\\\\%.*s", (int)hostlen, host);
11161116

1117-
rc = dns_resolve_server_name_to_ip(unc, (struct sockaddr *)&ss, NULL);
1117+
rc = dns_resolve_server_name_to_ip(server->dns_dom, unc,
1118+
(struct sockaddr *)&ss, NULL);
11181119
if (rc < 0) {
11191120
cifs_dbg(FYI, "%s: could not resolve %.*s. assuming server address matches.\n",
11201121
__func__, (int)hostlen, host);

fs/smb/client/dns_resolve.c

Lines changed: 60 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,69 +20,87 @@
2020
#include "cifsproto.h"
2121
#include "cifs_debug.h"
2222

23+
static int resolve_name(const char *name, size_t namelen,
24+
struct sockaddr *addr, time64_t *expiry)
25+
{
26+
char *ip;
27+
int rc;
28+
29+
rc = dns_query(current->nsproxy->net_ns, NULL, name,
30+
namelen, NULL, &ip, expiry, false);
31+
if (rc < 0) {
32+
cifs_dbg(FYI, "%s: unable to resolve: %*.*s\n",
33+
__func__, (int)namelen, (int)namelen, name);
34+
} else {
35+
cifs_dbg(FYI, "%s: resolved: %*.*s to %s expiry %llu\n",
36+
__func__, (int)namelen, (int)namelen, name, ip,
37+
expiry ? (*expiry) : 0);
38+
39+
rc = cifs_convert_address(addr, ip, strlen(ip));
40+
kfree(ip);
41+
if (!rc) {
42+
cifs_dbg(FYI, "%s: unable to determine ip address\n",
43+
__func__);
44+
rc = -EHOSTUNREACH;
45+
} else {
46+
rc = 0;
47+
}
48+
}
49+
return rc;
50+
}
51+
2352
/**
2453
* dns_resolve_server_name_to_ip - Resolve UNC server name to ip address.
54+
* @dom: optional DNS domain name
2555
* @unc: UNC path specifying the server (with '/' as delimiter)
2656
* @ip_addr: Where to return the IP address.
2757
* @expiry: Where to return the expiry time for the dns record.
2858
*
2959
* Returns zero success, -ve on error.
3060
*/
31-
int
32-
dns_resolve_server_name_to_ip(const char *unc, struct sockaddr *ip_addr, time64_t *expiry)
61+
int dns_resolve_server_name_to_ip(const char *dom, const char *unc,
62+
struct sockaddr *ip_addr, time64_t *expiry)
3363
{
34-
const char *hostname, *sep;
35-
char *ip;
36-
int len, rc;
64+
const char *name;
65+
size_t namelen, len;
66+
char *s;
67+
int rc;
3768

3869
if (!ip_addr || !unc)
3970
return -EINVAL;
4071

41-
len = strlen(unc);
42-
if (len < 3) {
43-
cifs_dbg(FYI, "%s: unc is too short: %s\n", __func__, unc);
72+
cifs_dbg(FYI, "%s: dom=%s unc=%s\n", __func__, dom, unc);
73+
if (strlen(unc) < 3)
4474
return -EINVAL;
45-
}
46-
47-
/* Discount leading slashes for cifs */
48-
len -= 2;
49-
hostname = unc + 2;
5075

51-
/* Search for server name delimiter */
52-
sep = memchr(hostname, '/', len);
53-
if (sep)
54-
len = sep - hostname;
55-
else
56-
cifs_dbg(FYI, "%s: probably server name is whole unc: %s\n",
57-
__func__, unc);
76+
extract_unc_hostname(unc, &name, &namelen);
77+
if (!namelen)
78+
return -EINVAL;
5879

80+
cifs_dbg(FYI, "%s: hostname=%.*s\n", __func__, (int)namelen, name);
5981
/* Try to interpret hostname as an IPv4 or IPv6 address */
60-
rc = cifs_convert_address(ip_addr, hostname, len);
82+
rc = cifs_convert_address(ip_addr, name, namelen);
6183
if (rc > 0) {
62-
cifs_dbg(FYI, "%s: unc is IP, skipping dns upcall: %*.*s\n", __func__, len, len,
63-
hostname);
84+
cifs_dbg(FYI, "%s: unc is IP, skipping dns upcall: %*.*s\n",
85+
__func__, (int)namelen, (int)namelen, name);
6486
return 0;
6587
}
6688

67-
/* Perform the upcall */
68-
rc = dns_query(current->nsproxy->net_ns, NULL, hostname, len,
69-
NULL, &ip, expiry, false);
70-
if (rc < 0) {
71-
cifs_dbg(FYI, "%s: unable to resolve: %*.*s\n",
72-
__func__, len, len, hostname);
73-
} else {
74-
cifs_dbg(FYI, "%s: resolved: %*.*s to %s expiry %llu\n",
75-
__func__, len, len, hostname, ip,
76-
expiry ? (*expiry) : 0);
77-
78-
rc = cifs_convert_address(ip_addr, ip, strlen(ip));
79-
kfree(ip);
89+
/*
90+
* If @name contains a NetBIOS name and @dom has been specified, then
91+
* convert @name to an FQDN and try resolving it first.
92+
*/
93+
if (dom && *dom && cifs_netbios_name(name, namelen)) {
94+
len = strnlen(dom, CIFS_MAX_DOMAINNAME_LEN) + namelen + 2;
95+
s = kmalloc(len, GFP_KERNEL);
96+
if (!s)
97+
return -ENOMEM;
8098

81-
if (!rc) {
82-
cifs_dbg(FYI, "%s: unable to determine ip address\n", __func__);
83-
rc = -EHOSTUNREACH;
84-
} else
85-
rc = 0;
99+
scnprintf(s, len, "%.*s.%s", (int)namelen, name, dom);
100+
rc = resolve_name(s, len - 1, ip_addr, expiry);
101+
kfree(s);
102+
if (!rc)
103+
return 0;
86104
}
87-
return rc;
105+
return resolve_name(name, namelen, ip_addr, expiry);
88106
}

fs/smb/client/dns_resolve.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
#include <linux/net.h>
1515

1616
#ifdef __KERNEL__
17-
int dns_resolve_server_name_to_ip(const char *unc, struct sockaddr *ip_addr, time64_t *expiry);
17+
int dns_resolve_server_name_to_ip(const char *dom, const char *unc,
18+
struct sockaddr *ip_addr, time64_t *expiry);
1819
#endif /* KERNEL */
1920

2021
#endif /* _DNS_RESOLVE_H */

fs/smb/client/fs_context.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ smb3_fs_context_dup(struct smb3_fs_context *new_ctx, struct smb3_fs_context *ctx
385385
new_ctx->source = NULL;
386386
new_ctx->iocharset = NULL;
387387
new_ctx->leaf_fullpath = NULL;
388+
new_ctx->dns_dom = NULL;
388389
/*
389390
* Make sure to stay in sync with smb3_cleanup_fs_context_contents()
390391
*/
@@ -399,6 +400,7 @@ smb3_fs_context_dup(struct smb3_fs_context *new_ctx, struct smb3_fs_context *ctx
399400
DUP_CTX_STR(nodename);
400401
DUP_CTX_STR(iocharset);
401402
DUP_CTX_STR(leaf_fullpath);
403+
DUP_CTX_STR(dns_dom);
402404

403405
return 0;
404406
}
@@ -1863,6 +1865,8 @@ smb3_cleanup_fs_context_contents(struct smb3_fs_context *ctx)
18631865
ctx->prepath = NULL;
18641866
kfree(ctx->leaf_fullpath);
18651867
ctx->leaf_fullpath = NULL;
1868+
kfree(ctx->dns_dom);
1869+
ctx->dns_dom = NULL;
18661870
}
18671871

18681872
void

fs/smb/client/fs_context.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ struct smb3_fs_context {
295295
bool dfs_automount:1; /* set for dfs automount only */
296296
enum cifs_reparse_type reparse_type;
297297
bool dfs_conn:1; /* set for dfs mounts */
298+
char *dns_dom;
298299
};
299300

300301
extern const struct fs_parameter_spec smb3_fs_parameters[];

fs/smb/client/misc.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,8 @@ int match_target_ip(struct TCP_Server_Info *server,
11891189

11901190
cifs_dbg(FYI, "%s: target name: %s\n", __func__, target + 2);
11911191

1192-
rc = dns_resolve_server_name_to_ip(target, (struct sockaddr *)&ss, NULL);
1192+
rc = dns_resolve_server_name_to_ip(server->dns_dom, target,
1193+
(struct sockaddr *)&ss, NULL);
11931194
kfree(target);
11941195

11951196
if (rc < 0)

0 commit comments

Comments
 (0)