[PATCH v1 1/1] sysctl: introduce uuid_le and uuid_be

From: Andy Shevchenko
Date: Tue May 24 2016 - 13:27:35 EST


By default the sysctl interface returns random UUID in big endian format.
Sometimes it's not suitable, e.g. using generated UUID for EFI variable name.
Provide uuid_le and uuid_be to comprehence that interface.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@xxxxxxxxxxxxxxx>
---
drivers/char/random.c | 44 +++++++++++++++++++++++++++++++++++++++++
include/uapi/linux/sysctl.h | 4 +++-
kernel/sysctl_binary.c | 48 ++++++++++++++++++++++++++++++++++++++++++++-
3 files changed, 94 insertions(+), 2 deletions(-)

diff --git a/drivers/char/random.c b/drivers/char/random.c
index 0158d3b..a864013 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -1673,6 +1673,38 @@ static int proc_do_uuid(struct ctl_table *table, int write,
return proc_dostring(&fake_table, write, buffer, lenp, ppos);
}

+static int proc_do_uuid_le(struct ctl_table *table, int write,
+ void __user *buffer, size_t *lenp, loff_t *ppos)
+{
+ struct ctl_table fake_table;
+ unsigned char buf[64];
+ uuid_le uuid;
+
+ uuid_le_gen(&uuid);
+ sprintf(buf, "%pUl", &uuid);
+
+ fake_table.data = buf;
+ fake_table.maxlen = sizeof(buf);
+
+ return proc_dostring(&fake_table, write, buffer, lenp, ppos);
+}
+
+static int proc_do_uuid_be(struct ctl_table *table, int write,
+ void __user *buffer, size_t *lenp, loff_t *ppos)
+{
+ struct ctl_table fake_table;
+ unsigned char buf[64];
+ uuid_be uuid;
+
+ uuid_be_gen(&uuid);
+ sprintf(buf, "%pUb", &uuid);
+
+ fake_table.data = buf;
+ fake_table.maxlen = sizeof(buf);
+
+ return proc_dostring(&fake_table, write, buffer, lenp, ppos);
+}
+
/*
* Return entropy available scaled to integral bits
*/
@@ -1745,6 +1777,18 @@ struct ctl_table random_table[] = {
.mode = 0444,
.proc_handler = proc_do_uuid,
},
+ {
+ .procname = "uuid_le",
+ .maxlen = 16,
+ .mode = 0444,
+ .proc_handler = proc_do_uuid_le,
+ },
+ {
+ .procname = "uuid_be",
+ .maxlen = 16,
+ .mode = 0444,
+ .proc_handler = proc_do_uuid_be,
+ },
#ifdef ADD_INTERRUPT_BENCH
{
.procname = "add_interrupt_avg_cycles",
diff --git a/include/uapi/linux/sysctl.h b/include/uapi/linux/sysctl.h
index 0956373..db5fcf1 100644
--- a/include/uapi/linux/sysctl.h
+++ b/include/uapi/linux/sysctl.h
@@ -233,7 +233,9 @@ enum
RANDOM_READ_THRESH=3,
RANDOM_WRITE_THRESH=4,
RANDOM_BOOT_ID=5,
- RANDOM_UUID=6
+ RANDOM_UUID=6,
+ RANDOM_UUID_LE=7,
+ RANDOM_UUID_BE=8,
};

/* /proc/sys/kernel/pty */
diff --git a/kernel/sysctl_binary.c b/kernel/sysctl_binary.c
index 6eb99c1..56b8c90 100644
--- a/kernel/sysctl_binary.c
+++ b/kernel/sysctl_binary.c
@@ -28,6 +28,8 @@ static bin_convert_t bin_string;
static bin_convert_t bin_intvec;
static bin_convert_t bin_ulongvec;
static bin_convert_t bin_uuid;
+static bin_convert_t bin_uuid_le;
+static bin_convert_t bin_uuid_be;
static bin_convert_t bin_dn_node_address;

#define CTL_DIR bin_dir
@@ -35,6 +37,8 @@ static bin_convert_t bin_dn_node_address;
#define CTL_INT bin_intvec
#define CTL_ULONG bin_ulongvec
#define CTL_UUID bin_uuid
+#define CTL_UUID_LE bin_uuid_le
+#define CTL_UUID_BE bin_uuid_be
#define CTL_DNADR bin_dn_node_address

#define BUFSZ 256
@@ -53,6 +57,8 @@ static const struct bin_table bin_random_table[] = {
{ CTL_INT, RANDOM_WRITE_THRESH, "write_wakeup_threshold" },
{ CTL_UUID, RANDOM_BOOT_ID, "boot_id" },
{ CTL_UUID, RANDOM_UUID, "uuid" },
+ { CTL_UUID_LE, RANDOM_UUID_LE, "uuid_le" },
+ { CTL_UUID_BE, RANDOM_UUID_BE, "uuid_be" },
{}
};

@@ -1111,7 +1117,7 @@ out:
return result;
}

-static ssize_t bin_uuid(struct file *file,
+static ssize_t bin_uuid_be(struct file *file,
void __user *oldval, size_t oldlen, void __user *newval, size_t newlen)
{
ssize_t result, copied = 0;
@@ -1145,6 +1151,46 @@ out:
return result;
}

+static ssize_t bin_uuid_le(struct file *file,
+ void __user *oldval, size_t oldlen, void __user *newval, size_t newlen)
+{
+ ssize_t result, copied = 0;
+
+ /* Only supports reads */
+ if (oldval && oldlen) {
+ char buf[UUID_STRING_LEN + 1];
+ uuid_le uuid;
+
+ result = kernel_read(file, 0, buf, sizeof(buf) - 1);
+ if (result < 0)
+ goto out;
+
+ buf[result] = '\0';
+
+ result = -EIO;
+ if (uuid_le_to_bin(buf, &uuid))
+ goto out;
+
+ if (oldlen > 16)
+ oldlen = 16;
+
+ result = -EFAULT;
+ if (copy_to_user(oldval, &uuid, oldlen))
+ goto out;
+
+ copied = oldlen;
+ }
+ result = copied;
+out:
+ return result;
+}
+
+static ssize_t bin_uuid(struct file *file,
+ void __user *oldval, size_t oldlen, void __user *newval, size_t newlen)
+{
+ return bin_uuid_be(file, oldval, oldlen, newval, newlen);
+}
+
static ssize_t bin_dn_node_address(struct file *file,
void __user *oldval, size_t oldlen, void __user *newval, size_t newlen)
{
--
2.8.1