summaryrefslogtreecommitdiffstats
path: root/src/app-layer-htp-mem.c
blob: bd9b79f6762398435a40a151ad1709c6b811c38b (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* Copyright (C) 2013 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.
 */

/**
 * \ingroup httplayer
 *
 * @{
 */

/**
 * \file
 *
 * \author Eric Leblond <eric@regit.org>
 *
 * This file provides a memory handling for the HTTP protocol support.
 */

#include "suricata-common.h"
#include "app-layer-htp-mem.h"

#include "conf.h"
#include "util-misc.h"
#include "util-debug.h"

SC_ATOMIC_DECLARE(uint64_t, htp_config_memcap);
SC_ATOMIC_DECLARE(uint64_t, htp_memuse);
SC_ATOMIC_DECLARE(uint64_t, htp_memcap);

void HTPParseMemcap(void)
{
    const char *conf_val;

    SC_ATOMIC_INIT(htp_config_memcap);

    /** set config values for memcap, prealloc and hash_size */
    uint64_t memcap;
    if ((ConfGet("app-layer.protocols.http.memcap", &conf_val)) == 1)
    {
        if (ParseSizeStringU64(conf_val, &memcap) < 0) {
            SCLogError("Error parsing http.memcap "
                       "from conf file - %s.  Killing engine",
                    conf_val);
            exit(EXIT_FAILURE);
        } else {
            SC_ATOMIC_SET(htp_config_memcap, memcap);
        }
        SCLogInfo("HTTP memcap: %"PRIu64, SC_ATOMIC_GET(htp_config_memcap));
    } else {
        /* default to unlimited */
        SC_ATOMIC_SET(htp_config_memcap, 0);
    }

    SC_ATOMIC_INIT(htp_memuse);
    SC_ATOMIC_INIT(htp_memcap);
}

static void HTPIncrMemuse(uint64_t size)
{
    (void) SC_ATOMIC_ADD(htp_memuse, size);
    return;
}

static void HTPDecrMemuse(uint64_t size)
{
    (void) SC_ATOMIC_SUB(htp_memuse, size);
    return;
}

uint64_t HTPMemuseGlobalCounter(void)
{
    uint64_t tmpval = SC_ATOMIC_GET(htp_memuse);
    return tmpval;
}

uint64_t HTPMemcapGlobalCounter(void)
{
    uint64_t tmpval = SC_ATOMIC_GET(htp_memcap);
    return tmpval;
}

/**
 *  \brief Check if alloc'ing "size" would mean we're over memcap
 *
 *  \retval 1 if in bounds
 *  \retval 0 if not in bounds
 */
static int HTPCheckMemcap(uint64_t size)
{
    uint64_t memcapcopy = SC_ATOMIC_GET(htp_config_memcap);
    if (memcapcopy == 0 || size + SC_ATOMIC_GET(htp_memuse) <= memcapcopy)
        return 1;
    (void) SC_ATOMIC_ADD(htp_memcap, 1);
    return 0;
}

/**
 *  \brief Update memcap value
 *
 *  \param size new memcap value
 */
int HTPSetMemcap(uint64_t size)
{
    if (size == 0 || (uint64_t)SC_ATOMIC_GET(htp_memuse) < size) {
        SC_ATOMIC_SET(htp_config_memcap, size);
        return 1;
    }
    return 0;
}

/**
 *  \brief Update memcap value
 *
 *  \retval memcap value
 */
uint64_t HTPGetMemcap(void)
{
    uint64_t memcapcopy = SC_ATOMIC_GET(htp_config_memcap);
    return memcapcopy;
}

void *HTPMalloc(size_t size)
{
    void *ptr = NULL;

    if (HTPCheckMemcap((uint32_t)size) == 0)
        return NULL;

    ptr = SCMalloc(size);

    if (unlikely(ptr == NULL))
        return NULL;

    HTPIncrMemuse((uint64_t)size);

    return ptr;
}

void *HTPCalloc(size_t n, size_t size)
{
    void *ptr = NULL;

    if (HTPCheckMemcap((uint32_t)(n * size)) == 0)
        return NULL;

    ptr = SCCalloc(n, size);

    if (unlikely(ptr == NULL))
        return NULL;

    HTPIncrMemuse((uint64_t)(n * size));

    return ptr;
}

void *HTPRealloc(void *ptr, size_t orig_size, size_t size)
{
    if (size > orig_size) {
        if (HTPCheckMemcap((uint32_t)(size - orig_size)) == 0)
            return NULL;
    }

    void *rptr = SCRealloc(ptr, size);
    if (rptr == NULL)
        return NULL;

    if (size > orig_size) {
        HTPIncrMemuse((uint64_t)(size - orig_size));
    } else {
        HTPDecrMemuse((uint64_t)(orig_size - size));
    }

    return rptr;
}

void HTPFree(void *ptr, size_t size)
{
    SCFree(ptr);

    HTPDecrMemuse((uint64_t)size);
}

/**
 * @}
 */