Skip to contentSkip to conversation

nfsd: Protect session creation and client confirm using client_lock

bug report Dan Carpenter 4 participants 11 messages Sep 7, 2021 – Sep 15, 2021

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

Hello Jeff Layton,

The patch d20c11d86d8f: "nfsd: Protect session creation and client confirm using client_lock" from Jul 30, 2014, leads to the following Smatch static checker warning:

	net/sunrpc/addr.c:178 rpc_parse_scope_id()
	warn: sleeping in atomic context

net/sunrpc/addr.c

    161 static int rpc_parse_scope_id(struct net *net, const char *buf,
    162                               const size_t buflen, const char *delim,
    163                               struct sockaddr_in6 *sin6)
    164 {
    165         char *p;
    166         size_t len;
    167 
    168         if ((buf + buflen) == delim)
    169                 return 1;
    170 
    171         if (*delim != IPV6_SCOPE_DELIMITER)
    172                 return 0;
    173 
    174         if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
    175                 return 0;
    176 
    177         len = (buf + buflen) - delim - 1;
--> 178         p = kmemdup_nul(delim + 1, len, GFP_KERNEL);
    179         if (p) {
    180                 u32 scope_id = 0;
    181                 struct net_device *dev;
    182 
    183                 dev = dev_get_by_name(net, p);
    184                 if (dev != NULL) {
    185                         scope_id = dev->ifindex;
    186                         dev_put(dev);
    187                 } else {
    188                         if (kstrtou32(p, 10, &scope_id) != 0) {
    189                                 kfree(p);
    190                                 return 0;
    191                         }
    192                 }
    193 
    194                 kfree(p);
    195 
    196                 sin6->sin6_scope_id = scope_id;
    197                 return 1;
    198         }
    199 
    200         return 0;
    201 }

The call tree is:

nfsd4_setclientid() <- disables preempt
-> gen_callback()
-> rpc_uaddr2sockaddr()

      -> rpc_pton()
         -> rpc_pton6()
            -> rpc_parse_scope_id()

The commit added a spin_lock to nfsd4_setclientid().

fs/nfsd/nfs4state.c
4023 trace_nfsd_clid_verf_mismatch(conf, rqstp,
4024 &clverifier);
4025 } else
4026 trace_nfsd_clid_fresh(new);
4027 new->cl_minorversion = 0;

  4028          gen_callback(new, setclid, rqstp);
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  4029          add_to_unconfirmed(new);
  4030          setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
  4031          setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
  4032          memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
  4033          new = NULL;
  4034          status = nfs_ok;
  4035  out:
  4036          spin_unlock(&nn->client_lock);
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This is the new lock.

  4037          if (new)
  4038                  free_client(new);
  4039          if (unconf) {
  4040                  trace_nfsd_clid_expire_unconf(&unconf->cl_clientid);
  4041                  expire_client(unconf);
  4042          }
  4043          return status;

regards,
dan carpenter

maintainer
#1 lore raw
Quoted 91 lines · Dan Carpenter

Hello Jeff Layton,

The patch d20c11d86d8f: "nfsd: Protect session creation and client confirm using client_lock" from Jul 30, 2014, leads to the following Smatch static checker warning:

	net/sunrpc/addr.c:178 rpc_parse_scope_id()
	warn: sleeping in atomic context

net/sunrpc/addr.c

    161 static int rpc_parse_scope_id(struct net *net, const char *buf,
    162                               const size_t buflen, const char *delim,
    163                               struct sockaddr_in6 *sin6)
    164 {
    165         char *p;
    166         size_t len;
    167 
    168         if ((buf + buflen) == delim)
    169                 return 1;
    170 
    171         if (*delim != IPV6_SCOPE_DELIMITER)
    172                 return 0;
    173 
    174         if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
    175                 return 0;
    176 
    177         len = (buf + buflen) - delim - 1;
--> 178         p = kmemdup_nul(delim + 1, len, GFP_KERNEL);
    179         if (p) {
    180                 u32 scope_id = 0;
    181                 struct net_device *dev;
    182 
    183                 dev = dev_get_by_name(net, p);
    184                 if (dev != NULL) {
    185                         scope_id = dev->ifindex;
    186                         dev_put(dev);
    187                 } else {
    188                         if (kstrtou32(p, 10, &scope_id) != 0) {
    189                                 kfree(p);
    190                                 return 0;
    191                         }
    192                 }
    193 
    194                 kfree(p);
    195 
    196                 sin6->sin6_scope_id = scope_id;
    197                 return 1;
    198         }
    199 
    200         return 0;
    201 }

The call tree is:

nfsd4_setclientid() <- disables preempt
-> gen_callback()
-> rpc_uaddr2sockaddr()

      -> rpc_pton()
         -> rpc_pton6()
            -> rpc_parse_scope_id()

