Skip to contentSkip to conversation

mountd: needless DNS queries when authenticating client against numeric IP

PATCH New Denis Vlasenko 3 participants 8 messages Mar 4, 2005 – Mar 10, 2005

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

mount 127.0.0.1:/ can fail if DNS is down
and mountd has been restarted without re-run of exportfs -r, because /var/lib/nfs/etab contains wrong hostname ("localhost" instead of "127.0.0.1") and mountd cannot determine that these are the same. This was explained in my previous mail.

Here is another, lesser problem:

If mountd was restarted _with_ prior run of exportfs -r, etab is correct and mount succeeds, but with ~10 sec delay because of:

nfs_export *
auth_authenticate(char *what, struct sockaddr_in *caller, char *path)

{
        nfs_export      *exp = NULL;
        char            epath[MAXPATHLEN+1];
        char            *p = NULL;
        struct hostent  *hp = NULL;
        struct in_addr  addr = caller->sin_addr;
        enum auth_error error;

        if (path [0] != '/') {
                xlog(L_WARNING, "bad path in %s request from %s: \"%s\"",
                     what, inet_ntoa(addr), path);
                return exp;
        }

        strncpy(epath, path, sizeof (epath) - 1);
        epath[sizeof (epath) - 1] = '\0';
        auth_fixpath(epath); /* strip duplicate '/' etc */

xlog(L_ERROR, "auth_authenticate(): get_reliable_hostbyaddr start"); //vda:
===> hp = get_reliable_hostbyaddr((const char*)&caller->sin_addr, sizeof(struct in_addr),

                                     AF_INET);
        ^^^^^^^^^^^^^^^^^ gethostbyaddr inside. we will wait for DNS reply (or timeout)
        if (!hp)
                hp = get_hostent((const char*)&caller->sin_addr, sizeof(struct in_addr),
                                     AF_INET);
        if (!hp)
                return exp;

This DNS query is not needed in my case.

Shall we first try to authenticate against numeric IP of client and retry with hostname returned from get_reliable_hostbyaddr() only if we fail auth against numeric IP?
--
vda

-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
_______________________________________________
NFS maillist - [email protected]
https://lists.sourceforge.net/lists/listinfo/nfs

#1 lore raw
Quoted 10 lines · Denis Vlasenko

mount 127.0.0.1:/ can fail if DNS is down
and mountd has been restarted without re-run of exportfs -r, because /var/lib/nfs/etab contains wrong hostname ("localhost" instead of "127.0.0.1") and mountd cannot determine that these are the same. This was explained in my previous mail.

Here is another, lesser problem:

If mountd was restarted _with_ prior run of exportfs -r, etab is correct and mount succeeds, but with ~10 sec delay because of DNS timeout

[snip]

I've cooked up a patch.
Now my mount 127.0.0.1:/ /mnt/tmp succeeds instantly, regardless of whether 127.0.0.1 resolves to 'localhost' or not.

This is accomplished by first trying to auth against numeric IP, and only if that fails, we resolve IP into name and try again.

Please comment/apply.
--
vda

utils/mountd/auth.c renamed from utils/mountd/auth.c.orig +38 -28
@@ -128,34 +128,14 @@ auth_authenticate_internal(char *what, s
 	return exp;
 }
 
-nfs_export *
-auth_authenticate(char *what, struct sockaddr_in *caller, char *path)
+/* NB: frees hp */
+static nfs_export *
+auth_authenticate_helper(char *what, struct sockaddr_in *caller, char *path, char *epath, struct hostent *hp)
 {
-	nfs_export	*exp = NULL;
-	char		epath[MAXPATHLEN+1];
-	char		*p = NULL;
-	struct hostent	*hp = NULL;
-	struct in_addr	addr = caller->sin_addr;
+	char *p = NULL;
+	nfs_export *exp;
 	enum auth_error	error;
 
-	if (path [0] != '/') {
-		xlog(L_WARNING, "bad path in %s request from %s: \"%s\"",
-		     what, inet_ntoa(addr), path);
-		return exp;
-	}
-
-	strncpy(epath, path, sizeof (epath) - 1);
-	epath[sizeof (epath) - 1] = '\0';
-	auth_fixpath(epath); /* strip duplicate '/' etc */
-
-	hp = get_reliable_hostbyaddr((const char*)&caller->sin_addr, sizeof(struct in_addr),
-				     AF_INET);
-	if (!hp)
-		hp = get_hostent((const char*)&caller->sin_addr, sizeof(struct in_addr),
-				     AF_INET);
-	if (!hp)
-		return exp;
-
 	/* Try the longest matching exported pathname. */
 	while (1) {
 		exp = auth_authenticate_internal(what, caller, epath,
@@ -172,12 +152,12 @@ auth_authenticate(char *what, struct soc
 	switch (error) {
 	case bad_path:
 		xlog(L_WARNING, "bad path in %s request from %s: \"%s\"",
-		     what, inet_ntoa(addr), path);
+		     what, inet_ntoa(caller->sin_addr), path);
 		break;
 
 	case unknown_host:
 		xlog(L_WARNING, "%s request from unknown host %s for %s (%s)",
-		     what, inet_ntoa(addr), path, epath);
+		     what, inet_ntoa(caller->sin_addr), path, epath);
 		break;
 
 	case no_entry:
@@ -203,10 +183,40 @@ auth_authenticate(char *what, struct soc
 		xlog(L_NOTICE, "%s request from %s:%d for %s (%s) gave %d",
 		     what, hp->h_name, ntohs(caller->sin_port), path, epath, error);
 	}
+	free (hp);
+	return exp;
+}
+
+nfs_export *
+auth_authenticate(char *what, struct sockaddr_in *caller, char *path)
+{
+	nfs_export	*exp = NULL;
+	char		epath[MAXPATHLEN+1];
+	struct hostent	*hp;
+
+	if (path [0] != '/') {
+		xlog(L_WARNING, "bad path in %s request from %s: \"%s\"",
+		     what, inet_ntoa(caller->sin_addr), path);
+		return NULL;
+	}
 
+	strncpy(epath, path, sizeof (epath) - 1);
+	epath[sizeof (epath) - 1] = '\0';
+	auth_fixpath(epath); /* strip duplicate '/' etc */
+
+	/* try to auth against address */
+	hp = get_hostent((const char*)&caller->sin_addr,
+					sizeof(struct in_addr), AF_INET);
 	if (hp)
-		free (hp);
+		exp = auth_authenticate_helper(what, caller, path, epath, hp);
 
+	/* Retry with resolved name if unsuccessful */
+	if (!exp) {
+		hp = get_reliable_hostbyaddr((const char*)&caller->sin_addr,
+					sizeof(struct in_addr), AF_INET);
+		if (hp)
+			exp = auth_authenticate_helper(what, caller, path, epath, hp);
+	}
 	return exp;
 }
 
#2 lore raw
Quoted 13 lines · On Saturday March 5, [email protected] wrote:
Quoted 10 lines · Denis Vlasenko

mount 127.0.0.1:/ can fail if DNS is down
and mountd has been restarted without re-run of exportfs -r, because /var/lib/nfs/etab contains wrong hostname ("localhost" instead of "127.0.0.1") and mountd cannot determine that these are the same. This was explained in my previous mail.

Here is another, lesser problem:

If mountd was restarted _with_ prior run of exportfs -r, etab is correct and mount succeeds, but with ~10 sec delay because of DNS timeout

[snip]

Quoted 8 lines

I've cooked up a patch.
Now my mount 127.0.0.1:/ /mnt/tmp succeeds instantly, regardless of whether 127.0.0.1 resolves to 'localhost' or not.

This is accomplished by first trying to auth against numeric IP, and only if that fails, we resolve IP into name and try again.

Please comment/apply.

sorry, but this has been tried before, and it doesn't work.

From the ChangeLog

2001-09-12 NeilBrown <[email protected]>

* utils/mountd/auth.c (auth_authenticate_internal): Reverse change from 2000-08-02: It causes problems if someone exports to both a hostname and IP addresses. nfs-utils must be consistant about the canonical name that it chooses.
...
2000-08-02 H.J. Lu <[email protected]>

* utils/mountd/auth.c (auth_authenticate_internal): Try to avoid the reverse name lookup.

It is only safe to avoid the DNS lookup if there are *no* names in the /etc/exports file. If everything is one of "*", "ip.ad.dr.es" or "ne.t-.wo.rk/mask" then it is OK, If there is any domain.name or @netgroup, then you always need to find the name.
The problem arises if someone exports one filesystem to an IP address, and another to the DNS name. Confusion and failure results.

A patch which checked is this was the case, and always avoided DNS lookup if it was would be seriously considered for acceptance.

Thanks,
NeilBrown

-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
_______________________________________________
NFS maillist - [email protected]
https://lists.sourceforge.net/lists/listinfo/nfs

#3 lore raw

PATCH 1/1 treat N.N.N.N as MCL_SUBNETWORK instead of MCL_FQDN

Meanwhile, this patch prevents exportfs from translating numeric IPs into hostnames for /var/lib/nfs/etab. Adheres to principle of least surprise. Avoids DNS lookups. --
vda

support/export/client.c renamed from support/export/client.c.orig +10 -1
@@ -138,7 +138,9 @@ client_init(nfs_client *clp, const char 
 
 	if (clp->m_type == MCL_SUBNETWORK) {
 		char	*cp = strchr(clp->m_hostname, '/');
+		static char slash32[] = "/32";
 
+		if(!cp) cp = slash32;
 		*cp = '\0';
 		clp->m_addrlist[0].s_addr = inet_addr(clp->m_hostname);
 		if (strchr(cp + 1, '.')) {
@@ -443,5 +445,12 @@ client_gettype(char *ident)
 		if (*sp == '\\' && sp[1])
 			sp++;
 	}
-	return MCL_FQDN;
+	/* check for N.N.N.N */
+	sp = ident;
+	if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
+	sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
+	sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
+	sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '\0') return MCL_FQDN;
+	/* we lie here a bit. but technically N.N.N.N == N.N.N.N/32 :) */
+	return MCL_SUBNETWORK;
 }
#4 lore raw
In reply to Neil Brown
Quoted 34 lines
Quoted 12 lines
Quoted 10 lines

mount 127.0.0.1:/ can fail if DNS is down
and mountd has been restarted without re-run of exportfs -r, because /var/lib/nfs/etab contains wrong hostname ("localhost" instead of "127.0.0.1") and mountd cannot determine that these are the same. This was explained in my previous mail.

Here is another, lesser problem:

If mountd was restarted _with_ prior run of exportfs -r, etab is correct and mount succeeds, but with ~10 sec delay because of DNS timeout

[snip]

Quoted 8 lines

I've cooked up a patch.
Now my mount 127.0.0.1:/ /mnt/tmp succeeds instantly, regardless of whether 127.0.0.1 resolves to 'localhost' or not.

This is accomplished by first trying to auth against numeric IP, and only if that fails, we resolve IP into name and try again.

Please comment/apply.

sorry, but this has been tried before, and it doesn't work.

From the ChangeLog

2001-09-12 NeilBrown <[email protected]>

* utils/mountd/auth.c (auth_authenticate_internal): Reverse change from 2000-08-02: It causes problems if someone exports to both a hostname and IP addresses. nfs-utils must be consistant about the canonical name that it chooses.
...
2000-08-02 H.J. Lu <[email protected]>

Quoted 7 lines

* utils/mountd/auth.c (auth_authenticate_internal): Try to avoid the reverse name lookup.

It is only safe to avoid the DNS lookup if there are *no* names in the /etc/exports file. If everything is one of "*", "ip.ad.dr.es" or "ne.t-.wo.rk/mask" then it is OK, If there is any domain.name or @netgroup, then you always need to find the name.

What about '*.domain.name' style wildcards?

Quoted 2 lines

The problem arises if someone exports one filesystem to an IP address, and another to the DNS name. Confusion and failure results.

Like this? /etc/exports:

/home 1.2.3.4(rw)
/public joker(rw)

What is the failure scenario? I don't quite understand where is the problem. --
vda

-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
_______________________________________________
NFS maillist - [email protected]
https://lists.sourceforge.net/lists/listinfo/nfs

#5 lore raw

On Wed, Mar 09, 2005 at 10:49:13AM +0200, Denis Vlasenko wrote:
[snip]

Quoted 9 lines
Quoted 2 lines

The problem arises if someone exports one filesystem to an IP address, and another to the DNS name. Confusion and failure results.

Like this? /etc/exports:

/home 1.2.3.4(rw)
/public joker(rw)

What is the failure scenario? I don't quite understand where is the problem.

/home 1.2.3.0/24(ro)
/home joker(rw)

If Joker -> 1.2.3.4 then you need to do the lookup because joker is more specific than 1.2.3.0/24.

You could get around this by converting host names to IPs at export time.

The precedence of exports is important as well. To do the least amount of look up you need to make it:
IP address
hostname (or get rid of these and only use IP addresses) networks
wildcards

Now if you find and IP you are done. If there are not hostnames and you find a network you are done. Otherwise you do the DNS lookup and look for wildcards.

Allen Morris

-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
_______________________________________________
NFS maillist - [email protected]
https://lists.sourceforge.net/lists/listinfo/nfs

#6 lore raw
In reply to Denis Vlasenko
Quoted 8 lines · On Wednesday March 9, [email protected] wrote:
Quoted 5 lines

It is only safe to avoid the DNS lookup if there are *no* names in the /etc/exports file. If everything is one of "*", "ip.ad.dr.es" or "ne.t-.wo.rk/mask" then it is OK, If there is any domain.name or @netgroup, then you always need to find the name.

What about '*.domain.name' style wildcards?

Yep, Always need DNS lookup if there are any of those too.

Quoted 9 lines
Quoted 2 lines

The problem arises if someone exports one filesystem to an IP address, and another to the DNS name. Confusion and failure results.

Like this? /etc/exports:

/home 1.2.3.4(rw)
/public joker(rw)

What is the failure scenario? I don't quite understand where is the problem.

Suppose joker is 1.2.3.4

joker mounts '/home'. Mountd doesn't bother with the DNS lookup and tell the kernel:

1/ IP address "1.2.3.4" has name "1.2.3.4"
2/ client with name "1.2.3.4" is allowed to access /home with
options "rw"

Then joker mounts '/public' so mountd need to do the DNS lookup, and tells the kernel:

1/ IP address "1.2.3.4" has name "joker"
2/ client with name "joker" is allowed to access /public
with options "rw".

Now joker tries to access it's mount of /home, and this fails, because 1.2.3.4 maps to "joker", and "joker" doesn't have access to /home

For this reason, there must be a canonical name for each IP address. This has always been:
DNS name if it exists, else dotted-quad

We could make it always
dotted-quad

but the former works better when you have multi-homed clients.

The compromise would be
If there are any names in /etc/exports and DNS name exists, use it,
else used dotted quad.

In 2.6, if /proc/fs/nfsd is mounted, the name used is a comma-separated list of all names mentioned in /etc/exports which match the IP address.

The same could possibly done for when /proc/fs/nfsd isn't mounted.

NeilBrown

-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
_______________________________________________
NFS maillist - [email protected]
https://lists.sourceforge.net/lists/listinfo/nfs

#7 lore raw
Quoted 17 lines
Quoted 4 lines

/home 1.2.3.4(rw)
/public joker(rw)

What is the failure scenario? I don't quite understand where is the problem.

Suppose joker is 1.2.3.4

joker mounts '/home'. Mountd doesn't bother with the DNS lookup and tell the kernel:

1/ IP address "1.2.3.4" has name "1.2.3.4"
2/ client with name "1.2.3.4" is allowed to access /home with
options "rw"

Then joker mounts '/public' so mountd need to do the DNS lookup, and tells the kernel:

Quoted 6 lines

1/ IP address "1.2.3.4" has name "joker"
2/ client with name "joker" is allowed to access /public
with options "rw".

Now joker tries to access it's mount of /home, and this fails, because 1.2.3.4 maps to "joker", and "joker" doesn't have access to /home

Uggh.

BTW, I understood in-kernel NFS auth as follows:

RPC request came -> kernel translates sender's IP into name -> kernel checks name against in-kernel export table for permissions

In-kernel export table gets (de)populated by mountd, as needed.

Correct me if I misunderstood something.

Quoted 6 lines

For this reason, there must be a canonical name for each IP address. This has always been:
DNS name if it exists, else dotted-quad

We could make it always
dotted-quad

Sounds very sensible. Will this allow us to skip IP->name translation in kernel entirely?

Quoted 1 line

but the former works better when you have multi-homed clients.

By keeping in-kernel export table compact? This isn't big, you rarely have hosts with 10+ heads. Any other reasons?

Quoted 7 lines

The compromise would be
If there are any names in /etc/exports and DNS name exists, use it,
else use dotted quad.

In 2.6, if /proc/fs/nfsd is mounted, the name used is a comma-separated list of all names mentioned in /etc/exports which match the IP address.

I see. I am getting familiar with how NFS internals work.
I exported / both to 127.0.0.1 and to localhost. Instrumented mountd says:

mountd: cache_export(): exp->m_client->m_addrlist[0]=127.0.0.1
mountd: cache_export(): exp->m_client->m_hostname=127.0.0.1,localhost

                                                  ^^^^^^^^^^^^^^^^^^^

mountd: cache_export_ent(): domain=127.0.0.1,localhost
mountd: cache_export_ent(): exp->e_path=/

Quoted 1 line

The same could possibly done for when /proc/fs/nfsd isn't mounted.

--
vda

-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
_______________________________________________
NFS maillist - [email protected]
https://lists.sourceforge.net/lists/listinfo/nfs