diff --git a/lib/libc/string/memmem.3 b/lib/libc/string/memmem.3 new file mode 100644 index 000000000000..678ae3d07f0c --- /dev/null +++ b/lib/libc/string/memmem.3 @@ -0,0 +1,84 @@ +.\" Copyright (c) 2005 Pascal Gloor +.\" +.\" 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. The name of the author may not be used to endorse or promote +.\" products derived from this software without specific prior written +.\" permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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. +.\" +\" $FreeBSD$ +.\" +.Dd August 24, 2005 +.Dt MEMMEM 3 +.Os +.Sh NAME +.Nm memmem +.Nd locate a byte substring in a byte string +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In string.h +.Ft void * +.Fn memmem "const char *big" "size_t big_len" \ +"const char *little" "size_t little_len" +.Sh DESCRIPTION +The +.Fn memmem +function +locates the first occurrence of the byte string +.Fa little +in the byte string +.Fa big . +.Sh RETURN VALUES +If +.Fa big_len +is smaller than +.Fa little_len , +if +.Fa little_len +is 0, if +.Fa big_len +is 0 or if +.Fa little +occurs nowhere in +.Fa big , +.Dv NULL +is returned; +otherwise a pointer to the first character of the first occurrence of +.Fa little +is returned. +.Sh SEE ALSO +.Xr memchr 3 , +.Xr strchr 3 , +.Xr strstr 3 +.Sh CONFORMING TO +.Fn memmem +is a GNU extension. +.Sh HISTORY +The +.Fn memmem +function first appeared in +.Fx 6.0 . +.Sh AUTHOR +Pascal Gloor +.Sh BUGS +This function was broken in Linux libc up to and including version 5.0.9 +and in GNU libc prior to version 2.1. diff --git a/lib/libc/string/memmem.c b/lib/libc/string/memmem.c new file mode 100644 index 000000000000..0ac0a6dfc80e --- /dev/null +++ b/lib/libc/string/memmem.c @@ -0,0 +1,67 @@ +/*- + * Copyright (c) 2005 Pascal Gloor + * + * 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. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 +__FBSDID("$FreeBSD$"); + +#include + +/* + * Find the first occurrence of the byte string s in byte string l. + */ + +void * +memmem(l, l_len, s, s_len) + const void *l; size_t l_len; + const void *s; size_t s_len; +{ + register char *cur, *last; + const char *cl = (const char *)l; + const char *cs = (const char *)s; + + /* we need something to compare */ + if (l_len == 0 || s_len == 0) + return NULL; + + /* "s" must be smaller or equal to "l" */ + if (l_len < s_len) + return NULL; + + /* special case where s_len == 1 */ + if (s_len == 1) + return memchr(l, (int)*cs, l_len); + + /* the last position where its possible to find "s" in "l" */ + last = (char *)cl + l_len - s_len; + + for (cur = (char *)cl; cur <= last; cur++) + if (cur[0] == cs[0] && memcmp(cur, cs, s_len) == 0) + return cur; + + return NULL; +} diff --git a/lib/libutil/pidfile.c b/lib/libutil/pidfile.c new file mode 100644 index 000000000000..0b8ade1be885 --- /dev/null +++ b/lib/libutil/pidfile.c @@ -0,0 +1,246 @@ +/*- + * Copyright (c) 2005 Pawel Jakub Dawidek + * 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. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS 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 AUTHORS 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 +__FBSDID("$FreeBSD$"); + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +static int _pidfile_remove(struct pidfh *pfh, int freeit); + +static int +pidfile_verify(struct pidfh *pfh) +{ + struct stat sb; + int fd; + + if (pfh == NULL || pfh->pf_fd == -1) + return (EDOOFUS); + /* + * Check remembered descriptor. + */ + if (fstat(pfh->pf_fd, &sb) == -1) + return (errno); + if (sb.st_dev != pfh->pf_dev || sb.st_ino != pfh->pf_ino) + return (EDOOFUS); + return (0); +} + +static int +pidfile_read(const char *path, pid_t *pidptr) +{ + char buf[16], *endptr; + int error, fd, i; + + fd = open(path, O_RDONLY); + if (fd == -1) + return (errno); + + i = read(fd, buf, sizeof(buf) - 1); + error = errno; /* Remember errno in case close() wants to change it. */ + close(fd); + if (i == -1) + return (error); + buf[i] = '\0'; + + *pidptr = strtol(buf, &endptr, 10); + if (endptr != &buf[i]) + return (EINVAL); + + return (0); +} + +struct pidfh * +pidfile_open(const char *path, mode_t mode, pid_t *pidptr) +{ + struct pidfh *pfh; + struct stat sb; + int error, fd; + + pfh = malloc(sizeof(*pfh)); + if (pfh == NULL) + return (NULL); + + if (path == NULL) { + snprintf(pfh->pf_path, sizeof(pfh->pf_path), "/var/run/%s.pid", + getprogname()); + } else { + strlcpy(pfh->pf_path, path, sizeof(pfh->pf_path)); + } + if (strlen(pfh->pf_path) == sizeof(pfh->pf_path) - 1) { + free(pfh); + errno = ENAMETOOLONG; + return (NULL); + } + + /* + * Open the PID file and obtain exclusive lock. + * We truncate PID file here only to remove old PID immediatelly, + * PID file will be truncated again in pidfile_write(), so + * pidfile_write() can be called multiple times. + */ + fd = open(pfh->pf_path, + O_WRONLY | O_CREAT | O_EXLOCK | O_TRUNC | O_NONBLOCK, mode); + if (fd == -1) { + if (errno == EWOULDBLOCK && pidptr != NULL) { + errno = pidfile_read(pfh->pf_path, pidptr); + if (errno == 0) + errno = EEXIST; + } + free(pfh); + return (NULL); + } + /* + * Remember file information, so in pidfile_write() we are sure we write + * to the proper descriptor. + */ + if (fstat(fd, &sb) == -1) { + error = errno; + unlink(pfh->pf_path); + close(fd); + free(pfh); + errno = error; + return (NULL); + } + + pfh->pf_fd = fd; + pfh->pf_dev = sb.st_dev; + pfh->pf_ino = sb.st_ino; + + return (pfh); +} + +int +pidfile_write(struct pidfh *pfh) +{ + struct stat sb; + char pidstr[16]; + int error, fd; + + /* + * Check remembered descriptor, so we don't overwrite some other + * file if pidfile was closed and descriptor reused. + */ + errno = pidfile_verify(pfh); + if (errno != 0) { + /* + * Don't close descriptor, because we are not sure if it's ours. + */ + return (-1); + } + fd = pfh->pf_fd; + + /* + * Truncate PID file, so multiple calls of pidfile_write() are allowed. + */ + if (ftruncate(fd, 0) == -1) { + error = errno; + _pidfile_remove(pfh, 0); + errno = error; + return (-1); + } + + snprintf(pidstr, sizeof(pidstr), "%u", getpid()); + if (write(fd, pidstr, strlen(pidstr)) != (ssize_t)strlen(pidstr)) { + error = errno; + _pidfile_remove(pfh, 0); + errno = error; + return (-1); + } + + return (0); +} + +int +pidfile_close(struct pidfh *pfh) +{ + int error; + + error = pidfile_verify(pfh); + if (error != 0) { + errno = error; + return (-1); + } + + if (close(pfh->pf_fd) == -1) + error = errno; + free(pfh); + if (error != 0) { + errno = error; + return (-1); + } + return (0); +} + +static int +_pidfile_remove(struct pidfh *pfh, int freeit) +{ + int error; + + error = pidfile_verify(pfh); + if (error != 0) { + errno = error; + return (-1); + } + + if (unlink(pfh->pf_path) == -1) + error = errno; + if (flock(pfh->pf_fd, LOCK_UN) == -1) { + if (error == 0) + error = errno; + } + if (close(pfh->pf_fd) == -1) { + if (error == 0) + error = errno; + } + if (freeit) + free(pfh); + else + pfh->pf_fd = -1; + if (error != 0) { + errno = error; + return (-1); + } + return (0); +} + +int +pidfile_remove(struct pidfh *pfh) +{ + + return (_pidfile_remove(pfh, 1)); +} diff --git a/sys/crypto/via/padlock.c b/sys/crypto/via/padlock.c new file mode 100644 index 000000000000..e9b02327e6f7 --- /dev/null +++ b/sys/crypto/via/padlock.c @@ -0,0 +1,504 @@ +/*- + * Copyright (c) 2005 Pawel Jakub Dawidek + * Copyright (c) 2004 Mark R V Murray + * 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. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS 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 AUTHORS 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. + */ + +/* $OpenBSD: via.c,v 1.3 2004/06/15 23:36:55 deraadt Exp $ */ +/*- + * Copyright (c) 2003 Jason Wright + * Copyright (c) 2003, 2004 Theo de Raadt + * All rights reserved. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#if defined(__i386__) && !defined(PC98) +#include +#include +#endif + +#include +#include + + +#define PADLOCK_ROUND_COUNT_AES128 10 +#define PADLOCK_ROUND_COUNT_AES192 12 +#define PADLOCK_ROUND_COUNT_AES256 14 + +#define PADLOCK_ALGORITHM_TYPE_AES 0 + +#define PADLOCK_KEY_GENERATION_HW 0 +#define PADLOCK_KEY_GENERATION_SW 1 + +#define PADLOCK_DIRECTION_ENCRYPT 0 +#define PADLOCK_DIRECTION_DECRYPT 1 + +#define PADLOCK_KEY_SIZE_128 0 +#define PADLOCK_KEY_SIZE_192 1 +#define PADLOCK_KEY_SIZE_256 2 + +union padlock_cw { + uint64_t raw; + struct { + u_int round_count : 4; + u_int algorithm_type : 3; + u_int key_generation : 1; + u_int intermediate : 1; + u_int direction : 1; + u_int key_size : 2; + u_int filler0 : 20; + u_int filler1 : 32; + u_int filler2 : 32; + u_int filler3 : 32; + } __field; +}; +#define cw_round_count __field.round_count +#define cw_algorithm_type __field.algorithm_type +#define cw_key_generation __field.key_generation +#define cw_intermediate __field.intermediate +#define cw_direction __field.direction +#define cw_key_size __field.key_size +#define cw_filler0 __field.filler0 +#define cw_filler1 __field.filler1 +#define cw_filler2 __field.filler2 +#define cw_filler3 __field.filler3 + +struct padlock_session { + union padlock_cw ses_cw __aligned(16); + uint32_t ses_ekey[4 * (RIJNDAEL_MAXNR + 1) + 4] __aligned(16); /* 128 bit aligned */ + uint32_t ses_dkey[4 * (RIJNDAEL_MAXNR + 1) + 4] __aligned(16); /* 128 bit aligned */ + uint8_t ses_iv[16] __aligned(16); /* 128 bit aligned */ + int ses_used; + uint32_t ses_id; + TAILQ_ENTRY(padlock_session) ses_next; +}; + +struct padlock_softc { + int32_t sc_cid; + uint32_t sc_sid; + TAILQ_HEAD(, padlock_session) sc_sessions; + struct mtx sc_sessions_mtx; +}; + +static struct padlock_softc *padlock_sc; + +static int padlock_newsession(void *arg __unused, uint32_t *sidp, + struct cryptoini *cri); +static int padlock_freesession(void *arg __unused, uint64_t tid); +static int padlock_process(void *arg __unused, struct cryptop *crp, + int hint __unused); + +static __inline void +padlock_cbc(void *in, void *out, size_t count, void *key, union padlock_cw *cw, + void *iv) +{ +#ifdef __GNUCLIKE_ASM + /* The .byte line is really VIA C3 "xcrypt-cbc" instruction */ + __asm __volatile( + "pushf \n\t" + "popf \n\t" + "rep \n\t" + ".byte 0x0f, 0xa7, 0xd0" + : "+a" (iv), "+c" (count), "+D" (out), "+S" (in) + : "b" (key), "d" (cw) + : "cc", "memory" + ); +#endif +} + +static int +padlock_init(void) +{ + struct padlock_softc *sc; +#if defined(__i386__) && !defined(PC98) + u_int regs[4]; + int has_ace = 0; + + if (cpu_class < CPUCLASS_586) + return (EINVAL); + do_cpuid(1, regs); + if ((regs[0] & 0xf) >= 3) { + do_cpuid(0xc0000000, regs); + if (regs[0] == 0xc0000001) { + do_cpuid(0xc0000001, regs); + if ((regs[3] & 0xc0) == 0xc0) + has_ace = 1; + } + } + if (!has_ace) { + printf("PADLOCK: No ACE support.\n"); + return (EINVAL); + } +#else + return (EINVAL); +#endif + + padlock_sc = sc = malloc(sizeof(*padlock_sc), M_DEVBUF, + M_NOWAIT | M_ZERO); + if (padlock_sc == NULL) { + printf("PADLOCK: Could not allocate memory.\n"); + return (ENOMEM); + } + TAILQ_INIT(&sc->sc_sessions); + sc->sc_sid = 1; + + sc->sc_cid = crypto_get_driverid(0); + if (sc->sc_cid < 0) { + printf("PADLOCK: Could not get crypto driver id.\n"); + free(padlock_sc, M_DEVBUF); + padlock_sc = NULL; + return (ENOMEM); + } + + mtx_init(&sc->sc_sessions_mtx, "padlock_mtx", NULL, MTX_DEF); + crypto_register(sc->sc_cid, CRYPTO_AES_CBC, 0, 0, padlock_newsession, + padlock_freesession, padlock_process, NULL); + return (0); +} + +static int +padlock_destroy(void) +{ + struct padlock_softc *sc = padlock_sc; + struct padlock_session *ses; + u_int active = 0; + + if (sc == NULL) + return (0); + mtx_lock(&sc->sc_sessions_mtx); + TAILQ_FOREACH(ses, &sc->sc_sessions, ses_next) { + if (ses->ses_used) + active++; + } + if (active > 0) { + mtx_unlock(&sc->sc_sessions_mtx); + printf("PADLOCK: Cannot destroy, %u sessions active.\n", + active); + return (EBUSY); + } + padlock_sc = NULL; + for (ses = TAILQ_FIRST(&sc->sc_sessions); ses != NULL; + ses = TAILQ_FIRST(&sc->sc_sessions)) { + TAILQ_REMOVE(&sc->sc_sessions, ses, ses_next); + free(ses, M_DEVBUF); + } + mtx_destroy(&sc->sc_sessions_mtx); + crypto_unregister_all(sc->sc_cid); + free(sc, M_DEVBUF); + return (0); +} + +static int +padlock_newsession(void *arg __unused, uint32_t *sidp, struct cryptoini *cri) +{ + struct padlock_softc *sc = padlock_sc; + struct padlock_session *ses = NULL; + union padlock_cw *cw; + int i; + + if (sc == NULL || sidp == NULL || cri == NULL || + cri->cri_next != NULL || cri->cri_alg != CRYPTO_AES_CBC) { + return (EINVAL); + } + if (cri->cri_klen != 128 && cri->cri_klen != 192 && + cri->cri_klen != 256) { + return (EINVAL); + } + + /* + * Let's look for a free session structure. + */ + mtx_lock(&sc->sc_sessions_mtx); + /* + * Free sessions goes first, so if first session is used, we need to + * allocate one. + */ + ses = TAILQ_FIRST(&sc->sc_sessions); + if (ses == NULL || ses->ses_used) + ses = NULL; + else { + TAILQ_REMOVE(&sc->sc_sessions, ses, ses_next); + ses->ses_used = 1; + TAILQ_INSERT_TAIL(&sc->sc_sessions, ses, ses_next); + } + mtx_unlock(&sc->sc_sessions_mtx); + if (ses == NULL) { + ses = malloc(sizeof(*ses), M_DEVBUF, M_NOWAIT | M_ZERO); + if (ses == NULL) + return (ENOMEM); + ses->ses_used = 1; + mtx_lock(&sc->sc_sessions_mtx); + ses->ses_id = sc->sc_sid++; + TAILQ_INSERT_TAIL(&sc->sc_sessions, ses, ses_next); + mtx_unlock(&sc->sc_sessions_mtx); + } + + cw = &ses->ses_cw; + bzero(cw, sizeof(*cw)); + cw->cw_algorithm_type = PADLOCK_ALGORITHM_TYPE_AES; + cw->cw_key_generation = PADLOCK_KEY_GENERATION_SW; + cw->cw_intermediate = 0; + switch (cri->cri_klen) { + case 128: + cw->cw_round_count = PADLOCK_ROUND_COUNT_AES128; + cw->cw_key_size = PADLOCK_KEY_SIZE_128; +#ifdef HW_KEY_GENERATION + /* This doesn't buy us much, that's why it is commented out. */ + cw->cw_key_generation = PADLOCK_KEY_GENERATION_HW; +#endif + break; + case 192: + cw->cw_round_count = PADLOCK_ROUND_COUNT_AES192; + cw->cw_key_size = PADLOCK_KEY_SIZE_192; + break; + case 256: + cw->cw_round_count = PADLOCK_ROUND_COUNT_AES256; + cw->cw_key_size = PADLOCK_KEY_SIZE_256; + break; + } + + arc4rand(ses->ses_iv, sizeof(ses->ses_iv), 0); + + if (cw->cw_key_generation == PADLOCK_KEY_GENERATION_SW) { + /* Build expanded keys for both directions */ + rijndaelKeySetupEnc(ses->ses_ekey, cri->cri_key, cri->cri_klen); + rijndaelKeySetupDec(ses->ses_dkey, cri->cri_key, cri->cri_klen); + for (i = 0; i < 4 * (RIJNDAEL_MAXNR + 1); i++) { + ses->ses_ekey[i] = ntohl(ses->ses_ekey[i]); + ses->ses_dkey[i] = ntohl(ses->ses_dkey[i]); + } + } else { + bcopy(cri->cri_key, ses->ses_ekey, cri->cri_klen); + bcopy(cri->cri_key, ses->ses_dkey, cri->cri_klen); + } + + *sidp = ses->ses_id; + return (0); +} + +static int +padlock_freesession(void *arg __unused, uint64_t tid) +{ + struct padlock_softc *sc = padlock_sc; + struct padlock_session *ses; + uint32_t sid = ((uint32_t)tid) & 0xffffffff; + + if (sc == NULL) + return (EINVAL); + mtx_lock(&sc->sc_sessions_mtx); + TAILQ_FOREACH(ses, &sc->sc_sessions, ses_next) { + if (ses->ses_id == sid) + break; + } + if (ses == NULL) { + mtx_unlock(&sc->sc_sessions_mtx); + return (EINVAL); + } + TAILQ_REMOVE(&sc->sc_sessions, ses, ses_next); + bzero(ses, sizeof(ses)); + ses->ses_used = 0; + TAILQ_INSERT_TAIL(&sc->sc_sessions, ses, ses_next); + mtx_unlock(&sc->sc_sessions_mtx); + return (0); +} + +static int +padlock_process(void *arg __unused, struct cryptop *crp, int hint __unused) +{ + struct padlock_softc *sc = padlock_sc; + struct padlock_session *ses; + union padlock_cw *cw; + struct cryptodesc *crd = NULL; + uint32_t *key; + u_char *buf, *abuf; + int err = 0; + + buf = NULL; + if (crp == NULL || crp->crp_callback == NULL) { + err = EINVAL; + goto out; + } + crd = crp->crp_desc; + if (crd == NULL || crd->crd_next != NULL || + crd->crd_alg != CRYPTO_AES_CBC || + (crd->crd_len % 16) != 0) { + err = EINVAL; + goto out; + } + + mtx_lock(&sc->sc_sessions_mtx); + TAILQ_FOREACH(ses, &sc->sc_sessions, ses_next) { + if (ses->ses_id == (crp->crp_sid & 0xffffffff)) + break; + } + mtx_unlock(&sc->sc_sessions_mtx); + if (ses == NULL) { + err = EINVAL; + goto out; + } + + buf = malloc(crd->crd_len + 16, M_DEVBUF, M_NOWAIT); + if (buf == NULL) { + err = ENOMEM; + goto out; + } + abuf = buf + 16 - ((uintptr_t)buf % 16); + + cw = &ses->ses_cw; + cw->cw_filler0 = 0; + cw->cw_filler1 = 0; + cw->cw_filler2 = 0; + cw->cw_filler3 = 0; + if ((crd->crd_flags & CRD_F_ENCRYPT) != 0) { + cw->cw_direction = PADLOCK_DIRECTION_ENCRYPT; + key = ses->ses_ekey; + if ((crd->crd_flags & CRD_F_IV_EXPLICIT) != 0) + bcopy(crd->crd_iv, ses->ses_iv, 16); + + if ((crd->crd_flags & CRD_F_IV_PRESENT) == 0) { + if ((crp->crp_flags & CRYPTO_F_IMBUF) != 0) { + m_copyback((struct mbuf *)crp->crp_buf, + crd->crd_inject, 16, ses->ses_iv); + } else if ((crp->crp_flags & CRYPTO_F_IOV) != 0) { + cuio_copyback((struct uio *)crp->crp_buf, + crd->crd_inject, 16, ses->ses_iv); + } else { + bcopy(ses->ses_iv, + crp->crp_buf + crd->crd_inject, 16); + } + } + } else { + cw->cw_direction = PADLOCK_DIRECTION_DECRYPT; + key = ses->ses_dkey; + if ((crd->crd_flags & CRD_F_IV_EXPLICIT) != 0) + bcopy(crd->crd_iv, ses->ses_iv, 16); + else { + if ((crp->crp_flags & CRYPTO_F_IMBUF) != 0) { + m_copydata((struct mbuf *)crp->crp_buf, + crd->crd_inject, 16, ses->ses_iv); + } else if ((crp->crp_flags & CRYPTO_F_IOV) != 0) { + cuio_copydata((struct uio *)crp->crp_buf, + crd->crd_inject, 16, ses->ses_iv); + } else { + bcopy(crp->crp_buf + crd->crd_inject, + ses->ses_iv, 16); + } + } + } + + if ((crp->crp_flags & CRYPTO_F_IMBUF) != 0) { + m_copydata((struct mbuf *)crp->crp_buf, crd->crd_skip, + crd->crd_len, abuf); + } else if ((crp->crp_flags & CRYPTO_F_IOV) != 0) { + cuio_copydata((struct uio *)crp->crp_buf, crd->crd_skip, + crd->crd_len, abuf); + } else { + bcopy(crp->crp_buf + crd->crd_skip, abuf, crd->crd_len); + } + + padlock_cbc(abuf, abuf, crd->crd_len / 16, key, cw, ses->ses_iv); + + if ((crp->crp_flags & CRYPTO_F_IMBUF) != 0) { + m_copyback((struct mbuf *)crp->crp_buf, crd->crd_skip, + crd->crd_len, abuf); + } else if ((crp->crp_flags & CRYPTO_F_IOV) != 0) { + cuio_copyback((struct uio *)crp->crp_buf, crd->crd_skip, + crd->crd_len, abuf); + } else { + bcopy(abuf, crp->crp_buf + crd->crd_skip, crd->crd_len); + } + + /* copy out last block for use as next session IV */ + if ((crd->crd_flags & CRD_F_ENCRYPT) != 0) { + if ((crp->crp_flags & CRYPTO_F_IMBUF) != 0) { + m_copydata((struct mbuf *)crp->crp_buf, + crd->crd_skip + crd->crd_len - 16, 16, ses->ses_iv); + } else if ((crp->crp_flags & CRYPTO_F_IOV) != 0) { + cuio_copydata((struct uio *)crp->crp_buf, + crd->crd_skip + crd->crd_len - 16, 16, ses->ses_iv); + } else { + bcopy(crp->crp_buf + crd->crd_skip + crd->crd_len - 16, + ses->ses_iv, 16); + } + } + +out: + if (buf != NULL) { + bzero(buf, crd->crd_len + 16); + free(buf, M_DEVBUF); + } + crp->crp_etype = err; + crypto_done(crp); + return (err); +} + +static int +padlock_modevent(module_t mod, int type, void *unused __unused) +{ + int error; + + error = EOPNOTSUPP; + switch (type) { + case MOD_LOAD: + error = padlock_init(); + break; + case MOD_UNLOAD: + error = padlock_destroy(); + break; + } + return (error); +} + +static moduledata_t padlock_mod = { + "padlock", + padlock_modevent, + 0 +}; +DECLARE_MODULE(padlock, padlock_mod, SI_SUB_DRIVERS, SI_ORDER_ANY); +MODULE_VERSION(padlock, 1); +MODULE_DEPEND(padlock, crypto, 1, 1, 1); diff --git a/sys/kern/subr_stack.c b/sys/kern/subr_stack.c new file mode 100644 index 000000000000..47dd2058d2a7 --- /dev/null +++ b/sys/kern/subr_stack.c @@ -0,0 +1,169 @@ +/*- + * Copyright (c) 2005 Antoine Brodin + * 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. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 +__FBSDID("$FreeBSD$"); + +#include +#include +#ifdef KTR +#include +#endif +#include +#include +#include +#include +#include + +MALLOC_DEFINE(M_STACK, "stack", "Stack Traces"); + +static void stack_symbol(vm_offset_t pc, const char **name, long *offset); + +struct stack * +stack_create(void) +{ + struct stack *st; + + st = malloc(sizeof *st, M_STACK, M_WAITOK | M_ZERO); + return (st); +} + +void +stack_destroy(struct stack *st) +{ + + free(st, M_STACK); +} + +int +stack_put(struct stack *st, vm_offset_t pc) +{ + + if (st->depth < STACK_MAX) { + st->pcs[st->depth++] = pc; + return (0); + } else + return (-1); +} + +void +stack_copy(struct stack *src, struct stack *dst) +{ + + *dst = *src; +} + +void +stack_zero(struct stack *st) +{ + + bzero(st, sizeof *st); +} + +void +stack_print(struct stack *st) +{ + const char *name; + long offset; + int i; + + KASSERT(st->depth <= STACK_MAX, ("bogous stack")); + for (i = 0; i < st->depth; i++) { + stack_symbol(st->pcs[i], &name, &offset); + printf("#%d %p at %s+%#lx\n", i, (void *)st->pcs[i], + name, offset); + } +} + +void +stack_sbuf_print(struct sbuf *sb, struct stack *st) +{ + const char *name; + long offset; + int i; + + KASSERT(st->depth <= STACK_MAX, ("bogous stack")); + for (i = 0; i < st->depth; i++) { + stack_symbol(st->pcs[i], &name, &offset); + sbuf_printf(sb, "#%d %p at %s+%#lx\n", i, (void *)st->pcs[i], + name, offset); + } +} + +#ifdef KTR +void +stack_ktr(u_int mask, const char *file, int line, struct stack *st, u_int depth, + int cheap) +{ + const char *name; + long offset; + int i; + + KASSERT(st->depth <= STACK_MAX, ("bogous stack")); + if (cheap) { + ktr_tracepoint(mask, file, line, "#0 %p %p %p %p %p %p", + st->pcs[0], st->pcs[1], st->pcs[2], st->pcs[3], + st->pcs[4], st->pcs[5]); + if (st->depth <= 6) + return; + ktr_tracepoint(mask, file, line, "#1 %p %p %p %p %p %p", + st->pcs[6], st->pcs[7], st->pcs[8], st->pcs[9], + st->pcs[10], st->pcs[11]); + if (st->depth <= 12) + return; + ktr_tracepoint(mask, file, line, "#2 %p %p %p %p %p %p", + st->pcs[12], st->pcs[13], st->pcs[14], st->pcs[15], + st->pcs[16], st->pcs[17]); + } else { + if (depth == 0 || st->depth < depth) + depth = st->depth; + for (i = 0; i < depth; i++) { + stack_symbol(st->pcs[i], &name, &offset); + ktr_tracepoint(mask, file, line, "#%d %p at %s+%#lx", + i, st->pcs[i], (u_long)name, offset, 0, 0); + } + } +} +#endif + +static void +stack_symbol(vm_offset_t pc, const char **name, long *offset) +{ + linker_symval_t symval; + c_linker_sym_t sym; + + if (linker_ddb_search_symbol((caddr_t)pc, &sym, offset) != 0) + goto out; + if (linker_ddb_symbol_values(sym, &symval) != 0) + goto out; + if (symval.name != NULL) { + *name = symval.name; + return; + } +out: + *offset = 0; + *name = "Unknown func"; +} diff --git a/sys/modules/padlock/Makefile b/sys/modules/padlock/Makefile new file mode 100644 index 000000000000..41d9a53924b0 --- /dev/null +++ b/sys/modules/padlock/Makefile @@ -0,0 +1,8 @@ +# $FreeBSD$ + +.PATH: ${.CURDIR}/../../crypto/via + +KMOD= padlock +SRCS= padlock.c + +.include diff --git a/sys/sys/stack.h b/sys/sys/stack.h new file mode 100644 index 000000000000..a7ac114537be --- /dev/null +++ b/sys/sys/stack.h @@ -0,0 +1,62 @@ +/*- + * Copyright (c) 2005 Antoine Brodin + * 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. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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. + * + * $FreeBSD$ + */ + +#ifndef _SYS_STACK_H_ +#define _SYS_STACK_H_ + +#define STACK_MAX 18 /* Don't change, stack_ktr relies on this. */ + +struct sbuf; + +struct stack { + int depth; + vm_offset_t pcs[STACK_MAX]; +}; + +/* MI Routines. */ +struct stack *stack_create(void); +void stack_destroy(struct stack *); +int stack_put(struct stack *, vm_offset_t); +void stack_copy(struct stack *, struct stack *); +void stack_zero(struct stack *); +void stack_print(struct stack *); +void stack_sbuf_print(struct sbuf *, struct stack *); +#ifdef KTR +void stack_ktr(u_int, const char *, int, struct stack *, u_int, int); +#define CTRSTACK(m, st, depth, cheap) do { \ + if (KTR_COMPILE & (m)) \ + stack_ktr((m), __FILE__, __LINE__, st, depth, cheap); \ + } while(0) +#else +#define CTRSTACK(m, st, depth, cheap) +#endif + +/* MD Routine. */ +void stack_save(struct stack *); + +#endif diff --git a/tools/regression/usr.bin/pkill/pgrep-LF.t b/tools/regression/usr.bin/pkill/pgrep-LF.t new file mode 100644 index 000000000000..d981175fb956 --- /dev/null +++ b/tools/regression/usr.bin/pkill/pgrep-LF.t @@ -0,0 +1,41 @@ +#!/bin/sh +# $FreeBSD$ + +base=`basename $0` + +echo "1..2" + +name="pgrep -LF " +pidfile=`mktemp /tmp/$base.XXXXXX` || exit 1 +sleep=`mktemp /tmp/$base.XXXXXX` || exit 1 +ln -sf /bin/sleep $sleep +daemon -p $pidfile $sleep 5 +sleep 0.3 +chpid=`cat $pidfile` +pid=`pgrep -f -L -F $pidfile $sleep` +if [ "$pid" = "$chpid" ]; then + echo "ok 1 - $name" +else + echo "not ok 1 - $name" +fi +kill "$chpid" + +# Be sure we cannot find process which pidfile is not locked. +$sleep 5 & +sleep 0.3 +chpid=$! +echo $chpid > $pidfile +pgrep -f -L -F $pidfile $sleep 2>/dev/null +ec=$? +case $ec in +0) + echo "not ok 2 - $name" + ;; +*) + echo "ok 2 - $name" + ;; +esac + +kill "$chpid" +rm -f $pidfile +rm -f $sleep diff --git a/tools/regression/usr.bin/pkill/pkill-LF.t b/tools/regression/usr.bin/pkill/pkill-LF.t new file mode 100644 index 000000000000..adb6ba9d4f97 --- /dev/null +++ b/tools/regression/usr.bin/pkill/pkill-LF.t @@ -0,0 +1,43 @@ +#!/bin/sh +# $FreeBSD$ + +base=`basename $0` + +echo "1..2" + +name="pkill -LF " +pidfile=`mktemp /tmp/$base.XXXXXX` || exit 1 +sleep=`mktemp /tmp/$base.XXXXXX` || exit 1 +ln -sf /bin/sleep $sleep +daemon -p $pidfile $sleep 5 +sleep 0.3 +pkill -f -L -F $pidfile $sleep +ec=$? +case $ec in +0) + echo "ok 1 - $name" + ;; +*) + echo "not ok 1 - $name" + ;; +esac + +# Be sure we cannot kill process which pidfile is not locked. +$sleep 5 & +sleep 0.3 +chpid=$! +echo $chpid > $pidfile +pkill -f -L -F $pidfile $sleep 2>/dev/null +ec=$? +case $ec in +0) + echo "not ok 2 - $name" + ;; +*) + echo "ok 2 - $name" + ;; +esac + +kill "$chpid" +rm -f $pidfile +rm -f $sleep diff --git a/usr.bin/cmp/link.c b/usr.bin/cmp/link.c new file mode 100644 index 000000000000..fe45def9fb57 --- /dev/null +++ b/usr.bin/cmp/link.c @@ -0,0 +1,93 @@ +/*- + * Copyright (c) 2005 Brian Somers + * 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. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include + +#include "extern.h" + +void +c_link(const char *file1, off_t skip1, const char *file2, off_t skip2) +{ + char buf1[PATH_MAX], *p1; + char buf2[PATH_MAX], *p2; + int dfound, len1, len2; + off_t byte; + u_char ch; + + if ((len1 = readlink(file1, buf1, sizeof(buf1) - 1)) < 0) { + if (!sflag) + err(ERR_EXIT, "%s", file1); + else + exit(ERR_EXIT); + } + + if ((len2 = readlink(file2, buf2, sizeof(buf2) - 1)) < 0) { + if (!sflag) + err(ERR_EXIT, "%s", file2); + else + exit(ERR_EXIT); + } + + if (skip1 > len1) + skip1 = len1; + buf1[len1] = '\0'; + + if (skip2 > len2) + skip2 = len2; + buf2[len2] = '\0'; + + dfound = 0; + byte = 1; + for (p1 = buf1 + skip1, p2 = buf2 + skip2; *p1 && *p2; p1++, p2++) { + if ((ch = *p1) != *p2) { + if (xflag) { + dfound = 1; + (void)printf("%08llx %02x %02x\n", + (long long)byte - 1, ch, *p2); + } else if (lflag) { + dfound = 1; + (void)printf("%6lld %3o %3o\n", + (long long)byte, ch, *p2); + } else + diffmsg(file1, file2, byte, 1); + /* NOTREACHED */ + } + byte++; + } + + if (*p1 || *p2) + eofmsg (*p1 ? file2 : file1); + if (dfound) + exit(DIFF_EXIT); +}