The commit added a spin_lock to nfsd4_setclientid().

fs/nfsd/nfs4state.c
4023 trace_nfsd_clid_verf_mismatch(conf, rqstp,
4024 &clverifier);
4025 } else
4026 trace_nfsd_clid_fresh(new);
4027 new->cl_minorversion = 0;

  4028          gen_callback(new, setclid, rqstp);
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  4029          add_to_unconfirmed(new);
  4030          setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
  4031          setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
  4032          memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
  4033          new = NULL;
  4034          status = nfs_ok;
  4035  out:
  4036          spin_unlock(&nn->client_lock);
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This is the new lock.

  4037          if (new)
  4038                  free_client(new);
  4039          if (unconf) {
  4040                  trace_nfsd_clid_expire_unconf(&unconf->cl_clientid);
  4041                  expire_client(unconf);
  4042          }
  4043          return status;
Quoted 2 lines

regards,
dan carpenter

(cc'ing Bruce and Chuck)

Wow that is an oldie, but it does seem to be a legit bug.

Probably we should just make rpc_parse_scope_id use an on-stack buffer instead of calling kmemdup_nul there. Thoughts?

Signature · 1 line
Jeff Layton <[email protected]>
#2 lore raw
Quoted 99 lines · Jeff Layton
Quoted 91 lines · Dan Carpenter

Hello Jeff Layton,

The patch d20c11d86d8f: "nfsd: Protect session creation and client confirm using client_lock" from Jul 30, 2014, leads to the following Smatch static checker warning:

	net/sunrpc/addr.c:178 rpc_parse_scope_id()
	warn: sleeping in atomic context

net/sunrpc/addr.c
161 static int rpc_parse_scope_id(struct net *net, const char *buf,
162 const size_t buflen, const char *delim,
163 struct sockaddr_in6 *sin6)

   164 {
   165         char *p;
   166         size_t len;
   167 
   168         if ((buf + buflen) == delim)
   169                 return 1;
   170 
   171         if (*delim != IPV6_SCOPE_DELIMITER)
   172                 return 0;
   173 
   174         if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
   175                 return 0;
   176 
   177         len = (buf + buflen) - delim - 1;
--> 178         p = kmemdup_nul(delim + 1, len, GFP_KERNEL);
   179         if (p) {
   180                 u32 scope_id = 0;
   181                 struct net_device *dev;
   182 
   183                 dev = dev_get_by_name(net, p);
   184                 if (dev != NULL) {
   185                         scope_id = dev->ifindex;
   186                         dev_put(dev);
   187                 } else {
   188                         if (kstrtou32(p, 10, &scope_id) != 0) {
   189                                 kfree(p);
   190                                 return 0;
   191                         }
   192                 }
   193 
   194                 kfree(p);
   195 
   196                 sin6->sin6_scope_id = scope_id;
   197                 return 1;
   198         }
   199 
   200         return 0;
   201 }

The call tree is:

nfsd4_setclientid() <- disables preempt
-> gen_callback()
-> rpc_uaddr2sockaddr()

     -> rpc_pton()
        -> rpc_pton6()
           -> rpc_parse_scope_id()

The commit added a spin_lock to nfsd4_setclientid().

fs/nfsd/nfs4state.c
4023 trace_nfsd_clid_verf_mismatch(conf, rqstp,
4024 &clverifier);
4025 } else
4026 trace_nfsd_clid_fresh(new);
4027 new->cl_minorversion = 0;

 4028          gen_callback(new, setclid, rqstp);
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 4029          add_to_unconfirmed(new);
 4030          setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
 4031          setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
 4032          memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
 4033          new = NULL;
 4034          status = nfs_ok;
 4035  out:
 4036          spin_unlock(&nn->client_lock);
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This is the new lock.

4037 if (new)
4038 free_client(new);
4039 if (unconf) {
4040 trace_nfsd_clid_expire_unconf(&unconf->cl_clientid);
4041 expire_client(unconf);
4042 }
4043 return status;

Quoted 2 lines

regards,
dan carpenter

Quoted 6 lines

