diff options
Diffstat (limited to 'sys')
-rw-r--r-- | sys/arch/amd64/isa/i8042.c | 6 | ||||
-rw-r--r-- | sys/dev/cons/cons.c | 2 | ||||
-rw-r--r-- | sys/dev/random/chacha20.c | 97 | ||||
-rw-r--r-- | sys/dev/random/entropy.c | 55 | ||||
-rw-r--r-- | sys/dev/random/random.c | 88 | ||||
-rw-r--r-- | sys/dev/random/siphash.c | 116 | ||||
-rw-r--r-- | sys/fs/initramfs.c | 6 | ||||
-rw-r--r-- | sys/include/dev/random/chacha20.h | 47 | ||||
-rw-r--r-- | sys/include/dev/random/entropy.h | 40 | ||||
-rw-r--r-- | sys/include/dev/random/siphash.h | 34 | ||||
-rw-r--r-- | sys/include/sys/limits.h | 1 | ||||
-rw-r--r-- | sys/include/sys/syscall.h | 1 | ||||
-rw-r--r-- | sys/include/sys/ucred.h | 2 | ||||
-rw-r--r-- | sys/include/sys/vnode.h | 24 | ||||
-rw-r--r-- | sys/kern/exec_elf64.c | 2 | ||||
-rw-r--r-- | sys/kern/kern_cred.c | 19 | ||||
-rw-r--r-- | sys/kern/kern_descrip.c | 2 | ||||
-rw-r--r-- | sys/kern/kern_exit.c | 16 | ||||
-rw-r--r-- | sys/kern/kern_syscall.c | 1 | ||||
-rw-r--r-- | sys/kern/vfs_lookup.c | 4 | ||||
-rw-r--r-- | sys/kern/vfs_subr.c | 6 | ||||
-rw-r--r-- | sys/kern/vfs_syscalls.c | 2 | ||||
-rw-r--r-- | sys/vm/vm_vnode.c | 2 |
23 files changed, 553 insertions, 20 deletions
diff --git a/sys/arch/amd64/isa/i8042.c b/sys/arch/amd64/isa/i8042.c index 69d9f92..cde70ff 100644 --- a/sys/arch/amd64/isa/i8042.c +++ b/sys/arch/amd64/isa/i8042.c @@ -33,6 +33,7 @@ #include <sys/syslog.h> #include <sys/spinlock.h> #include <sys/param.h> +#include <sys/ascii.h> #include <sys/proc.h> #include <sys/reboot.h> #include <sys/queue.h> @@ -75,7 +76,7 @@ static int i8042_kb_getc(uint8_t sc, char *chr); static void i8042_drain(void); static char keytab[] = { - '\0', '\0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', + '\0', '\x1B', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\b', '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n', '\0', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`', '\0', '\\', 'z', 'x', 'c', 'v', @@ -276,6 +277,9 @@ i8042_kb_getc(uint8_t sc, char *chr) bool release = ISSET(sc, BIT(7)); switch (sc) { + case 0x76: + *chr = ASCII_ESC; + return 0; /* Caps lock [press] */ case 0x3A: /* diff --git a/sys/dev/cons/cons.c b/sys/dev/cons/cons.c index 7407ec1..88cbcbe 100644 --- a/sys/dev/cons/cons.c +++ b/sys/dev/cons/cons.c @@ -375,7 +375,6 @@ dev_read(dev_t dev, struct sio_txn *sio, int flags) } cons_attach(); - spinlock_acquire(&g_root_scr.lock); for (;;) { /* Buffer too small */ if (n == 0) { @@ -392,7 +391,6 @@ dev_read(dev_t dev, struct sio_txn *sio, int flags) break; } } - spinlock_release(&g_root_scr.lock); return sio->len; } diff --git a/sys/dev/random/chacha20.c b/sys/dev/random/chacha20.c new file mode 100644 index 0000000..41f823c --- /dev/null +++ b/sys/dev/random/chacha20.c @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <dev/random/chacha20.h> + +static const char sigma[16] = "expand 32-byte k"; + +void chacha20_init(uint32_t state[16], const uint8_t key[32], + const uint8_t nonce[12], uint32_t counter) +{ + state[0] = ((uint32_t *)sigma)[0]; + state[1] = ((uint32_t *)sigma)[1]; + state[2] = ((uint32_t *)sigma)[2]; + state[3] = ((uint32_t *)sigma)[3]; + + for (int i = 0; i < 8; ++i) { + state[4 + i] = ((uint32_t *)key)[i]; + } + + state[12] = counter; + state[13] = ((uint32_t *)nonce)[0]; + state[14] = ((uint32_t *)nonce)[1]; + state[15] = ((uint32_t *)nonce)[2]; +} + +void +chacha20_block(uint32_t state[16], uint8_t out[64]) +{ + uint32_t x[16]; + memcpy(x, state, sizeof(x)); + + for (int i = 0; i < 10; i++) { + + QR(x[0], x[4], x[8], x[12]); + QR(x[1], x[5], x[9], x[13]); + QR(x[2], x[6], x[10], x[14]); + QR(x[3], x[7], x[11], x[15]); + + QR(x[0], x[5], x[10], x[15]); + QR(x[1], x[6], x[11], x[12]); + QR(x[2], x[7], x[8], x[13]); + QR(x[3], x[4], x[9], x[14]); + } + + for (int i = 0; i < 16; ++i) { + x[i] += state[i]; + ((uint32_t *)out)[i] = x[i]; + } + + state[12]++; +} + +void +chacha20_encrypt(uint32_t state[16], uint8_t *in, + uint8_t *out, size_t len) +{ + uint8_t block[64]; + size_t offset = 0; + + while (len > 0) { + chacha20_block(state, block); + size_t n = len > 64 ? 64 : len; + + for (size_t i = 0; i < n; ++i) { + out[offset + i] = in ? in[offset + i] ^ block[i] : block[i]; + } + + offset += n; + len -= n; + } +} diff --git a/sys/dev/random/entropy.c b/sys/dev/random/entropy.c new file mode 100644 index 0000000..d392b9c --- /dev/null +++ b/sys/dev/random/entropy.c @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <stdint.h> +#include <string.h> +#include <dev/random/entropy.h> +#include <dev/random/siphash.h> + +void +mix_entropy(struct entropy_pool *ep, const uint8_t *input, + size_t input_len, uint32_t input_entropy_bits) +{ + char key[16] = {0,1,2,3,4,5,6,7,8,9,0xa,0xb,0xc,0xd,0xe,0xf}; + uint64_t hash_result; + uint8_t buffer[ENTROPY_POOL_SIZE + input_len]; + memcpy(buffer, ep->pool, ENTROPY_POOL_SIZE); + memcpy(buffer + ENTROPY_POOL_SIZE, input, input_len); + + hash_result = siphash24(buffer, sizeof(buffer), key); + + for (int i = 0; i < 8; ++i) { + ep->pool[i] ^= (hash_result >> (i * 8)) & 0xFF; + } + + ep->entropy_bits += input_entropy_bits; + if (ep->entropy_bits > ENTROPY_POOL_SIZE * 8) { + ep->entropy_bits = ENTROPY_POOL_SIZE * 8; + } +} diff --git a/sys/dev/random/random.c b/sys/dev/random/random.c new file mode 100644 index 0000000..d79df69 --- /dev/null +++ b/sys/dev/random/random.c @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/sio.h> +#include <sys/device.h> +#include <sys/driver.h> +#include <dev/random/chacha20.h> +#include <dev/random/siphash.h> +#include <dev/random/entropy.h> +#include <fs/devfs.h> +#include <string.h> + +static struct cdevsw random_cdevsw; +static struct entropy_pool entropy; + +uint8_t key[32] = {0}; +uint8_t nonce[12] = {0}; +uint32_t state[16]; +uint32_t tsc; + +static inline uint64_t +read_tsc(void) +{ + uint32_t lo, hi; + __asm__ volatile ("rdtsc" : "=a"(lo), "=d"(hi)); + return ((uint64_t)hi << 32) | lo; +} + +static int +random_read(dev_t dev, struct sio_txn *sio, int flags) +{ + tsc = read_tsc(); + mix_entropy(&entropy, (uint8_t *)&tsc, sizeof(tsc), 1); + + chacha20_init(state, entropy.pool, nonce, 0); + chacha20_encrypt(state, NULL, sio->buf, sio->len); + + return sio->len; +} + +static int +random_init(void) +{ + char devname[] = "random"; + devmajor_t major; + dev_t dev; + + /* Register the device here */ + major = dev_alloc_major(); + dev = dev_alloc(major); + dev_register(major, dev, &random_cdevsw); + devfs_create_entry(devname, major, dev, 0444); + + return 0; +} + +static struct cdevsw random_cdevsw = { + .read = random_read, + .write = nowrite +}; + +DRIVER_EXPORT(random_init, "random"); diff --git a/sys/dev/random/siphash.c b/sys/dev/random/siphash.c new file mode 100644 index 0000000..2b2243f --- /dev/null +++ b/sys/dev/random/siphash.c @@ -0,0 +1,116 @@ +/* <MIT License> + Copyright (c) 2013 Marek Majkowski <marek@popcount.org> + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + </MIT License> + + Original location: + https://github.com/majek/csiphash/ + + Solution inspired by code from: + Samuel Neves (supercop/crypto_auth/siphash24/little) + djb (supercop/crypto_auth/siphash24/little2) + Jean-Philippe Aumasson (https://131002.net/siphash/siphash24.c) +*/ + +#include <dev/random/siphash.h> +#include <stdint.h> + +#if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \ + __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ +# define _le64toh(x) ((uint64_t)(x)) +#elif defined(_WIN32) +/* Windows is always little endian, unless you're on xbox360 + http://msdn.microsoft.com/en-us/library/b0084kay(v=vs.80).aspx */ +# define _le64toh(x) ((uint64_t)(x)) +#elif defined(__APPLE__) +# include <libkern/OSByteOrder.h> +# define _le64toh(x) OSSwapLittleToHostInt64(x) +#else + +/* See: http://sourceforge.net/p/predef/wiki/Endianness/ */ +# if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) +# include <sys/endian.h> +# else +# include <endian.h> +# endif +# if defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \ + __BYTE_ORDER == __LITTLE_ENDIAN +# define _le64toh(x) ((uint64_t)(x)) +# else +# define _le64toh(x) le64toh(x) +# endif + +#endif + + +#define ROTATE(x, b) (uint64_t)( ((x) << (b)) | ( (x) >> (64 - (b))) ) + +#define HALF_ROUND(a,b,c,d,s,t) \ + a += b; c += d; \ + b = ROTATE(b, s) ^ a; \ + d = ROTATE(d, t) ^ c; \ + a = ROTATE(a, 32); + +#define DOUBLE_ROUND(v0,v1,v2,v3) \ + HALF_ROUND(v0,v1,v2,v3,13,16); \ + HALF_ROUND(v2,v1,v0,v3,17,21); \ + HALF_ROUND(v0,v1,v2,v3,13,16); \ + HALF_ROUND(v2,v1,v0,v3,17,21); + + +uint64_t siphash24(const void *src, unsigned long src_sz, const char key[16]) { + const uint64_t *_key = (uint64_t *)key; + uint64_t k0 = _le64toh(_key[0]); + uint64_t k1 = _le64toh(_key[1]); + uint64_t b = (uint64_t)src_sz << 56; + const uint64_t *in = (uint64_t*)src; + + uint64_t v0 = k0 ^ 0x736f6d6570736575ULL; + uint64_t v1 = k1 ^ 0x646f72616e646f6dULL; + uint64_t v2 = k0 ^ 0x6c7967656e657261ULL; + uint64_t v3 = k1 ^ 0x7465646279746573ULL; + + while (src_sz >= 8) { + uint64_t mi = _le64toh(*in); + in += 1; src_sz -= 8; + v3 ^= mi; + DOUBLE_ROUND(v0,v1,v2,v3); + v0 ^= mi; + } + + uint64_t t = 0; uint8_t *pt = (uint8_t *)&t; uint8_t *m = (uint8_t *)in; + switch (src_sz) { + case 7: pt[6] = m[6]; + case 6: pt[5] = m[5]; + case 5: pt[4] = m[4]; + case 4: *((uint32_t*)&pt[0]) = *((uint32_t*)&m[0]); break; + case 3: pt[2] = m[2]; + case 2: pt[1] = m[1]; + case 1: pt[0] = m[0]; + } + b |= _le64toh(t); + + v3 ^= b; + DOUBLE_ROUND(v0,v1,v2,v3); + v0 ^= b; v2 ^= 0xff; + DOUBLE_ROUND(v0,v1,v2,v3); + DOUBLE_ROUND(v0,v1,v2,v3); + return (v0 ^ v1) ^ (v2 ^ v3); +} diff --git a/sys/fs/initramfs.c b/sys/fs/initramfs.c index c41deb4..beb2e84 100644 --- a/sys/fs/initramfs.c +++ b/sys/fs/initramfs.c @@ -61,12 +61,16 @@ struct initramfs_node { * @magic: Header magic ("OMAR") * @len: Length of the file * @namelen: Length of the filename + * @rev: OMAR revision + * @mode: File permissions */ struct __packed omar_hdr { char magic[4]; uint8_t type; uint8_t namelen; uint32_t len; + uint8_t rev; + uint32_t mode; }; static volatile struct limine_module_request mod_req = { @@ -140,7 +144,7 @@ initramfs_get_file(const char *path, struct initramfs_node *res) p += hdr->namelen; if (strcmp(namebuf, path) == 0) { - node.mode = 0700; + node.mode = hdr->mode; node.size = hdr->len; node.data = (void *)p; *res = node; diff --git a/sys/include/dev/random/chacha20.h b/sys/include/dev/random/chacha20.h new file mode 100644 index 0000000..d35702a --- /dev/null +++ b/sys/include/dev/random/chacha20.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <stdint.h> +#include <stddef.h> +#include <string.h> + +#define ROTL(a,b) (((a) << (b)) | ((a) >> (32 - (b)))) + +#define QR(a,b,c,d) \ + a += b; d ^= a; d = ROTL(d, 16); \ + c += d; b ^= c; b = ROTL(b, 12); \ + a += b; d ^= a; d = ROTL(d, 8); \ + c += d; b ^= c; b = ROTL(b, 7); + +void chacha20_init(uint32_t state[16], const uint8_t key[32], + const uint8_t nonce[12], uint32_t counter); + +void chacha20_block(uint32_t state[16], uint8_t out[64]); +void chacha20_encrypt(uint32_t state[16], uint8_t *in, uint8_t *out, size_t len); + diff --git a/sys/include/dev/random/entropy.h b/sys/include/dev/random/entropy.h new file mode 100644 index 0000000..34d86df --- /dev/null +++ b/sys/include/dev/random/entropy.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <stdint.h> + +#define ENTROPY_POOL_SIZE 32 + +struct entropy_pool { + uint8_t pool[ENTROPY_POOL_SIZE]; + uint32_t entropy_bits; +}; + +void mix_entropy(struct entropy_pool *ep, const uint8_t *input, + size_t input_len, uint32_t input_entropy_bits); diff --git a/sys/include/dev/random/siphash.h b/sys/include/dev/random/siphash.h new file mode 100644 index 0000000..ecabb4a --- /dev/null +++ b/sys/include/dev/random/siphash.h @@ -0,0 +1,34 @@ +/* <MIT License> + Copyright (c) 2013 Marek Majkowski <marek@popcount.org> + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + </MIT License> + + Original location: + https://github.com/majek/csiphash/ + + Solution inspired by code from: + Samuel Neves (supercop/crypto_auth/siphash24/little) + djb (supercop/crypto_auth/siphash24/little2) + Jean-Philippe Aumasson (https://131002.net/siphash/siphash24.c) +*/ + +#include <stdint.h> + +uint64_t siphash24(const void *src, unsigned long src_sz, const char k[16]); diff --git a/sys/include/sys/limits.h b/sys/include/sys/limits.h index f56958e..5b97b68 100644 --- a/sys/include/sys/limits.h +++ b/sys/include/sys/limits.h @@ -31,6 +31,7 @@ #define _SYS_LIMITS_H_ #define PATH_MAX 1024 +#define NAME_MAX 256 #define SSIZE_MAX 32767 #define ARG_MAX 4096 #define CHAR_BIT 8 diff --git a/sys/include/sys/syscall.h b/sys/include/sys/syscall.h index 05288f4..51c2579 100644 --- a/sys/include/sys/syscall.h +++ b/sys/include/sys/syscall.h @@ -57,6 +57,7 @@ #define SYS_getpid 16 #define SYS_getppid 17 #define SYS_setuid 18 +#define SYS_getuid 19 #if defined(_KERNEL) /* Syscall return value and arg type */ diff --git a/sys/include/sys/ucred.h b/sys/include/sys/ucred.h index b44a7fe..f8cbbe0 100644 --- a/sys/include/sys/ucred.h +++ b/sys/include/sys/ucred.h @@ -48,8 +48,10 @@ struct ucred { }; int setuid(uid_t new); +uid_t getuid(void); #if defined(_KERNEL) scret_t sys_setuid(struct syscall_args *scargs); +scret_t sys_getuid(struct syscall_args *scargs); #endif #endif /* !_SYS_UCRED_H_ */ diff --git a/sys/include/sys/vnode.h b/sys/include/sys/vnode.h index b135433..3402b02 100644 --- a/sys/include/sys/vnode.h +++ b/sys/include/sys/vnode.h @@ -92,6 +92,16 @@ struct vop_create_args { struct vnode **vpp; /* Result vnode */ }; +struct vop_getattr_args { + struct vnode *vp; /* Target vnode */ + struct vattr *res; /* Result vattr */ +}; + +struct vop_readdir_args { + struct vnode *vp; /* Target vnode */ + struct sio_txn *sio; /* SIO data to read into */ +}; + /* * A field in this structure is unavailable * if it has a value of VNOVAL. @@ -101,14 +111,10 @@ struct vattr { size_t size; }; -struct vop_getattr_args { - struct vnode *vp; - struct vattr *res; -}; - struct vops { int(*lookup)(struct vop_lookup_args *args); int(*getattr)(struct vop_getattr_args *args); + int(*readdir)(struct vop_readdir_args *args); int(*read)(struct vnode *vp, struct sio_txn *sio); int(*write)(struct vnode *vp, struct sio_txn *sio); int(*reclaim)(struct vnode *vp); @@ -117,19 +123,21 @@ struct vops { extern struct vnode *g_root_vnode; +/* Vnode cache operations */ int vfs_vcache_type(void); int vfs_vcache_migrate(int newtype); - int vfs_vcache_enter(struct vnode *vp); struct vnode *vfs_recycle_vnode(void); +/* Vnode operations */ int vfs_alloc_vnode(struct vnode **res, int type); int vfs_release_vnode(struct vnode *vp); -int vfs_vop_lookup(struct vnode *vp, struct vop_lookup_args *args); +/* Vnode operation wrappers */ +int vfs_vop_lookup(struct vop_lookup_args *args); +int vfs_vop_getattr(struct vop_getattr_args *args); int vfs_vop_read(struct vnode *vp, struct sio_txn *sio); int vfs_vop_write(struct vnode *vp, struct sio_txn *sio); -int vfs_vop_getattr(struct vnode *vp, struct vop_getattr_args *args); #endif /* _KERNEL */ #endif /* !_SYS_VNODE_H_ */ diff --git a/sys/kern/exec_elf64.c b/sys/kern/exec_elf64.c index 9706e77..8dc87dc 100644 --- a/sys/kern/exec_elf64.c +++ b/sys/kern/exec_elf64.c @@ -112,7 +112,7 @@ elf_get_file(const char *pathname, struct elf_file *res) getattr_args.res = &vattr; getattr_args.vp = vp; - status = vfs_vop_getattr(vp, &getattr_args); + status = vfs_vop_getattr(&getattr_args); if (status != 0) goto done; diff --git a/sys/kern/kern_cred.c b/sys/kern/kern_cred.c index e1202fc..017b22a 100644 --- a/sys/kern/kern_cred.c +++ b/sys/kern/kern_cred.c @@ -56,6 +56,19 @@ setuid(uid_t new) return 0; } +uid_t +getuid(void) +{ + struct proc *td; + + td = this_td(); + if (td == NULL) { + return -1; + } + + return td->cred.ruid; +} + /* * setuid() syscall * @@ -66,3 +79,9 @@ sys_setuid(struct syscall_args *scargs) { return setuid(scargs->arg0); } + +scret_t +sys_getuid(struct syscall_args *scargs) +{ + return getuid(); +} diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c index 0fb026f..b5ff144 100644 --- a/sys/kern/kern_descrip.c +++ b/sys/kern/kern_descrip.c @@ -351,7 +351,7 @@ fd_seek(int fildes, off_t offset, int whence) getattr_args.vp = tmp->vp; getattr_args.res = &attr; - if ((vfs_vop_getattr(tmp->vp, &getattr_args)) < 0) { + if ((vfs_vop_getattr(&getattr_args)) < 0) { return -EPIPE; } diff --git a/sys/kern/kern_exit.c b/sys/kern/kern_exit.c index a4bf0f9..b1bd9ba 100644 --- a/sys/kern/kern_exit.c +++ b/sys/kern/kern_exit.c @@ -31,6 +31,8 @@ #include <sys/sched.h> #include <sys/syslog.h> #include <sys/panic.h> +#include <sys/filedesc.h> +#include <sys/vnode.h> #include <vm/physmem.h> #include <vm/dynalloc.h> #include <vm/vm.h> @@ -83,9 +85,23 @@ void proc_reap(struct proc *td) { struct pcb *pcbp; + struct filedesc *fdp; vaddr_t stack_va; paddr_t stack_pa; + /* Clear out all fds */ + for (size_t i = 4; i < PROC_MAX_FILEDES; ++i) { + fdp = td->fds[i]; + if (fdp == NULL) { + continue; + } + if (fdp->refcnt == 1) { + vfs_release_vnode(fdp->vp); + dynfree(fdp); + fdp = NULL; + } + } + pcbp = &td->pcb; unload_td(td); diff --git a/sys/kern/kern_syscall.c b/sys/kern/kern_syscall.c index 276c7c7..a28d2dd 100644 --- a/sys/kern/kern_syscall.c +++ b/sys/kern/kern_syscall.c @@ -58,6 +58,7 @@ scret_t(*g_sctab[])(struct syscall_args *) = { sys_getpid, /* SYS_getpid */ sys_getppid, /* SYS_getppid */ sys_setuid, /* SYS_setuid */ + sys_getuid, /* SYS_getuid */ }; const size_t MAX_SYSCALLS = NELEM(g_sctab); diff --git a/sys/kern/vfs_lookup.c b/sys/kern/vfs_lookup.c index d88c447..7320102 100644 --- a/sys/kern/vfs_lookup.c +++ b/sys/kern/vfs_lookup.c @@ -181,7 +181,7 @@ namei_mp_search(struct mount *mp, const char *path, struct nameidata *ndp) lookup_args.dirvp = vp; lookup_args.vpp = &vp; - status = vfs_vop_lookup(vp, &lookup_args); + status = vfs_vop_lookup(&lookup_args); dynfree(name); if (status != 0) { @@ -234,7 +234,7 @@ namei(struct nameidata *ndp) lookup_args.name = path; lookup_args.dirvp = g_root_vnode; lookup_args.vpp = &vp; - status = vfs_vop_lookup(lookup_args.dirvp, &lookup_args); + status = vfs_vop_lookup(&lookup_args); /* Did we find it in the root */ if (status == 0) { diff --git a/sys/kern/vfs_subr.c b/sys/kern/vfs_subr.c index da0a4f9..69417d0 100644 --- a/sys/kern/vfs_subr.c +++ b/sys/kern/vfs_subr.c @@ -141,8 +141,9 @@ vfs_release_vnode(struct vnode *vp) } int -vfs_vop_lookup(struct vnode *vp, struct vop_lookup_args *args) +vfs_vop_lookup(struct vop_lookup_args *args) { + const struct vnode *vp = args->dirvp; const struct vops *vops = vp->vops; if (vops == NULL) @@ -180,8 +181,9 @@ vfs_vop_write(struct vnode *vp, struct sio_txn *sio) } int -vfs_vop_getattr(struct vnode *vp, struct vop_getattr_args *args) +vfs_vop_getattr(struct vop_getattr_args *args) { + const struct vnode *vp = args->vp; const struct vops *vops = vp->vops; if (vops == NULL) diff --git a/sys/kern/vfs_syscalls.c b/sys/kern/vfs_syscalls.c index 0d51331..d15ecf1 100644 --- a/sys/kern/vfs_syscalls.c +++ b/sys/kern/vfs_syscalls.c @@ -68,7 +68,7 @@ vfs_dostat(const char *path, struct stat *sbuf) vp = nd.vp; gattr.vp = vp; gattr.res = &attr; - error = vfs_vop_getattr(vp, &gattr); + error = vfs_vop_getattr(&gattr); if (error != 0) { return error; diff --git a/sys/vm/vm_vnode.c b/sys/vm/vm_vnode.c index 27defc9..777b382 100644 --- a/sys/vm/vm_vnode.c +++ b/sys/vm/vm_vnode.c @@ -73,7 +73,7 @@ vn_io(struct vnode *vp, struct vm_page **pgs, unsigned int npages, int rw) args.res = &vattr; c = MAX(vattr.size / DEFAULT_PAGESIZE, 1); - if ((err = vfs_vop_getattr(vp, &args)) != 0) { + if ((err = vfs_vop_getattr(&args)) != 0) { return err; } |