summaryrefslogtreecommitdiffstats
path: root/src/util-pages.c
blob: 170170b1933da4d9dbcc7719d976de9f348b064f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/* Copyright (C) 2016 Open Information Security Foundation
 *
 * You can copy, redistribute or modify this Program under the terms of
 * the GNU General Public License version 2 as published by the Free
 * Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * version 2 along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

/**
 * \file
 *
 * \author Victor Julien <victor@inliniac.net>
 *
 * Page util functions
 */

#include "suricata-common.h"
#include "util-pages.h"
#include "util-debug.h"

#ifndef HAVE_PAGESUPPORTSRWX_AS_MACRO

/** \brief check if OS allows for RWX pages
 *
 *  Some OS' disallow RWX pages for security reasons. This function mmaps
 *  some memory RW and then tries to turn it into RWX. If this fails we
 *  assume that the OS doesn't allow for this.
 *
 *  Thanks to Shawn Webb from HardenedBSD for the suggestion.
 *
 *  \retval 1 RWX supported
 *  \retval 0 not supported
 */
int PageSupportsRWX(void)
{
    int retval = 1;
    void *ptr;
    ptr = mmap(0, getpagesize(), PROT_READ|PROT_WRITE, MAP_ANON|MAP_SHARED, -1, 0);
    if (ptr != MAP_FAILED) {
        if (mprotect(ptr, getpagesize(), PROT_READ|PROT_WRITE|PROT_EXEC) == -1) {
            SCLogConfig("RWX pages denied by OS");
            retval = 0;
        }
        munmap(ptr, getpagesize());
    }
    return retval;
}
#endif /* HAVE_PAGESUPPORTSRWX_AS_MACRO */