(cc'ing Bruce and Chuck)

Wow that is an oldie, but it does seem to be a legit bug.

Probably we should just make rpc_parse_scope_id use an on-stack buffer instead of calling kmemdup_nul there. Thoughts?

We have IPV6_SCOPE_ID_LEN as a maximum size of the scope ID, and it's not a big value. As long as boundary checking is made to be sufficient, then a stack residency for the device name should be safe.

--
Chuck Lever

#3 lore raw
Quoted 4 lines · Chuck Lever III

We have IPV6_SCOPE_ID_LEN as a maximum size of the scope ID, and it's not a big value. As long as boundary checking is made to be sufficient, then a stack residency for the device name should be safe.

Something like this? (Or are you making a patch? I'm not even sure how to test.)

--b.

net/sunrpc/addr.c +18 -22
index 6e4dbd577a39..d435bffc6199 100644
@@ -162,8 +162,10 @@ static int rpc_parse_scope_id(struct net *net, const char *buf,
 			      const size_t buflen, const char *delim,
 			      struct sockaddr_in6 *sin6)
 {
-	char *p;
+	char p[IPV6_SCOPE_ID_LEN + 1];
 	size_t len;
+	u32 scope_id = 0;
+	struct net_device *dev;
 
 	if ((buf + buflen) == delim)
 		return 1;
@@ -175,29 +177,23 @@ static int rpc_parse_scope_id(struct net *net, const char *buf,
 		return 0;
 
 	len = (buf + buflen) - delim - 1;
-	p = kmemdup_nul(delim + 1, len, GFP_KERNEL);
-	if (p) {
-		u32 scope_id = 0;
-		struct net_device *dev;
-
-		dev = dev_get_by_name(net, p);
-		if (dev != NULL) {
-			scope_id = dev->ifindex;
-			dev_put(dev);
-		} else {
-			if (kstrtou32(p, 10, &scope_id) != 0) {
-				kfree(p);
-				return 0;
-			}
-		}
-
-		kfree(p);
-
-		sin6->sin6_scope_id = scope_id;
-		return 1;
+	if (len > IPV6_SCOPE_ID_LEN)
+		return 0;
+
+	memcpy(p, delim + 1, len);
+	p[len] = 0;
+
+	dev = dev_get_by_name(net, p);
+	if (dev != NULL) {
+		scope_id = dev->ifindex;
+		dev_put(dev);
+	} else {
+		if (kstrtou32(p, 10, &scope_id) != 0)
+			return 0;
 	}
 
-	return 0;
+	sin6->sin6_scope_id = scope_id;
+	return 1;
 }
 
 static size_t rpc_pton6(struct net *net, const char *buf, const size_t buflen,
#4 lore raw
Quoted 11 lines · Bruce Fields
Quoted 4 lines · Chuck Lever III

We have IPV6_SCOPE_ID_LEN as a maximum size of the scope ID, and it's not a big value. As long as boundary checking is made to be sufficient, then a stack residency for the device name should be safe.

Something like this? (Or are you making a patch?

I thought Jeff was going to handle it? More below.

Quoted 39 lines

I'm not even sure how to test.)

--b.

net/sunrpc/addr.c +3 -16
index 6e4dbd577a39..d435bffc6199 100644
@@ -162,8 +162,10 @@ static int rpc_parse_scope_id(struct net *net, const char *buf,
 			      const size_t buflen, const char *delim,
 			      struct sockaddr_in6 *sin6)
 {
-	char *p;
+	char p[IPV6_SCOPE_ID_LEN + 1];
 	size_t len;
+	u32 scope_id = 0;
+	struct net_device *dev;
 
 	if ((buf + buflen) == delim)
 		return 1;
@@ -175,29 +177,23 @@ static int rpc_parse_scope_id(struct net *net, const char *buf,
 		return 0;
 
 	len = (buf + buflen) - delim - 1;
-	p = kmemdup_nul(delim + 1, len, GFP_KERNEL);
-	if (p) {
-		u32 scope_id = 0;
-		struct net_device *dev;
-
-		dev = dev_get_by_name(net, p);
-		if (dev != NULL) {
-			scope_id = dev->ifindex;
-			dev_put(dev);
-		} else {
-			if (kstrtou32(p, 10, &scope_id) != 0) {
-				kfree(p);
-				return 0;
-			}
-		}

net/sunrpc/addr.c static int rpc_parse_scope_id(struct net *net, const char *buf, · line 184

Quoted 10 lines
net/sunrpc/addr.c +5 -5
-
-		kfree(p);
-
-		sin6->sin6_scope_id = scope_id;
-		return 1;
+	if (len > IPV6_SCOPE_ID_LEN)
+		return 0;
+
+	memcpy(p, delim + 1, len);
+	p[len] = 0;

If I recall correctly, Linus prefers us to use the str*() functions instead of raw memcpy() in cases like this.

Quoted 6 lines
net/sunrpc/addr.c +6 -0
+
+	dev = dev_get_by_name(net, p);
+	if (dev != NULL) {
+		scope_id = dev->ifindex;
+		dev_put(dev);
+	} else {

net/sunrpc/addr.c static int rpc_parse_scope_id(struct net *net, const char *buf, · line 199

Quoted 10 lines
net/sunrpc/addr.c +4 -1
+		if (kstrtou32(p, 10, &scope_id) != 0)
+			return 0;
 	}
 
-	return 0;
+	sin6->sin6_scope_id = scope_id;
+	return 1;
 }
 
 static size_t rpc_pton6(struct net *net, const char *buf, const size_t buflen,

--
Chuck Lever

maintainer
#5 lore raw
Quoted 4 lines · Chuck Lever III
Quoted 4 lines · Bruce Fields

On Tue, Sep 07, 2021 at 03:00:23PM +0000, Chuck Lever III wrote:

Quoted 9 lines
Quoted 6 lines
Quoted 4 lines

We have IPV6_SCOPE_ID_LEN as a maximum size of the scope ID, and it's not a big value. As long as boundary checking is made to be sufficient, then a stack residency for the device name should be safe.

Something like this? (Or are you making a patch?

I thought Jeff was going to handle it? More below.

No, sorry... I was just suggesting a potential fix. I'd probably rather you guys fix it since you're better positioned to test this at the moment.

Quoted 45 lines
Quoted 45 lines

I'm not even sure how to test.)
are
--b.

net/sunrpc/addr.c +3 -21
index 6e4dbd577a39..d435bffc6199 100644
@@ -162,8 +162,10 @@ static int rpc_parse_scope_id(struct net *net, const char *buf,
 			      const size_t buflen, const char *delim,
 			      struct sockaddr_in6 *sin6)
 {
-	char *p;
+	char p[IPV6_SCOPE_ID_LEN + 1];
 	size_t len;
+	u32 scope_id = 0;
+	struct net_device *dev;
 
 	if ((buf + buflen) == delim)
 		return 1;
@@ -175,29 +177,23 @@ static int rpc_parse_scope_id(struct net *net, const char *buf,
 		return 0;
 
 	len = (buf + buflen) - delim - 1;
-	p = kmemdup_nul(delim + 1, len, GFP_KERNEL);
-	if (p) {
-		u32 scope_id = 0;
-		struct net_device *dev;
-
-		dev = dev_get_by_name(net, p);
-		if (dev != NULL) {
-			scope_id = dev->ifindex;
-			dev_put(dev);
-		} else {
-			if (kstrtou32(p, 10, &scope_id) != 0) {
-				kfree(p);
-				return 0;
-			}
-		}
-
-		kfree(p);
-
-		sin6->sin6_scope_id = scope_id;
-		return 1;
Quoted 9 lines

net/sunrpc/addr.c static int rpc_parse_scope_id(struct net *net, const char *buf, · line 184

Quoted 5 lines
net/sunrpc/addr.c +5 -0
+	if (len > IPV6_SCOPE_ID_LEN)
+		return 0;
+
+	memcpy(p, delim + 1, len);
+	p[len] = 0;

If I recall correctly, Linus prefers us to use the str*() functions instead of raw memcpy() in cases like this.

I hadn't heard that. If you already know the length to be copied, then strcpy and the like tend to be less efficient since they continually have to check for null terminators as they walk the source string.

Quoted 23 lines
Quoted 7 lines
net/sunrpc/addr.c +6 -0
+
+	dev = dev_get_by_name(net, p);
+	if (dev != NULL) {
+		scope_id = dev->ifindex;
+		dev_put(dev);
+	} else {

net/sunrpc/addr.c static int rpc_parse_scope_id(struct net *net, const char *buf, · line 199

Quoted 10 lines
net/sunrpc/addr.c +4 -1
+		if (kstrtou32(p, 10, &scope_id) != 0)
+			return 0;
 	}
 
-	return 0;
+	sin6->sin6_scope_id = scope_id;
+	return 1;
 }
 
 static size_t rpc_pton6(struct net *net, const char *buf, const size_t buflen,

--
Chuck Lever

Signature · 1 line
Jeff Layton <[email protected]>
#6 lore raw
Quoted 77 lines · Jeff Layton
Quoted 4 lines · Chuck Lever III
Quoted 4 lines · Bruce Fields

On Tue, Sep 07, 2021 at 03:00:23PM +0000, Chuck Lever III wrote:

Quoted 9 lines
Quoted 6 lines
Quoted 4 lines

We have IPV6_SCOPE_ID_LEN as a maximum size of the scope ID, and it's not a big value. As long as boundary checking is made to be sufficient, then a stack residency for the device name should be safe.

Something like this? (Or are you making a patch?

I thought Jeff was going to handle it? More below.

No, sorry... I was just suggesting a potential fix. I'd probably rather you guys fix it since you're better positioned to test this at the moment.

Quoted 45 lines
Quoted 45 lines

I'm not even sure how to test.)
are
--b.

net/sunrpc/addr.c +3 -21
index 6e4dbd577a39..d435bffc6199 100644
@@ -162,8 +162,10 @@ static int rpc_parse_scope_id(struct net *net, const char *buf,
 			      const size_t buflen, const char *delim,
 			      struct sockaddr_in6 *sin6)
 {
-	char *p;
+	char p[IPV6_SCOPE_ID_LEN + 1];
 	size_t len;
+	u32 scope_id = 0;
+	struct net_device *dev;
 
 	if ((buf + buflen) == delim)
 		return 1;
@@ -175,29 +177,23 @@ static int rpc_parse_scope_id(struct net *net, const char *buf,
 		return 0;
 
 	len = (buf + buflen) - delim - 1;
-	p = kmemdup_nul(delim + 1, len, GFP_KERNEL);
-	if (p) {
-		u32 scope_id = 0;
-		struct net_device *dev;
-
-		dev = dev_get_by_name(net, p);
-		if (dev != NULL) {
-			scope_id = dev->ifindex;
-			dev_put(dev);
-		} else {
-			if (kstrtou32(p, 10, &scope_id) != 0) {
-				kfree(p);
-				return 0;
-			}
-		}
-
-		kfree(p);
-
-		sin6->sin6_scope_id = scope_id;
-		return 1;
Quoted 8 lines

net/sunrpc/addr.c static int rpc_parse_scope_id(struct net *net, const char *buf, · line 184

Quoted 5 lines
net/sunrpc/addr.c +5 -0
+	if (len > IPV6_SCOPE_ID_LEN)
+		return 0;
+
+	memcpy(p, delim + 1, len);
+	p[len] = 0;

If I recall correctly, Linus prefers us to use the str*() functions instead of raw memcpy() in cases like this.

Quoted 1 line

I hadn't heard that.

I'm paraphrasing these:

https://lore.kernel.org/lkml/CAHk-=wj5Pp5J-CAPck22RSQ13k3cEOVnJHUA-WocAZqCJK1BZw@mail.gmail.com/

https://lore.kernel.org/lkml/CAHk-=wjWosrcv2=6m-=YgXRKev=5cnCg-1EhqDpbRXT5z6eQmg@mail.gmail.com/

Quoted 3 lines

If you already know the length to be copied, then
strcpy and the like tend to be less efficient since they continually have to check for null terminators as they walk the source string.

I'm sure there's one str helper that comes close to what we need. Here efficiency doesn't really matter, and the size of the device string is always going to be in the single digits.

The priority is correctness.

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
net/sunrpc/addr.c +10 -1
+
+	dev = dev_get_by_name(net, p);
+	if (dev != NULL) {
+		scope_id = dev->ifindex;
+		dev_put(dev);
+	} else {
+		if (kstrtou32(p, 10, &scope_id) != 0)
+			return 0;
 	}
 
-	return 0;
+	sin6->sin6_scope_id = scope_id;
+	return 1;
 }
 
Quoted 10 lines
Quoted 7 lines

net/sunrpc/addr.c static int rpc_parse_scope_id(struct net *net, const char *buf, · line 199

Quoted 1 line
net/sunrpc/addr.c +0 -0
 static size_t rpc_pton6(struct net *net, const char *buf, const size_t buflen,

--
Chuck Lever

Signature · 1 line
Jeff Layton <[email protected]>

--
Chuck Lever

maintainer
#7 lore raw
Quoted 89 lines · Chuck Lever III
Quoted 76 lines · Jeff Layton
Quoted 4 lines · Chuck Lever III
Quoted 4 lines · Bruce Fields

On Tue, Sep 07, 2021 at 03:00:23PM +0000, Chuck Lever III wrote:

Quoted 9 lines
Quoted 6 lines
Quoted 4 lines

We have IPV6_SCOPE_ID_LEN as a maximum size of the scope ID, and it's not a big value. As long as boundary checking is made to be sufficient, then a stack residency for the device name should be safe.

Something like this? (Or are you making a patch?

I thought Jeff was going to handle it? More below.

No, sorry... I was just suggesting a potential fix. I'd probably rather you guys fix it since you're better positioned to test this at the moment.

Quoted 45 lines
Quoted 45 lines

I'm not even sure how to test.)
are
--b.

net/sunrpc/addr.c +3 -21
index 6e4dbd577a39..d435bffc6199 100644
@@ -162,8 +162,10 @@ static int rpc_parse_scope_id(struct net *net, const char *buf,
 			      const size_t buflen, const char *delim,
 			      struct sockaddr_in6 *sin6)
 {
-	char *p;
+	char p[IPV6_SCOPE_ID_LEN + 1];
 	size_t len;
+	u32 scope_id = 0;
+	struct net_device *dev;
 
 	if ((buf + buflen) == delim)
 		return 1;
@@ -175,29 +177,23 @@ static int rpc_parse_scope_id(struct net *net, const char *buf,
 		return 0;
 
 	len = (buf + buflen) - delim - 1;
-	p = kmemdup_nul(delim + 1, len, GFP_KERNEL);
-	if (p) {
-		u32 scope_id = 0;
-		struct net_device *dev;
-
-		dev = dev_get_by_name(net, p);
-		if (dev != NULL) {
-			scope_id = dev->ifindex;
-			dev_put(dev);
-		} else {
-			if (kstrtou32(p, 10, &scope_id) != 0) {
-				kfree(p);
-				return 0;
-			}
-		}
-
-		kfree(p);
-
-		sin6->sin6_scope_id = scope_id;
-		return 1;
Quoted 8 lines

net/sunrpc/addr.c static int rpc_parse_scope_id(struct net *net, const char *buf, · line 184

Quoted 5 lines
net/sunrpc/addr.c +5 -0
+	if (len > IPV6_SCOPE_ID_LEN)
+		return 0;
+
+	memcpy(p, delim + 1, len);
+	p[len] = 0;

If I recall correctly, Linus prefers us to use the str*() functions instead of raw memcpy() in cases like this.

Quoted 1 line

I hadn't heard that.

I'm paraphrasing these:

https://lore.kernel.org/lkml/CAHk-=wj5Pp5J-CAPck22RSQ13k3cEOVnJHUA-WocAZqCJK1BZw@mail.gmail.com/

https://lore.kernel.org/lkml/CAHk-=wjWosrcv2=6m-=YgXRKev=5cnCg-1EhqDpbRXT5z6eQmg@mail.gmail.com/

Quoted 3 lines

If you already know the length to be copied, then
strcpy and the like tend to be less efficient since they continually have to check for null terminators as they walk the source string.

Quoted 7 lines

I'm sure there's one str helper that comes close to what we need. Here efficiency doesn't really matter, and the size of the device string is always going to be in the single digits.

The priority is correctness.

Hmm, it sounds line in the second email he suggests using memcpy():

"Your "memcpy()" example implies that the source is always a fixed-size thing. In that case, maybe that's the rigth thing to do, and you should just create a real function for it."

Maybe I'm missing the context though.

In any case, when you're certain about the length of the source and destination buffers, there's no real benefit to avoiding memcpy in favor of strcpy and the like. It's just as correct.

Your call though, of course. ;)

Quoted 31 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
net/sunrpc/addr.c +10 -1
+
+	dev = dev_get_by_name(net, p);
+	if (dev != NULL) {
+		scope_id = dev->ifindex;
+		dev_put(dev);
+	} else {
+		if (kstrtou32(p, 10, &scope_id) != 0)
+			return 0;
 	}
 
-	return 0;
+	sin6->sin6_scope_id = scope_id;
+	return 1;
 }
 
Quoted 10 lines
Quoted 7 lines

net/sunrpc/addr.c static int rpc_parse_scope_id(struct net *net, const char *buf, · line 199

Quoted 1 line
net/sunrpc/addr.c +0 -0
 static size_t rpc_pton6(struct net *net, const char *buf, const size_t buflen,

--
Chuck Lever

Signature · 1 line
Jeff Layton <[email protected]>

--
Chuck Lever

Signature · 1 line
Jeff Layton <[email protected]>
#8 lore raw

From: "J. Bruce Fields" <[email protected]>
Subject: [PATCH] nfsd: don't alloc under spinlock in rpc_parse_scope_id

Dan Carpenter says:

The patch d20c11d86d8f: "nfsd: Protect session creation and client confirm using client_lock" from Jul 30, 2014, leads to the following Smatch static checker warning:

net/sunrpc/addr.c:178 rpc_parse_scope_id()
warn: sleeping in atomic context

Fixes d20c11d86d8f ("nfsd: Protect session creation and client...")

---

 net/sunrpc/addr.c | 40 ++++++++++++++++++----------------------
 1 file changed, 18 insertions(+), 22 deletions(-)
Quoted 11 lines · Jeff Layton

Hmm, it sounds line in the second email he suggests using memcpy():

"Your "memcpy()" example implies that the source is always a fixed-size thing. In that case, maybe that's the rigth thing to do, and you should just create a real function for it."

Maybe I'm missing the context though.

In any case, when you're certain about the length of the source and destination buffers, there's no real benefit to avoiding memcpy in favor of strcpy and the like. It's just as correct.

OK, queueing this up as is for 5.16 unless someone objects. (But, could really use testing, I'm not currently testing over ipv6.)--b.

net/sunrpc/addr.c +18 -22
index 6e4dbd577a39..d435bffc6199 100644
@@ -162,8 +162,10 @@ static int rpc_parse_scope_id(struct net *net, const char *buf,
 			      const size_t buflen, const char *delim,
 			      struct sockaddr_in6 *sin6)
 {
-	char *p;
+	char p[IPV6_SCOPE_ID_LEN + 1];
 	size_t len;
+	u32 scope_id = 0;
+	struct net_device *dev;
 
 	if ((buf + buflen) == delim)
 		return 1;
@@ -175,29 +177,23 @@ static int rpc_parse_scope_id(struct net *net, const char *buf,
 		return 0;
 
 	len = (buf + buflen) - delim - 1;
-	p = kmemdup_nul(delim + 1, len, GFP_KERNEL);
-	if (p) {
-		u32 scope_id = 0;
-		struct net_device *dev;
-
-		dev = dev_get_by_name(net, p);
-		if (dev != NULL) {
-			scope_id = dev->ifindex;
-			dev_put(dev);
-		} else {
-			if (kstrtou32(p, 10, &scope_id) != 0) {
-				kfree(p);
-				return 0;
-			}
-		}
-
-		kfree(p);
-
-		sin6->sin6_scope_id = scope_id;
-		return 1;
+	if (len > IPV6_SCOPE_ID_LEN)
+		return 0;
+
+	memcpy(p, delim + 1, len);
+	p[len] = 0;
+
+	dev = dev_get_by_name(net, p);
+	if (dev != NULL) {
+		scope_id = dev->ifindex;
+		dev_put(dev);
+	} else {
+		if (kstrtou32(p, 10, &scope_id) != 0)
+			return 0;
 	}
 
-	return 0;
+	sin6->sin6_scope_id = scope_id;
+	return 1;
 }
 
 static size_t rpc_pton6(struct net *net, const char *buf, const size_t buflen,
Signature · 1 line
2.31.1
#9 lore raw
Quoted 19 lines · Bruce Fields

From: "J. Bruce Fields" <[email protected]>
Subject: [PATCH] nfsd: don't alloc under spinlock in rpc_parse_scope_id

Dan Carpenter says:

The patch d20c11d86d8f: "nfsd: Protect session creation and client confirm using client_lock" from Jul 30, 2014, leads to the following Smatch static checker warning:

net/sunrpc/addr.c:178 rpc_parse_scope_id()
warn: sleeping in atomic context

Fixes d20c11d86d8f ("nfsd: Protect session creation and client...")
Quoted 13 lines

---

net/sunrpc/addr.c | 40 ++++++++++++++++++----------------------

1 file changed, 18 insertions(+), 22 deletions(-)
Quoted 7 lines · Jeff Layton

Hmm, it sounds line in the second email he suggests using memcpy():

"Your "memcpy()" example implies that the source is always a fixed-size thing. In that case, maybe that's the rigth thing to do, and you should just create a real function for it."

Maybe I'm missing the context though.

The scope identifier isn't fixed in size, so I'm not sure how you got there.

Quoted 5 lines
Quoted 3 lines

In any case, when you're certain about the length of the source and destination buffers, there's no real benefit to avoiding memcpy in favor of strcpy and the like. It's just as correct.

OK, queueing this up as is for 5.16 unless someone objects.

IMO Linus prefers strscpy() over open-coded memcpys, but it's not a hill I'm going to fight and die on.

Quoted 2 lines

(But, could
really use testing, I'm not currently testing over ipv6.)--b.

Seems like you could generate some artificial test cases without needing to set up IPv6.

Quoted 64 lines
net/sunrpc/addr.c +18 -22
index 6e4dbd577a39..d435bffc6199 100644
@@ -162,8 +162,10 @@ static int rpc_parse_scope_id(struct net *net, const char *buf,
 			      const size_t buflen, const char *delim,
 			      struct sockaddr_in6 *sin6)
 {
-	char *p;
+	char p[IPV6_SCOPE_ID_LEN + 1];
 	size_t len;
+	u32 scope_id = 0;
+	struct net_device *dev;
 
 	if ((buf + buflen) == delim)
 		return 1;
@@ -175,29 +177,23 @@ static int rpc_parse_scope_id(struct net *net, const char *buf,
 		return 0;
 
 	len = (buf + buflen) - delim - 1;
-	p = kmemdup_nul(delim + 1, len, GFP_KERNEL);
-	if (p) {
-		u32 scope_id = 0;
-		struct net_device *dev;
-
-		dev = dev_get_by_name(net, p);
-		if (dev != NULL) {
-			scope_id = dev->ifindex;
-			dev_put(dev);
-		} else {
-			if (kstrtou32(p, 10, &scope_id) != 0) {
-				kfree(p);
-				return 0;
-			}
-		}
-
-		kfree(p);
-
-		sin6->sin6_scope_id = scope_id;
-		return 1;
+	if (len > IPV6_SCOPE_ID_LEN)
+		return 0;
+
+	memcpy(p, delim + 1, len);
+	p[len] = 0;
+
+	dev = dev_get_by_name(net, p);
+	if (dev != NULL) {
+		scope_id = dev->ifindex;
+		dev_put(dev);
+	} else {
+		if (kstrtou32(p, 10, &scope_id) != 0)
+			return 0;
 	}
 
-	return 0;
+	sin6->sin6_scope_id = scope_id;
+	return 1;
 }
 
 static size_t rpc_pton6(struct net *net, const char *buf, const size_t buflen,
Signature · 1 line
2.31.1

--
Chuck Lever

#10 lore raw
Quoted 51 lines · Chuck Lever III
Quoted 20 lines · Bruce Fields

From: "J. Bruce Fields" <[email protected]>
Subject: [PATCH] nfsd: don't alloc under spinlock in rpc_parse_scope_id

Dan Carpenter says:

The patch d20c11d86d8f: "nfsd: Protect session creation and client confirm using client_lock" from Jul 30, 2014, leads to the following Smatch static checker warning:

net/sunrpc/addr.c:178 rpc_parse_scope_id()
warn: sleeping in atomic context

Fixes d20c11d86d8f ("nfsd: Protect session creation and client...")
Quoted 13 lines

---

net/sunrpc/addr.c | 40 ++++++++++++++++++----------------------

1 file changed, 18 insertions(+), 22 deletions(-)
Quoted 7 lines · Jeff Layton

Hmm, it sounds line in the second email he suggests using memcpy():

"Your "memcpy()" example implies that the source is always a fixed-size thing. In that case, maybe that's the rigth thing to do, and you should just create a real function for it."

Maybe I'm missing the context though.

The scope identifier isn't fixed in size, so I'm not sure how you got there.

Quoted 5 lines
Quoted 3 lines

In any case, when you're certain about the length of the source and destination buffers, there's no real benefit to avoiding memcpy in favor of strcpy and the like. It's just as correct.

OK, queueing this up as is for 5.16 unless someone objects.

IMO Linus prefers strscpy() over open-coded memcpys, but it's not a hill I'm going to fight and die on.

Quoted 5 lines
Quoted 2 lines

(But, could
really use testing, I'm not currently testing over ipv6.)--b.

Seems like you could generate some artificial test cases without needing to set up IPv6.

Actually, a scope ID is needed when using link-local addresses, which are set up automatically simply when IPv6 is enabled. If you see an address like this:

inet6 fe80::2eae:686b:8c82:fad2/64 scope link noprefixroute
valid_lft forever preferred_lft forever

Then you can use this address to mount with by adding that interface name as the scope ID.

Quoted 60 lines
Quoted 60 lines
net/sunrpc/addr.c +18 -22
index 6e4dbd577a39..d435bffc6199 100644
@@ -162,8 +162,10 @@ static int rpc_parse_scope_id(struct net *net, const char *buf,
 			      const size_t buflen, const char *delim,
 			      struct sockaddr_in6 *sin6)
 {
-	char *p;
+	char p[IPV6_SCOPE_ID_LEN + 1];
 	size_t len;
+	u32 scope_id = 0;
+	struct net_device *dev;
 
 	if ((buf + buflen) == delim)
 		return 1;
@@ -175,29 +177,23 @@ static int rpc_parse_scope_id(struct net *net, const char *buf,
 		return 0;
 
 	len = (buf + buflen) - delim - 1;
-	p = kmemdup_nul(delim + 1, len, GFP_KERNEL);
-	if (p) {
-		u32 scope_id = 0;
-		struct net_device *dev;
-
-		dev = dev_get_by_name(net, p);
-		if (dev != NULL) {
-			scope_id = dev->ifindex;
-			dev_put(dev);
-		} else {
-			if (kstrtou32(p, 10, &scope_id) != 0) {
-				kfree(p);
-				return 0;
-			}
-		}
-
-		kfree(p);
-
-		sin6->sin6_scope_id = scope_id;
-		return 1;
+	if (len > IPV6_SCOPE_ID_LEN)
+		return 0;
+
+	memcpy(p, delim + 1, len);
+	p[len] = 0;
+
+	dev = dev_get_by_name(net, p);
+	if (dev != NULL) {
+		scope_id = dev->ifindex;
+		dev_put(dev);
+	} else {
+		if (kstrtou32(p, 10, &scope_id) != 0)
+			return 0;
 	}
 
-	return 0;
+	sin6->sin6_scope_id = scope_id;
+	return 1;
 }
 
Quoted 7 lines
Quoted 4 lines
net/sunrpc/addr.c +0 -0
 static size_t rpc_pton6(struct net *net, const char *buf, const size_t buflen,
Signature · 1 line
2.31.1

--
Chuck Lever

--
Chuck Lever