Home | History | Annotate | Download | only in tests
      1 /*
      2  * Check decoding of prctl operations which use arg2 as pointer to an integer
      3  * value: PR_GET_CHILD_SUBREAPER, PR_GET_ENDIAN, PR_GET_FPEMU, and PR_GET_FPEXC.
      4  *
      5  * Copyright (c) 2016 Eugene Syromyatnikov <evgsyr (at) gmail.com>
      6  * Copyright (c) 2016 Dmitry V. Levin <ldv (at) altlinux.org>
      7  * All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include "tests.h"
     33 #include <asm/unistd.h>
     34 
     35 #if defined __NR_prctl
     36 
     37 # include <stdint.h>
     38 # include <stdio.h>
     39 # include <unistd.h>
     40 # include <linux/prctl.h>
     41 
     42 static const char *errstr;
     43 
     44 static long
     45 prctl(kernel_ulong_t arg1, kernel_ulong_t arg2)
     46 {
     47 	static const kernel_ulong_t bogus_arg =
     48 		(kernel_ulong_t) 0xdeadbeefbadc0dedULL;
     49 	long rc = syscall(__NR_prctl, arg1, arg2, bogus_arg);
     50 	errstr = sprintrc(rc);
     51 	return rc;
     52 }
     53 
     54 int
     55 main(void)
     56 {
     57 	static const kernel_ulong_t bogus_addr1 =
     58 		(kernel_ulong_t) 0x1e55c0de00000000ULL;
     59 	static const kernel_ulong_t bogus_addr2 =
     60 		(kernel_ulong_t) 0xfffffffffffffffdULL;
     61 	static const kernel_ulong_t bogus_op_bits =
     62 		(kernel_ulong_t) 0xbadc0ded00000000ULL;
     63 	static const struct {
     64 		kernel_ulong_t val;
     65 		const char *str;
     66 	} options[] = {
     67 		{ 37, "PR_GET_CHILD_SUBREAPER" },
     68 		{ 19, "PR_GET_ENDIAN" },
     69 		{  9, "PR_GET_FPEMU" },
     70 		{ 11, "PR_GET_FPEXC" },
     71 	};
     72 
     73 	unsigned int *ptr = tail_alloc(sizeof(*ptr));
     74 	long rc;
     75 	unsigned int i;
     76 
     77 	for (i = 0; i < ARRAY_SIZE(options); ++i) {
     78 		prctl(options[i].val | bogus_op_bits, 0);
     79 		printf("prctl(%s, NULL) = %s\n", options[i].str, errstr);
     80 
     81 		if (bogus_addr1) {
     82 			prctl(options[i].val | bogus_op_bits, bogus_addr1);
     83 			printf("prctl(%s, %#llx) = %s\n", options[i].str,
     84 			       (unsigned long long) bogus_addr1, errstr);
     85 		}
     86 
     87 		prctl(options[i].val | bogus_op_bits, bogus_addr2);
     88 		printf("prctl(%s, %#llx) = %s\n", options[i].str,
     89 		       (unsigned long long) bogus_addr2, errstr);
     90 
     91 		prctl(options[i].val | bogus_op_bits, (uintptr_t) (ptr + 1));
     92 		printf("prctl(%s, %p) = %s\n", options[i].str,
     93 		       ptr + 1, errstr);
     94 
     95 		rc = prctl(options[i].val | bogus_op_bits, (uintptr_t) ptr);
     96 		if (!rc) {
     97 			printf("prctl(%s, [%u]) = %s\n",
     98 			       options[i].str, *ptr, errstr);
     99 		} else {
    100 			printf("prctl(%s, %p) = %s\n",
    101 			       options[i].str, ptr, errstr);
    102 		}
    103 
    104 		if (F8ILL_KULONG_SUPPORTED) {
    105 			kernel_ulong_t bogus_addr3 = f8ill_ptr_to_kulong(ptr);
    106 			prctl(options[i].val | bogus_op_bits, bogus_addr3);
    107 			printf("prctl(%s, %#llx) = %s\n", options[i].str,
    108 			       (unsigned long long) bogus_addr3, errstr);
    109 		}
    110 	}
    111 
    112 	puts("+++ exited with 0 +++");
    113 	return 0;
    114 }
    115 
    116 #else
    117 
    118 SKIP_MAIN_UNDEFINED("__NR_prctl")
    119 
    120 #endif
    121