xref: /openbsd-src/lib/libelf/elf_rand.c (revision a5f4ee4e3f8f42ac550adf39a31813f588ef9dc2)
1a1b5ec25Sjsg /*-
2a1b5ec25Sjsg  * Copyright (c) 2006,2008 Joseph Koshy
3a1b5ec25Sjsg  * All rights reserved.
4a1b5ec25Sjsg  *
5a1b5ec25Sjsg  * Redistribution and use in source and binary forms, with or without
6a1b5ec25Sjsg  * modification, are permitted provided that the following conditions
7a1b5ec25Sjsg  * are met:
8a1b5ec25Sjsg  * 1. Redistributions of source code must retain the above copyright
9a1b5ec25Sjsg  *    notice, this list of conditions and the following disclaimer.
10a1b5ec25Sjsg  * 2. Redistributions in binary form must reproduce the above copyright
11a1b5ec25Sjsg  *    notice, this list of conditions and the following disclaimer in the
12a1b5ec25Sjsg  *    documentation and/or other materials provided with the distribution.
13a1b5ec25Sjsg  *
14a1b5ec25Sjsg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15a1b5ec25Sjsg  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16a1b5ec25Sjsg  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17a1b5ec25Sjsg  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18a1b5ec25Sjsg  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19a1b5ec25Sjsg  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20a1b5ec25Sjsg  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21a1b5ec25Sjsg  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22a1b5ec25Sjsg  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23a1b5ec25Sjsg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24a1b5ec25Sjsg  * SUCH DAMAGE.
25a1b5ec25Sjsg  */
26a1b5ec25Sjsg 
27a1b5ec25Sjsg #include <ar.h>
28a1b5ec25Sjsg #include <libelf.h>
29e219834fSjsg #include <stdint.h>
30a1b5ec25Sjsg 
31a1b5ec25Sjsg #include "_libelf.h"
32a1b5ec25Sjsg 
33*a5f4ee4eSjsg ELFTC_VCSID("$Id: elf_rand.c,v 1.3 2019/03/19 02:37:46 jsg Exp $");
34a1b5ec25Sjsg 
35a1b5ec25Sjsg off_t
elf_rand(Elf * ar,off_t offset)36a1b5ec25Sjsg elf_rand(Elf *ar, off_t offset)
37a1b5ec25Sjsg {
38a1b5ec25Sjsg 	struct ar_hdr *arh;
39e219834fSjsg 	off_t offset_of_member;
40e219834fSjsg 
41a1b5ec25Sjsg 	if (ar == NULL || ar->e_kind != ELF_K_AR ||
42a1b5ec25Sjsg 	    (offset & 1) || offset < SARMAG ||
43*a5f4ee4eSjsg 	    offset >= ar->e_rawsize) {
44e219834fSjsg 		LIBELF_SET_ERROR(ARGUMENT, 0);
45e219834fSjsg 		return 0;
46e219834fSjsg 	}
47e219834fSjsg 
48*a5f4ee4eSjsg 	offset_of_member = offset + (off_t) sizeof(struct ar_hdr);
49*a5f4ee4eSjsg 
50*a5f4ee4eSjsg 	if (offset_of_member <= 0 || /* Numeric overflow. */
51*a5f4ee4eSjsg 	    offset_of_member >= ar->e_rawsize) {
52a1b5ec25Sjsg 		LIBELF_SET_ERROR(ARGUMENT, 0);
53a1b5ec25Sjsg 		return 0;
54a1b5ec25Sjsg 	}
55a1b5ec25Sjsg 
56a1b5ec25Sjsg 	arh = (struct ar_hdr *) (ar->e_rawfile + offset);
57a1b5ec25Sjsg 
58a1b5ec25Sjsg 	/* a too simple sanity check */
59a1b5ec25Sjsg 	if (arh->ar_fmag[0] != '`' || arh->ar_fmag[1] != '\n') {
60a1b5ec25Sjsg 		LIBELF_SET_ERROR(ARCHIVE, 0);
61a1b5ec25Sjsg 		return 0;
62a1b5ec25Sjsg 	}
63a1b5ec25Sjsg 
64a1b5ec25Sjsg 	ar->e_u.e_ar.e_next = offset;
65a1b5ec25Sjsg 
66a1b5ec25Sjsg 	return (offset);
67a1b5ec25Sjsg }
68