All of lore.kernel.org
 help / color / mirror / Atom feed
From: Chuck Lever <chuck.lever@oracle.com>
To: bfields@fieldses.org
Cc: linux-nfs@vger.kernel.org
Subject: [PATCH 07/13] SUNRPC: Switch one_sock_name() to use snprintf()
Date: Wed, 05 Nov 2008 12:29:13 -0500	[thread overview]
Message-ID: <20081105172913.7330.34745.stgit@ingres.1015granger.net> (raw)
In-Reply-To: <20081105172351.7330.50739.stgit-07a7zB5ZJzbwdl/1UfZZQIVfYA8g3rJ/@public.gmane.org>

Use snprintf() in one_sock_name() to help prevent buffer overflows.

Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
---

 net/sunrpc/svcsock.c |   23 +++++++++++++++++------
 1 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/net/sunrpc/svcsock.c b/net/sunrpc/svcsock.c
index ee42edd..db5eb7d 100644
--- a/net/sunrpc/svcsock.c
+++ b/net/sunrpc/svcsock.c
@@ -244,22 +244,27 @@ out:
 /*
  * Report socket names for nfsdfs
  */
-static int one_sock_name(char *buf, struct svc_sock *svsk)
+static int svc_one_sock_name(struct svc_sock *svsk, char *buf, int remaining)
 {
 	int len;
 
 	switch(svsk->sk_sk->sk_family) {
 	case AF_INET:
-		len = sprintf(buf, "ipv4 %s %u.%u.%u.%u %d\n",
+		len = snprintf(buf, remaining, "ipv4 %s %u.%u.%u.%u %d\n",
 			      svsk->sk_sk->sk_protocol==IPPROTO_UDP?
 			      "udp" : "tcp",
 			      NIPQUAD(inet_sk(svsk->sk_sk)->rcv_saddr),
 			      inet_sk(svsk->sk_sk)->num);
 		break;
 	default:
-		len = sprintf(buf, "*unknown-%d*\n",
+		len = snprintf(buf, remaining, "*unknown-%d*\n",
 			       svsk->sk_sk->sk_family);
 	}
+
+	if (len >= remaining) {
+		*buf = '\0';
+		return -ENAMETOOLONG;
+	}
 	return len;
 }
 
@@ -286,15 +291,21 @@ int svc_sock_names(struct svc_serv *serv, char *buf, size_t buflen,
 
 	if (!serv)
 		return 0;
+
 	spin_lock_bh(&serv->sv_lock);
 	list_for_each_entry(svsk, &serv->sv_permsocks, sk_xprt.xpt_list) {
-		int onelen = one_sock_name(buf+len, svsk);
-		if (toclose && strcmp(toclose, buf+len) == 0)
+		int onelen = svc_one_sock_name(svsk, buf + len, buflen - len);
+		if (onelen < 0) {
+			len = onelen;
+			break;
+		}
+		if (toclose && strcmp(toclose, buf + len) == 0)
 			closesk = svsk;
 		else
 			len += onelen;
 	}
 	spin_unlock_bh(&serv->sv_lock);
+
 	if (closesk)
 		/* Should unregister with portmap, but you cannot
 		 * unregister just one protocol...
@@ -1226,7 +1237,7 @@ int svc_addsock(struct svc_serv *serv, int fd, char *name_return, size_t len)
 		sockfd_put(so);
 		return err;
 	}
-	return one_sock_name(name_return, svsk);
+	return svc_one_sock_name(svsk, name_return, len);
 }
 EXPORT_SYMBOL_GPL(svc_addsock);
 


  parent reply	other threads:[~2008-11-05 17:30 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-11-05 17:28 [PATCH 00/13] kernel server-side IPv6 support Chuck Lever
     [not found] ` <20081105172351.7330.50739.stgit-07a7zB5ZJzbwdl/1UfZZQIVfYA8g3rJ/@public.gmane.org>
2008-11-05 17:28   ` [PATCH 01/13] lockd: Enable NLM use of AF_INET6 Chuck Lever
2008-11-05 17:28   ` [PATCH 02/13] NFSD: Add documenting comments for nfsctl interface Chuck Lever
2008-11-05 17:28   ` [PATCH 03/13] NFSD: Add helper functions for __write_ports() Chuck Lever
2008-11-05 17:28   ` [PATCH 04/13] NFSD: Refactor __write_ports() Chuck Lever
2008-11-05 17:28   ` [PATCH 05/13] NFSD: Prevent a buffer overflow in svc_xprt_names() Chuck Lever
2008-11-05 17:29   ` [PATCH 06/13] SUNRPC: pass buffer size to svc_addsock() and svc_sock_names() Chuck Lever
2008-11-05 17:29   ` Chuck Lever [this message]
2008-11-05 17:29   ` [PATCH 08/13] SUNRPC: Support AF_INET6 in one_sock_name() Chuck Lever
2008-11-05 17:29   ` [PATCH 09/13] SUNRPC: Clean up one_sock_name() Chuck Lever
2008-11-05 17:29   ` [PATCH 10/13] NFSD: Support AF_INET6 in svc_addsock() function Chuck Lever
2008-11-05 17:29   ` [PATCH 11/13] NFS: Move NFS client's IP address parser to nfs_common/ Chuck Lever
2008-11-05 17:29   ` [PATCH 12/13] NFSD: Support IPv6 addresses in write_failover_ip() Chuck Lever
2008-11-05 17:29   ` [PATCH 13/13] NFSD: Enable NFS server use of AF_INET6 Chuck Lever
2008-11-05 20:06   ` [PATCH 01/12] lockd: fix inconsistent use of nsm_use_hostnames J. Bruce Fields
2008-11-05 20:06     ` [PATCH 02/12] lockd: kill redundant host->h_server check J. Bruce Fields
2008-11-05 20:06       ` [PATCH 03/12] lockd: fix nsm_unmonitor return value J. Bruce Fields
2008-11-05 20:06         ` [PATCH 04/12] lockd: unmonitor only when last host goes away J. Bruce Fields
2008-11-05 20:06           ` [PATCH 05/12] lockd: encapsulate nlm_hosts table variables in one structure J. Bruce Fields
2008-11-05 20:06             ` [PATCH 06/12] lockd: define host_for_each{_safe} macros J. Bruce Fields
2008-11-05 20:06               ` [PATCH 07/12] lockd: reorganize nlm_host_rebooted J. Bruce Fields
2008-11-05 20:06                 ` [PATCH 08/12] lockd: move debugging warning into a separate function J. Bruce Fields
2008-11-05 20:06                   ` [PATCH 09/12] lockd: move garbage collection loop into " J. Bruce Fields
2008-11-05 20:06                     ` [PATCH 10/12] lockd: move immediate host expiry " J. Bruce Fields
2008-11-05 20:06                       ` [PATCH 11/12] lockd: split client and server host lists J. Bruce Fields
2008-11-05 20:06                         ` [PATCH 12/12] lockd: remove redundant rpc_shutdown_client() J. Bruce Fields
2008-11-06 12:17                           ` Jeff Layton
     [not found]                           ` <20081106071733.6d4983ce@barsoom .rdu.redhat.com>
     [not found]                             ` <20081106071733.6d4983ce-xSBYVWDuneFaJnirhKH9O4GKTjYczspe@public.gmane.org>
2008-11-06 14:32                               ` Chuck Lever
2008-11-06 14:51                                 ` Jeff Layton
2008-11-06 22:10                               ` J. Bruce Fields
2008-11-06 22:21                                 ` Chuck Lever
2008-11-06 22:22                                   ` J. Bruce Fields
2008-11-06 22:25                                 ` Jeff Layton
2008-11-05 22:33                       ` [PATCH 10/12] lockd: move immediate host expiry into separate function Chuck Lever
2008-11-05 22:29                 ` [PATCH 07/12] lockd: reorganize nlm_host_rebooted Chuck Lever
2008-11-05 22:25             ` [PATCH 05/12] lockd: encapsulate nlm_hosts table variables in one structure Chuck Lever
2008-11-05 22:41               ` J. Bruce Fields
2008-11-05 23:11                 ` Trond Myklebust
     [not found]                   ` <1225926710.16857.94.camel-rJ7iovZKK19ZJLDQqaL3InhyD016LWXt@public.gmane.org>
2008-11-05 23:21                     ` J. Bruce Fields
2008-11-05 22:22           ` [PATCH 04/12] lockd: unmonitor only when last host goes away Chuck Lever
2008-11-05 22:14         ` [PATCH 03/12] lockd: fix nsm_unmonitor return value Chuck Lever
2008-11-05 22:10     ` [PATCH 01/12] lockd: fix inconsistent use of nsm_use_hostnames Chuck Lever

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20081105172913.7330.34745.stgit@ingres.1015granger.net \
    --to=chuck.lever@oracle.com \
    --cc=bfields@fieldses.org \
    --cc=linux-nfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.