From owner-svn-src-head@freebsd.org Wed Apr 15 18:39:13 2020 Return-Path: Delivered-To: svn-src-head@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id A286F2BE844; Wed, 15 Apr 2020 18:39:13 +0000 (UTC) (envelope-from kevans@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 492WNK3p0kz4D4b; Wed, 15 Apr 2020 18:39:13 +0000 (UTC) (envelope-from kevans@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 78E282358F; Wed, 15 Apr 2020 18:39:13 +0000 (UTC) (envelope-from kevans@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 03FIdDBP026072; Wed, 15 Apr 2020 18:39:13 GMT (envelope-from kevans@FreeBSD.org) Received: (from kevans@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 03FIdDPJ026070; Wed, 15 Apr 2020 18:39:13 GMT (envelope-from kevans@FreeBSD.org) Message-Id: <202004151839.03FIdDPJ026070@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: kevans set sender to kevans@FreeBSD.org using -f From: Kyle Evans Date: Wed, 15 Apr 2020 18:39:13 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r359980 - in head/sys: kern sys X-SVN-Group: head X-SVN-Commit-Author: kevans X-SVN-Commit-Paths: in head/sys: kern sys X-SVN-Commit-Revision: 359980 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 15 Apr 2020 18:39:13 -0000 Author: kevans Date: Wed Apr 15 18:39:12 2020 New Revision: 359980 URL: https://svnweb.freebsd.org/changeset/base/359980 Log: validate_uuid: absorb the rest of parse_uuid with a flags arg This makes the naming annoyance (validate_uuid vs. parse_uuid) less of an issue and centralizes all of the functionality into the new KPI while still making the extra validation optional. The end-result is all the same as far as hostuuid validation-only goes. Modified: head/sys/kern/kern_uuid.c head/sys/sys/uuid.h Modified: head/sys/kern/kern_uuid.c ============================================================================== --- head/sys/kern/kern_uuid.c Wed Apr 15 18:27:28 2020 (r359979) +++ head/sys/kern/kern_uuid.c Wed Apr 15 18:39:12 2020 (r359980) @@ -382,13 +382,21 @@ be_uuid_dec(void const *buf, struct uuid *uuid) } int -validate_uuid(const char *str, size_t size, struct uuid *uuid) +validate_uuid(const char *str, size_t size, struct uuid *uuid, int flags) { u_int c[11]; int n; - if (size == 0 || *str == '\0') + if (size == 0 || *str == '\0') { + /* An empty string may represent a nil UUID. */ + if ((flags & VUUIDF_EMPTYOK) != 0) { + if (uuid != NULL) + bzero(uuid, sizeof(*uuid)); + return (0); + } + return (EINVAL); + } /* The UUID string representation has a fixed length. */ if (size != 36) @@ -421,30 +429,21 @@ validate_uuid(const char *str, size_t size, struct uui uuid->node[n] = c[n + 5]; } - return (0); + if ((flags & VUUIDF_CHECKSEMANTICS) == 0) + return (0); + + return (((c[3] & 0x80) != 0x00 && /* variant 0? */ + (c[3] & 0xc0) != 0x80 && /* variant 1? */ + (c[3] & 0xe0) != 0xc0) ? EINVAL : 0); /* variant 2? */ } +#define VUUIDF_PARSEFLAGS (VUUIDF_EMPTYOK | VUUIDF_CHECKSEMANTICS) + int parse_uuid(const char *str, struct uuid *uuid) { - unsigned int clock_seq; - int ret; - /* An empty string represents a nil UUID. */ - if (*str == '\0') { - bzero(uuid, sizeof(*uuid)); - return (0); - } - - ret = validate_uuid(str, strlen(str), uuid); - if (ret != 0) - return (ret); - - /* Check semantics... */ - clock_seq = uuid->clock_seq_hi_and_reserved; - return (((clock_seq & 0x80) != 0x00 && /* variant 0? */ - (clock_seq & 0xc0) != 0x80 && /* variant 1? */ - (clock_seq & 0xe0) != 0xc0) ? EINVAL : 0); /* variant 2? */ + return (validate_uuid(str, strlen(str), uuid, VUUIDF_PARSEFLAGS)); } int Modified: head/sys/sys/uuid.h ============================================================================== --- head/sys/sys/uuid.h Wed Apr 15 18:27:28 2020 (r359979) +++ head/sys/sys/uuid.h Wed Apr 15 18:39:12 2020 (r359980) @@ -68,20 +68,18 @@ int printf_uuid(struct uuid *); int sbuf_printf_uuid(struct sbuf *, struct uuid *); /* - * There are a few key differences between validate_uuid and parse_uuid: - * - * - The struct uuid * parameter to validate_uuid is optional, so the caller - * can simply validate UUID format without doing anything with the result. - * - validate_uuid will not pass an empty string as a valid UUID, as it doesn't - * strictly meet the formatting requirements. parse_uuid will accept an - * empty string and zero out the uuid struct accordingly. - * - parse_uuid does additional semantic checks on clock_seq_hi_and_reserved - * that validate_uuid will not do. - * - * validate_uuid is intended to strictly check that it's a well-formed uuid. + * validate_uuid will, with no flags passed, validate only the format of the + * passed in UUID. Flags below are available to give it part of or all of the + * functionality that parse_uuid has traditionally had: acknowledging an empty + * string as valid, and checking the semantics of the UUID as well. */ -int validate_uuid(const char *, size_t, struct uuid *); +int validate_uuid(const char *, size_t, struct uuid *, int); int parse_uuid(const char *, struct uuid *); + +/* Flags to validate_uuid(). */ +#define VUUIDF_EMPTYOK 0x0001 +#define VUUIDF_CHECKSEMANTICS 0x0002 + int uuidcmp(const struct uuid *, const struct uuid *); void be_uuid_dec(void const *buf, struct uuid *uuid);