Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

cifs: Add a function to Hash the contents of an iterator

Add a function to push the contents of a BVEC-, KVEC- or XARRAY-type
iterator into a synchronous hash algorithm.

UBUF- and IOBUF-type iterators are not supported on the assumption that
either we're doing buffered I/O, in which case we won't see them, or we're
doing direct I/O, in which case the iterator will have been extracted into
a BVEC-type iterator higher up.

Signed-off-by: David Howells <dhowells@redhat.com>
cc: Steve French <sfrench@samba.org>
cc: Shyam Prasad N <nspmangalore@gmail.com>
cc: Rohith Surabattula <rohiths.msft@gmail.com>
cc: Jeff Layton <jlayton@kernel.org>
cc: linux-cifs@vger.kernel.org
cc: linux-fsdevel@vger.kernel.org
cc: linux-crypto@vger.kernel.org

Link: https://lore.kernel.org/r/166697257423.61150.12070648579830206483.stgit@warthog.procyon.org.uk/ # rfc
Link: https://lore.kernel.org/r/166732029577.3186319.17162612653237909961.stgit@warthog.procyon.org.uk/ # rfc
Signed-off-by: Steve French <stfrench@microsoft.com>

authored by

David Howells and committed by
Steve French
39bc5820 e5fbdde4

+144
+144
fs/cifs/cifsencrypt.c
··· 24 24 #include "../smbfs_common/arc4.h" 25 25 #include <crypto/aead.h> 26 26 27 + /* 28 + * Hash data from a BVEC-type iterator. 29 + */ 30 + static int cifs_shash_bvec(const struct iov_iter *iter, ssize_t maxsize, 31 + struct shash_desc *shash) 32 + { 33 + const struct bio_vec *bv = iter->bvec; 34 + unsigned long start = iter->iov_offset; 35 + unsigned int i; 36 + void *p; 37 + int ret; 38 + 39 + for (i = 0; i < iter->nr_segs; i++) { 40 + size_t off, len; 41 + 42 + len = bv[i].bv_len; 43 + if (start >= len) { 44 + start -= len; 45 + continue; 46 + } 47 + 48 + len = min_t(size_t, maxsize, len - start); 49 + off = bv[i].bv_offset + start; 50 + 51 + p = kmap_local_page(bv[i].bv_page); 52 + ret = crypto_shash_update(shash, p + off, len); 53 + kunmap_local(p); 54 + if (ret < 0) 55 + return ret; 56 + 57 + maxsize -= len; 58 + if (maxsize <= 0) 59 + break; 60 + start = 0; 61 + } 62 + 63 + return 0; 64 + } 65 + 66 + /* 67 + * Hash data from a KVEC-type iterator. 68 + */ 69 + static int cifs_shash_kvec(const struct iov_iter *iter, ssize_t maxsize, 70 + struct shash_desc *shash) 71 + { 72 + const struct kvec *kv = iter->kvec; 73 + unsigned long start = iter->iov_offset; 74 + unsigned int i; 75 + int ret; 76 + 77 + for (i = 0; i < iter->nr_segs; i++) { 78 + size_t len; 79 + 80 + len = kv[i].iov_len; 81 + if (start >= len) { 82 + start -= len; 83 + continue; 84 + } 85 + 86 + len = min_t(size_t, maxsize, len - start); 87 + ret = crypto_shash_update(shash, kv[i].iov_base + start, len); 88 + if (ret < 0) 89 + return ret; 90 + maxsize -= len; 91 + 92 + if (maxsize <= 0) 93 + break; 94 + start = 0; 95 + } 96 + 97 + return 0; 98 + } 99 + 100 + /* 101 + * Hash data from an XARRAY-type iterator. 102 + */ 103 + static ssize_t cifs_shash_xarray(const struct iov_iter *iter, ssize_t maxsize, 104 + struct shash_desc *shash) 105 + { 106 + struct folio *folios[16], *folio; 107 + unsigned int nr, i, j, npages; 108 + loff_t start = iter->xarray_start + iter->iov_offset; 109 + pgoff_t last, index = start / PAGE_SIZE; 110 + ssize_t ret = 0; 111 + size_t len, offset, foffset; 112 + void *p; 113 + 114 + if (maxsize == 0) 115 + return 0; 116 + 117 + last = (start + maxsize - 1) / PAGE_SIZE; 118 + do { 119 + nr = xa_extract(iter->xarray, (void **)folios, index, last, 120 + ARRAY_SIZE(folios), XA_PRESENT); 121 + if (nr == 0) 122 + return -EIO; 123 + 124 + for (i = 0; i < nr; i++) { 125 + folio = folios[i]; 126 + npages = folio_nr_pages(folio); 127 + foffset = start - folio_pos(folio); 128 + offset = foffset % PAGE_SIZE; 129 + for (j = foffset / PAGE_SIZE; j < npages; j++) { 130 + len = min_t(size_t, maxsize, PAGE_SIZE - offset); 131 + p = kmap_local_page(folio_page(folio, j)); 132 + ret = crypto_shash_update(shash, p, len); 133 + kunmap_local(p); 134 + if (ret < 0) 135 + return ret; 136 + maxsize -= len; 137 + if (maxsize <= 0) 138 + return 0; 139 + start += len; 140 + offset = 0; 141 + index++; 142 + } 143 + } 144 + } while (nr == ARRAY_SIZE(folios)); 145 + return 0; 146 + } 147 + 148 + /* 149 + * Pass the data from an iterator into a hash. 150 + */ 151 + static int cifs_shash_iter(const struct iov_iter *iter, size_t maxsize, 152 + struct shash_desc *shash) 153 + { 154 + if (maxsize == 0) 155 + return 0; 156 + 157 + switch (iov_iter_type(iter)) { 158 + case ITER_BVEC: 159 + return cifs_shash_bvec(iter, maxsize, shash); 160 + case ITER_KVEC: 161 + return cifs_shash_kvec(iter, maxsize, shash); 162 + case ITER_XARRAY: 163 + return cifs_shash_xarray(iter, maxsize, shash); 164 + default: 165 + pr_err("cifs_shash_iter(%u) unsupported\n", iov_iter_type(iter)); 166 + WARN_ON_ONCE(1); 167 + return -EIO; 168 + } 169 + } 170 + 27 171 int __cifs_calc_signature(struct smb_rqst *rqst, 28 172 struct TCP_Server_Info *server, char *signature, 29 173 struct shash_desc *shash)