2017-04-30 14:09:55 -07:00
|
|
|
|
/* Copyright (c) 2008, 2009, 2010, 2011, 2013, 2016, 2017 Nicira, Inc.
|
2009-09-24 15:02:36 -07:00
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at:
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
|
|
#include "uuid.h"
|
|
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
#include <sys/types.h>
|
2010-08-13 09:58:29 -07:00
|
|
|
|
#include <unistd.h>
|
2009-09-24 15:02:36 -07:00
|
|
|
|
|
|
|
|
|
#include "aes128.h"
|
2010-08-12 15:47:25 -07:00
|
|
|
|
#include "entropy.h"
|
2021-05-27 15:29:01 +02:00
|
|
|
|
#include "fatal-signal.h"
|
|
|
|
|
#include "openvswitch/vlog.h"
|
|
|
|
|
#include "ovs-replay.h"
|
2013-04-23 16:37:44 -07:00
|
|
|
|
#include "ovs-thread.h"
|
2009-09-24 15:02:36 -07:00
|
|
|
|
#include "sha1.h"
|
2011-03-31 16:23:50 -07:00
|
|
|
|
#include "timeval.h"
|
2009-09-24 15:02:36 -07:00
|
|
|
|
#include "util.h"
|
|
|
|
|
|
2021-05-27 15:29:01 +02:00
|
|
|
|
VLOG_DEFINE_THIS_MODULE(uuid);
|
|
|
|
|
|
2009-09-24 15:02:36 -07:00
|
|
|
|
static struct aes128 key;
|
|
|
|
|
static uint64_t counter[2];
|
|
|
|
|
BUILD_ASSERT_DECL(sizeof counter == 16);
|
|
|
|
|
|
|
|
|
|
static void do_init(void);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Initialize the UUID module. Aborts the program with an error message if
|
|
|
|
|
* initialization fails (which should never happen on a properly configured
|
|
|
|
|
* machine.)
|
|
|
|
|
*
|
|
|
|
|
* Currently initialization is only needed by uuid_generate(). uuid_generate()
|
|
|
|
|
* will automatically call uuid_init() itself, so it's only necessary to call
|
|
|
|
|
* this function explicitly if you want to abort the program earlier than the
|
|
|
|
|
* first UUID generation in case of failure.
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
uuid_init(void)
|
|
|
|
|
{
|
2013-04-23 16:37:44 -07:00
|
|
|
|
static pthread_once_t once = PTHREAD_ONCE_INIT;
|
|
|
|
|
pthread_once(&once, do_init);
|
2009-09-24 15:02:36 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-27 15:29:01 +02:00
|
|
|
|
/* Record/replay of uuid generation. */
|
|
|
|
|
static replay_file_t uuid_replay_file;
|
|
|
|
|
static int uuid_replay_seqno;
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
uuid_replay_file_close(void *aux OVS_UNUSED)
|
|
|
|
|
{
|
|
|
|
|
ovs_replay_file_close(uuid_replay_file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
uuid_replay_file_open(void)
|
|
|
|
|
{
|
|
|
|
|
int error;
|
|
|
|
|
|
|
|
|
|
ovs_replay_lock();
|
|
|
|
|
error = ovs_replay_file_open("__uuid_generate", &uuid_replay_file,
|
|
|
|
|
&uuid_replay_seqno);
|
|
|
|
|
ovs_replay_unlock();
|
|
|
|
|
if (error) {
|
|
|
|
|
VLOG_FATAL("failed to open uuid replay file: %s.",
|
|
|
|
|
ovs_strerror(error));
|
|
|
|
|
}
|
|
|
|
|
fatal_signal_add_hook(uuid_replay_file_close, NULL, NULL, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
uuid_replay_file_read(struct uuid *uuid)
|
|
|
|
|
{
|
|
|
|
|
int norm_seqno = ovs_replay_normalized_seqno(uuid_replay_seqno);
|
|
|
|
|
int retval, len;
|
|
|
|
|
|
|
|
|
|
ovs_replay_lock();
|
|
|
|
|
ovs_assert(norm_seqno == ovs_replay_seqno());
|
|
|
|
|
ovs_assert(ovs_replay_seqno_is_read(uuid_replay_seqno));
|
|
|
|
|
|
|
|
|
|
retval = ovs_replay_read(uuid_replay_file, uuid, sizeof *uuid,
|
|
|
|
|
&len, &uuid_replay_seqno, true);
|
|
|
|
|
if (retval || len != sizeof *uuid) {
|
|
|
|
|
VLOG_FATAL("failed to read from replay file: %s.",
|
|
|
|
|
ovs_strerror(retval));
|
|
|
|
|
}
|
|
|
|
|
ovs_replay_unlock();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
uuid_replay_file_write(struct uuid *uuid)
|
|
|
|
|
{
|
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
|
|
retval = ovs_replay_write(uuid_replay_file, uuid, sizeof *uuid, true);
|
|
|
|
|
if (retval) {
|
|
|
|
|
VLOG_FATAL("failed to write uuid to replay file: %s.",
|
|
|
|
|
ovs_strerror(retval));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-24 15:02:36 -07:00
|
|
|
|
/* Generates a new random UUID in 'uuid'.
|
|
|
|
|
*
|
|
|
|
|
* We go to some trouble to ensure as best we can that the generated UUID has
|
|
|
|
|
* these properties:
|
|
|
|
|
*
|
|
|
|
|
* - Uniqueness. The random number generator is seeded using both the
|
|
|
|
|
* system clock and the system random number generator, plus a few
|
|
|
|
|
* other identifiers, which is about as good as we can get in any kind
|
|
|
|
|
* of simple way.
|
|
|
|
|
*
|
|
|
|
|
* - Unpredictability. In some situations it could be bad for an
|
|
|
|
|
* adversary to be able to guess the next UUID to be generated with some
|
|
|
|
|
* probability of success. This property may or may not be important
|
|
|
|
|
* for our purposes, but it is better if we can get it.
|
|
|
|
|
*
|
|
|
|
|
* To ensure both of these, we start by taking our seed data and passing it
|
|
|
|
|
* through SHA-1. We use the result as an AES-128 key. We also generate a
|
|
|
|
|
* random 16-byte value[*] which we then use as the counter for CTR mode. To
|
|
|
|
|
* generate a UUID in a manner compliant with the above goals, we merely
|
|
|
|
|
* increment the counter and encrypt it.
|
|
|
|
|
*
|
|
|
|
|
* [*] It is not actually important that the initial value of the counter be
|
|
|
|
|
* random. AES-128 in counter mode is secure either way.
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
uuid_generate(struct uuid *uuid)
|
|
|
|
|
{
|
Use "error-checking" mutexes in place of other kinds wherever possible.
We've seen a number of deadlocks in the tree since thread safety was
introduced. So far, all of these are self-deadlocks, that is, a single
thread acquiring a lock and then attempting to re-acquire the same lock
recursively. When this has happened, the process simply hung, and it was
somewhat difficult to find the cause.
POSIX "error-checking" mutexes check for this specific problem (and
others). This commit switches from other types of mutexes to
error-checking mutexes everywhere that we can, that is, everywhere that
we're not using recursive mutexes. This ought to help find problems more
quickly in the future.
There might be performance advantages to other kinds of mutexes in some
cases. However, the existing mutex type choices were just guesses, so I'd
rather go for easy detection of errors until we know that other mutex
types actually perform better in specific cases. Also, I did a quick
microbenchmark of glibc mutex types on my host and found that the
error checking mutexes weren't any slower than the other types, at least
when the mutex is uncontended.
Signed-off-by: Ben Pfaff <blp@nicira.com>
Acked-by: Ethan Jackson <ethan@nicira.com>
2013-08-20 13:40:02 -07:00
|
|
|
|
static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
|
2021-05-27 15:29:01 +02:00
|
|
|
|
enum ovs_replay_state replay_state = ovs_replay_get_state();
|
2013-04-23 16:37:44 -07:00
|
|
|
|
uint64_t copy[2];
|
|
|
|
|
|
2009-09-24 15:02:36 -07:00
|
|
|
|
uuid_init();
|
|
|
|
|
|
2021-05-27 15:29:01 +02:00
|
|
|
|
if (replay_state == OVS_REPLAY_READ) {
|
|
|
|
|
uuid_replay_file_read(uuid);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-23 16:37:44 -07:00
|
|
|
|
/* Copy out the counter's current value, then increment it. */
|
2013-07-30 15:31:48 -07:00
|
|
|
|
ovs_mutex_lock(&mutex);
|
2013-04-23 16:37:44 -07:00
|
|
|
|
copy[0] = counter[0];
|
|
|
|
|
copy[1] = counter[1];
|
2009-09-24 15:02:36 -07:00
|
|
|
|
if (++counter[1] == 0) {
|
|
|
|
|
counter[0]++;
|
|
|
|
|
}
|
2013-07-30 15:31:48 -07:00
|
|
|
|
ovs_mutex_unlock(&mutex);
|
2009-09-24 15:02:36 -07:00
|
|
|
|
|
|
|
|
|
/* AES output is exactly 16 bytes, so we encrypt directly into 'uuid'. */
|
2013-04-23 16:37:44 -07:00
|
|
|
|
aes128_encrypt(&key, copy, uuid);
|
2009-09-24 15:02:36 -07:00
|
|
|
|
|
2015-06-09 13:35:29 -07:00
|
|
|
|
uuid_set_bits_v4(uuid);
|
2021-05-27 15:29:01 +02:00
|
|
|
|
|
|
|
|
|
if (replay_state == OVS_REPLAY_WRITE) {
|
|
|
|
|
uuid_replay_file_write(uuid);
|
|
|
|
|
}
|
2015-06-09 13:35:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-22 14:37:09 -07:00
|
|
|
|
struct uuid
|
|
|
|
|
uuid_random(void)
|
|
|
|
|
{
|
|
|
|
|
struct uuid uuid;
|
|
|
|
|
uuid_generate(&uuid);
|
|
|
|
|
return uuid;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-09 13:35:29 -07:00
|
|
|
|
void
|
|
|
|
|
uuid_set_bits_v4(struct uuid *uuid)
|
|
|
|
|
{
|
2009-09-24 15:02:36 -07:00
|
|
|
|
/* Set bits to indicate a random UUID. See RFC 4122 section 4.4. */
|
|
|
|
|
uuid->parts[2] &= ~0xc0000000;
|
|
|
|
|
uuid->parts[2] |= 0x80000000;
|
|
|
|
|
uuid->parts[1] &= ~0x0000f000;
|
|
|
|
|
uuid->parts[1] |= 0x00004000;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Sets 'uuid' to all-zero-bits. */
|
|
|
|
|
void
|
|
|
|
|
uuid_zero(struct uuid *uuid)
|
|
|
|
|
{
|
2016-11-11 15:47:54 -08:00
|
|
|
|
*uuid = UUID_ZERO;
|
2009-09-24 15:02:36 -07:00
|
|
|
|
}
|
|
|
|
|
|
2010-01-11 13:14:54 -08:00
|
|
|
|
/* Returns true if 'uuid' is all zero, otherwise false. */
|
|
|
|
|
bool
|
|
|
|
|
uuid_is_zero(const struct uuid *uuid)
|
|
|
|
|
{
|
|
|
|
|
return (!uuid->parts[0] && !uuid->parts[1]
|
|
|
|
|
&& !uuid->parts[2] && !uuid->parts[3]);
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-24 15:02:36 -07:00
|
|
|
|
/* Compares 'a' and 'b'. Returns a negative value if 'a < b', zero if 'a ==
|
|
|
|
|
* b', or positive if 'a > b'. The ordering is lexicographical order of the
|
|
|
|
|
* conventional way of writing out UUIDs as strings. */
|
|
|
|
|
int
|
|
|
|
|
uuid_compare_3way(const struct uuid *a, const struct uuid *b)
|
|
|
|
|
{
|
|
|
|
|
if (a->parts[0] != b->parts[0]) {
|
|
|
|
|
return a->parts[0] > b->parts[0] ? 1 : -1;
|
|
|
|
|
} else if (a->parts[1] != b->parts[1]) {
|
|
|
|
|
return a->parts[1] > b->parts[1] ? 1 : -1;
|
|
|
|
|
} else if (a->parts[2] != b->parts[2]) {
|
|
|
|
|
return a->parts[2] > b->parts[2] ? 1 : -1;
|
|
|
|
|
} else if (a->parts[3] != b->parts[3]) {
|
|
|
|
|
return a->parts[3] > b->parts[3] ? 1 : -1;
|
|
|
|
|
} else {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Attempts to convert string 's' into a UUID in 'uuid'. Returns true if
|
|
|
|
|
* successful, which will be the case only if 's' has the exact format
|
|
|
|
|
* specified by RFC 4122. Returns false on failure. On failure, 'uuid' will
|
|
|
|
|
* be set to all-zero-bits. */
|
|
|
|
|
bool
|
|
|
|
|
uuid_from_string(struct uuid *uuid, const char *s)
|
2010-02-10 15:54:43 -08:00
|
|
|
|
{
|
|
|
|
|
if (!uuid_from_string_prefix(uuid, s)) {
|
|
|
|
|
return false;
|
|
|
|
|
} else if (s[UUID_LEN] != '\0') {
|
|
|
|
|
uuid_zero(uuid);
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Same as uuid_from_string() but s[UUID_LEN] is not required to be a null byte
|
|
|
|
|
* to succeed; that is, 's' need only begin with UUID syntax, not consist
|
|
|
|
|
* entirely of it. */
|
|
|
|
|
bool
|
|
|
|
|
uuid_from_string_prefix(struct uuid *uuid, const char *s)
|
2009-09-24 15:02:36 -07:00
|
|
|
|
{
|
2010-11-15 10:18:10 -08:00
|
|
|
|
/* 0 1 2 3 */
|
|
|
|
|
/* 012345678901234567890123456789012345 */
|
|
|
|
|
/* ------------------------------------ */
|
|
|
|
|
/* 00000000-1111-1111-2222-222233333333 */
|
|
|
|
|
|
|
|
|
|
bool ok;
|
|
|
|
|
|
|
|
|
|
uuid->parts[0] = hexits_value(s, 8, &ok);
|
|
|
|
|
if (!ok || s[8] != '-') {
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uuid->parts[1] = hexits_value(s + 9, 4, &ok) << 16;
|
|
|
|
|
if (!ok || s[13] != '-') {
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uuid->parts[1] += hexits_value(s + 14, 4, &ok);
|
|
|
|
|
if (!ok || s[18] != '-') {
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uuid->parts[2] = hexits_value(s + 19, 4, &ok) << 16;
|
|
|
|
|
if (!ok || s[23] != '-') {
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uuid->parts[2] += hexits_value(s + 24, 4, &ok);
|
|
|
|
|
if (!ok) {
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uuid->parts[3] = hexits_value(s + 28, 8, &ok);
|
|
|
|
|
if (!ok) {
|
|
|
|
|
goto error;
|
2009-09-24 15:02:36 -07:00
|
|
|
|
}
|
2010-11-15 10:18:10 -08:00
|
|
|
|
return true;
|
2009-09-24 15:02:36 -07:00
|
|
|
|
|
|
|
|
|
error:
|
|
|
|
|
uuid_zero(uuid);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-12-28 09:17:51 -08:00
|
|
|
|
|
2017-04-30 14:09:55 -07:00
|
|
|
|
/* If 's' is a string representation of a UUID, or the beginning of one,
|
|
|
|
|
* returns strlen(s), otherwise 0.
|
|
|
|
|
*
|
|
|
|
|
* For example:
|
|
|
|
|
*
|
|
|
|
|
* "123" yields 3
|
|
|
|
|
* "xyzzy" yields 0
|
|
|
|
|
* "123xyzzy" yields 0
|
|
|
|
|
* "e66250bb-9531-491b-b9c3-5385cabb0080" yields 36
|
|
|
|
|
* "e66250bb-9531-491b-b9c3-5385cabb0080xyzzy" yields 0
|
|
|
|
|
*/
|
2016-12-28 09:17:51 -08:00
|
|
|
|
int
|
|
|
|
|
uuid_is_partial_string(const char *s)
|
|
|
|
|
{
|
2025-04-24 13:30:47 +02:00
|
|
|
|
const char *tmpl = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
|
2016-12-28 09:17:51 -08:00
|
|
|
|
size_t i;
|
|
|
|
|
for (i = 0; i < UUID_LEN; i++) {
|
2017-04-30 14:09:55 -07:00
|
|
|
|
if (s[i] == '\0') {
|
|
|
|
|
return i;
|
|
|
|
|
} else if (tmpl[i] == 'x'
|
|
|
|
|
? hexit_value(s[i]) < 0
|
|
|
|
|
: s[i] != '-') {
|
|
|
|
|
return 0;
|
2016-12-28 09:17:51 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-04-30 14:09:55 -07:00
|
|
|
|
if (s[i] != '\0') {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2016-12-28 09:17:51 -08:00
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-30 14:10:29 -07:00
|
|
|
|
/* Compares 'match' to the string representation of 'uuid'. If 'match' equals
|
|
|
|
|
* or is a prefix of this string representation, returns strlen(match);
|
|
|
|
|
* otherwise, returns 0. */
|
|
|
|
|
int
|
|
|
|
|
uuid_is_partial_match(const struct uuid *uuid, const char *match)
|
|
|
|
|
{
|
|
|
|
|
char uuid_s[UUID_LEN + 1];
|
|
|
|
|
snprintf(uuid_s, sizeof uuid_s, UUID_FMT, UUID_ARGS(uuid));
|
|
|
|
|
size_t match_len = strlen(match);
|
|
|
|
|
return !strncmp(uuid_s, match, match_len) ? match_len : 0;
|
|
|
|
|
}
|
2009-09-24 15:02:36 -07:00
|
|
|
|
|
2014-03-10 09:18:50 -07:00
|
|
|
|
static void
|
|
|
|
|
sha1_update_int(struct sha1_ctx *sha1_ctx, uintmax_t x)
|
|
|
|
|
{
|
|
|
|
|
sha1_update(sha1_ctx, &x, sizeof x);
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-24 15:02:36 -07:00
|
|
|
|
static void
|
|
|
|
|
do_init(void)
|
|
|
|
|
{
|
|
|
|
|
uint8_t sha1[SHA1_DIGEST_SIZE];
|
|
|
|
|
struct sha1_ctx sha1_ctx;
|
|
|
|
|
uint8_t random_seed[16];
|
|
|
|
|
struct timeval now;
|
|
|
|
|
|
2021-05-27 15:29:01 +02:00
|
|
|
|
if (ovs_replay_is_active()) {
|
|
|
|
|
uuid_replay_file_open();
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-24 15:02:36 -07:00
|
|
|
|
/* Get seed data. */
|
2010-08-12 15:47:25 -07:00
|
|
|
|
get_entropy_or_die(random_seed, sizeof random_seed);
|
2011-03-31 16:23:50 -07:00
|
|
|
|
xgettimeofday(&now);
|
2009-09-24 15:02:36 -07:00
|
|
|
|
|
|
|
|
|
/* Convert seed into key. */
|
|
|
|
|
sha1_init(&sha1_ctx);
|
|
|
|
|
sha1_update(&sha1_ctx, random_seed, sizeof random_seed);
|
2014-03-10 09:18:50 -07:00
|
|
|
|
sha1_update(&sha1_ctx, &now, sizeof now);
|
|
|
|
|
sha1_update_int(&sha1_ctx, getpid());
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
sha1_update_int(&sha1_ctx, getppid());
|
|
|
|
|
sha1_update_int(&sha1_ctx, getuid());
|
|
|
|
|
sha1_update_int(&sha1_ctx, getgid());
|
|
|
|
|
#endif
|
2009-09-24 15:02:36 -07:00
|
|
|
|
sha1_final(&sha1_ctx, sha1);
|
|
|
|
|
|
|
|
|
|
/* Generate key. */
|
|
|
|
|
BUILD_ASSERT(sizeof sha1 >= 16);
|
|
|
|
|
aes128_schedule(&key, sha1);
|
|
|
|
|
|
|
|
|
|
/* Generate initial counter. */
|
2010-08-12 15:47:25 -07:00
|
|
|
|
get_entropy_or_die(counter, sizeof counter);
|
2009-09-24 15:02:36 -07:00
|
|
|
|
}
|