|
| 1 | +/*- |
| 2 | + * Copyright (c) 1982, 1986, 1991, 1993 |
| 3 | + * The Regents of the University of California. All rights reserved. |
| 4 | + * (c) UNIX System Laboratories, Inc. |
| 5 | + * All or some portions of this file are derived from material licensed |
| 6 | + * to the University of California by American Telephone and Telegraph |
| 7 | + * Co. or Unix System Laboratories, Inc. and are reproduced herein with |
| 8 | + * the permission of UNIX System Laboratories, Inc. |
| 9 | + * |
| 10 | + * Redistribution and use in source and binary forms, with or without |
| 11 | + * modification, are permitted provided that the following conditions |
| 12 | + * are met: |
| 13 | + * 1. Redistributions of source code must retain the above copyright |
| 14 | + * notice, this list of conditions and the following disclaimer. |
| 15 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 16 | + * notice, this list of conditions and the following disclaimer in the |
| 17 | + * documentation and/or other materials provided with the distribution. |
| 18 | + * 4. Neither the name of the University nor the names of its contributors |
| 19 | + * may be used to endorse or promote products derived from this software |
| 20 | + * without specific prior written permission. |
| 21 | + * |
| 22 | + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 23 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 24 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 25 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 26 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 27 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 28 | + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 29 | + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 30 | + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 31 | + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 32 | + * SUCH DAMAGE. |
| 33 | + * |
| 34 | + * @(#)kern_subr.c 8.3 (Berkeley) 1/21/94 |
| 35 | + */ |
| 36 | + |
| 37 | +#include <assert.h> |
| 38 | +#include <errno.h> |
| 39 | +#include <stdlib.h> |
| 40 | +#include <string.h> |
| 41 | +#include "uio.h" |
| 42 | + |
| 43 | +int |
| 44 | +uiomove(void *cp, int n, struct uio *uio) |
| 45 | +{ |
| 46 | + assert(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE); |
| 47 | + |
| 48 | + while (n > 0 && uio->uio_resid) { |
| 49 | + struct iovec *iov = uio->uio_iov; |
| 50 | + size_t cnt = iov->iov_len; |
| 51 | + if (cnt == 0) { |
| 52 | + uio->uio_iov++; |
| 53 | + uio->uio_iovcnt--; |
| 54 | + continue; |
| 55 | + } |
| 56 | + if (cnt > n) |
| 57 | + cnt = n; |
| 58 | + |
| 59 | + if (uio->uio_rw == UIO_READ) |
| 60 | + memcpy(iov->iov_base, cp, cnt); |
| 61 | + else |
| 62 | + memcpy(cp, iov->iov_base, cnt); |
| 63 | + |
| 64 | + iov->iov_base = (char *)iov->iov_base + cnt; |
| 65 | + iov->iov_len -= cnt; |
| 66 | + uio->uio_resid -= cnt; |
| 67 | + uio->uio_offset += cnt; |
| 68 | + cp = (char *)cp + cnt; |
| 69 | + n -= cnt; |
| 70 | + } |
| 71 | + |
| 72 | + return 0; |
| 73 | +} |
| 74 | + |
| 75 | +/* |
| 76 | + * Wrapper for uiomove() that validates the arguments against a known-good |
| 77 | + * kernel buffer. Currently, uiomove accepts a signed (n) argument, which |
| 78 | + * is almost definitely a bad thing, so we catch that here as well. We |
| 79 | + * return a runtime failure, but it might be desirable to generate a runtime |
| 80 | + * assertion failure instead. |
| 81 | + */ |
| 82 | +int |
| 83 | +uiomove_frombuf(void *buf, int buflen, struct uio *uio) |
| 84 | +{ |
| 85 | + size_t offset, n; |
| 86 | + |
| 87 | + if (uio->uio_offset < 0 || uio->uio_resid < 0 || |
| 88 | + (offset = uio->uio_offset) != uio->uio_offset) |
| 89 | + return (EINVAL); |
| 90 | + if (buflen <= 0 || offset >= buflen) |
| 91 | + return (0); |
| 92 | + if ((n = buflen - offset) > IOSIZE_MAX) |
| 93 | + return (EINVAL); |
| 94 | + return (uiomove((char *)buf + offset, n, uio)); |
| 95 | +} |
| 96 | + |
| 97 | +/* |
| 98 | + * In OSv we don't really copy the iovect in because it lives in the same |
| 99 | + * address space. But keeping this abstraction still allows keeping the |
| 100 | + * checks in one place and eases porting code. |
| 101 | + */ |
| 102 | +int |
| 103 | +copyinuio(struct iovec *iovp, u_int iovcnt, struct uio **uiop) |
| 104 | +{ |
| 105 | + struct uio *uio; |
| 106 | + int i; |
| 107 | + |
| 108 | + *uiop = NULL; |
| 109 | + if (iovcnt > UIO_MAXIOV) |
| 110 | + return (EINVAL); |
| 111 | + uio = malloc(sizeof(*uio)); |
| 112 | + |
| 113 | + uio->uio_iov = iovp; |
| 114 | + uio->uio_iovcnt = iovcnt; |
| 115 | + uio->uio_offset = -1; |
| 116 | + uio->uio_resid = 0; |
| 117 | + for (i = 0; i < iovcnt; i++) { |
| 118 | + if (iovp->iov_len > IOSIZE_MAX - uio->uio_resid) { |
| 119 | + free(uio); |
| 120 | + return (EINVAL); |
| 121 | + } |
| 122 | + uio->uio_resid += iovp->iov_len; |
| 123 | + iovp++; |
| 124 | + } |
| 125 | + *uiop = uio; |
| 126 | + return (0); |
| 127 | +} |
0 commit comments