From owner-svn-src-head@freebsd.org  Fri May 10 16:43:48 2019
Return-Path: <owner-svn-src-head@freebsd.org>
Delivered-To: svn-src-head@mailman.ysv.freebsd.org
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1])
 by mailman.ysv.freebsd.org (Postfix) with ESMTP id 5F61C15A723C;
 Fri, 10 May 2019 16:43:48 +0000 (UTC)
 (envelope-from markj@FreeBSD.org)
Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org
 [IPv6:2610:1c1:1:606c::19:3])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 server-signature RSA-PSS (4096 bits)
 client-signature RSA-PSS (4096 bits) client-digest SHA256)
 (Client CN "mxrelay.nyi.freebsd.org",
 Issuer "Let's Encrypt Authority X3" (verified OK))
 by mx1.freebsd.org (Postfix) with ESMTPS id 0553180AFC;
 Fri, 10 May 2019 16:43:48 +0000 (UTC)
 (envelope-from markj@FreeBSD.org)
Received: from repo.freebsd.org (repo.freebsd.org
 [IPv6:2610:1c1:1:6068::e6a:0])
 (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
 (Client did not present a certificate)
 by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id C8E136FC7;
 Fri, 10 May 2019 16:43:47 +0000 (UTC)
 (envelope-from markj@FreeBSD.org)
Received: from repo.freebsd.org ([127.0.1.37])
 by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id x4AGhlkx087907;
 Fri, 10 May 2019 16:43:47 GMT (envelope-from markj@FreeBSD.org)
Received: (from markj@localhost)
 by repo.freebsd.org (8.15.2/8.15.2/Submit) id x4AGhlja087906;
 Fri, 10 May 2019 16:43:47 GMT (envelope-from markj@FreeBSD.org)
Message-Id: <201905101643.x4AGhlja087906@repo.freebsd.org>
X-Authentication-Warning: repo.freebsd.org: markj set sender to
 markj@FreeBSD.org using -f
From: Mark Johnston <markj@FreeBSD.org>
Date: Fri, 10 May 2019 16:43:47 +0000 (UTC)
To: src-committers@freebsd.org, svn-src-all@freebsd.org,
 svn-src-head@freebsd.org
Subject: svn commit: r347439 - head/lib/libnetgraph
X-SVN-Group: head
X-SVN-Commit-Author: markj
X-SVN-Commit-Paths: head/lib/libnetgraph
X-SVN-Commit-Revision: 347439
X-SVN-Commit-Repository: base
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Rspamd-Queue-Id: 0553180AFC
X-Spamd-Bar: --
Authentication-Results: mx1.freebsd.org
X-Spamd-Result: default: False [-2.97 / 15.00];
 local_wl_from(0.00)[FreeBSD.org];
 NEURAL_HAM_MEDIUM(-1.00)[-0.995,0];
 NEURAL_HAM_LONG(-1.00)[-1.000,0];
 NEURAL_HAM_SHORT(-0.98)[-0.977,0];
 ASN(0.00)[asn:11403, ipnet:2610:1c1:1::/48, country:US]
X-BeenThere: svn-src-head@freebsd.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: SVN commit messages for the src tree for head/-current
 <svn-src-head.freebsd.org>
List-Unsubscribe: <https://lists.freebsd.org/mailman/options/svn-src-head>,
 <mailto:svn-src-head-request@freebsd.org?subject=unsubscribe>
List-Archive: <http://lists.freebsd.org/pipermail/svn-src-head/>
List-Post: <mailto:svn-src-head@freebsd.org>
List-Help: <mailto:svn-src-head-request@freebsd.org?subject=help>
List-Subscribe: <https://lists.freebsd.org/mailman/listinfo/svn-src-head>,
 <mailto:svn-src-head-request@freebsd.org?subject=subscribe>
X-List-Received-Date: Fri, 10 May 2019 16:43:48 -0000

Author: markj
Date: Fri May 10 16:43:47 2019
New Revision: 347439
URL: https://svnweb.freebsd.org/changeset/base/347439

Log:
  Atomically update the global gMsgId in libnetgraph.
  
  Otherwise concurrently running threads may inadvertently use the same
  token for different messages.
  
  Preserve the behaviour of disallowing negative message tokens, but allow
  a message token value of zero since this simplifies the code a bit and
  tokens are documented to be non-negative.
  
  PR:		234442
  Reported and tested by:	eugen
  MFC after:	1 month
  Sponsored by:	The FreeBSD Foundation

Modified:
  head/lib/libnetgraph/msg.c

Modified: head/lib/libnetgraph/msg.c
==============================================================================
--- head/lib/libnetgraph/msg.c	Fri May 10 16:41:33 2019	(r347438)
+++ head/lib/libnetgraph/msg.c	Fri May 10 16:43:47 2019	(r347439)
@@ -44,6 +44,7 @@ __FBSDID("$FreeBSD$");
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <stdarg.h>
+#include <stdatomic.h>
 #include <netgraph/ng_message.h>
 #include <netgraph/ng_socket.h>
 
@@ -51,7 +52,7 @@ __FBSDID("$FreeBSD$");
 #include "internal.h"
 
 /* Next message token value */
-static int	gMsgId;
+static _Atomic(unsigned int) gMsgId;
 
 /* For delivering both messages and replies */
 static int	NgDeliverMsg(int cs, const char *path,
@@ -72,9 +73,7 @@ NgSendMsg(int cs, const char *path,
 	memset(&msg, 0, sizeof(msg));
 	msg.header.version = NG_VERSION;
 	msg.header.typecookie = cookie;
-	if (++gMsgId < 0)
-		gMsgId = 1;
-	msg.header.token = gMsgId;
+	msg.header.token = atomic_fetch_add(&gMsgId, 1) & INT_MAX;
 	msg.header.flags = NGF_ORIG;
 	msg.header.cmd = cmd;
 	snprintf((char *)msg.header.cmdstr, NG_CMDSTRSIZ, "cmd%d", cmd);
@@ -143,9 +142,7 @@ NgSendAsciiMsg(int cs, const char *path, const char *f
 
 	/* Now send binary version */
 	binary = (struct ng_mesg *)reply->data;
-	if (++gMsgId < 0)
-		gMsgId = 1;
-	binary->header.token = gMsgId;
+	binary->header.token = atomic_fetch_add(&gMsgId, 1) & INT_MAX;
 	binary->header.version = NG_VERSION;
 	if (NgDeliverMsg(cs,
 	    path, binary, binary->data, binary->header.arglen) < 0) {