File: malloc.c

package info (click to toggle)
clsync 0.4.5-2.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,404 kB
  • sloc: ansic: 15,074; sh: 466; makefile: 269
file content (256 lines) | stat: -rw-r--r-- 5,646 bytes parent folder | download | duplicates (3)
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
    clsync - file tree sync utility based on inotify

    Copyright (C) 2013  Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    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
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "macros.h"

#include <stdlib.h>
#include <string.h>
#ifdef CAPABILITIES_SUPPORT
# include <unistd.h>
# include <sys/mman.h>
# ifdef SECCOMP_SUPPORT
#  include <sys/stat.h>
#  include <fcntl.h>
# endif
#endif

#include <sys/ipc.h>			// shmget()
#include <sys/shm.h>			// shmget()

#include "malloc.h"
#include "error.h"
#include "configuration.h"

#ifdef CAPABILITIES_SUPPORT
long pagesize;
# ifdef SECCOMP_SUPPORT
int  devzero_fd;
# endif
#endif

void *xmalloc ( size_t size )
{
	debug ( 20, "(%li)", size );
#ifdef PARANOID
	size++;	// Just in case
#endif
	void *ret = malloc ( size );

	if ( unlikely(ret == NULL) )
		critical ( "(%li): Cannot allocate memory.", size );

#ifdef PARANOID
	memset ( ret, 0, size );
#endif
	return ret;
}

void *xcalloc ( size_t nmemb, size_t size )
{
	debug ( 20, "(%li, %li)", nmemb, size );
#ifdef PARANOID
	nmemb++; // Just in case
	size++;	 // Just in case
#endif
	void *ret = calloc ( nmemb, size );

	if ( unlikely(ret == NULL) )
		critical ( "(%li): Cannot allocate memory.", size );

//	memset(ret, 0, nmemb*size);	// Just in case
	return ret;
}

void *xrealloc ( void *oldptr, size_t size )
{
	debug ( 20, "(%p, %li)", oldptr, size );
#ifdef PARANOID
	size++;	// Just in case
#endif
	void *ret = realloc ( oldptr, size );

	if ( unlikely(ret == NULL) )
		critical ( "(%p, %li): Cannot reallocate memory.", oldptr, size );

	return ret;
}

char *xstrncpy ( char *dest, const char *src, size_t n )
{
    char *ret = strncpy ( dest, src, n - 1 );
    if ( likely(n > 0) )
        dest[n-1] = 0;
    else
        dest[0] = 0;

    return ret;
}

#ifdef CAPABILITIES_SUPPORT
void *malloc_align ( size_t size )
{
	size_t total_size;
	void *ret = NULL;
	debug ( 20, "(%li)", size );
# ifdef PARANOID
	size++;	 // Just in case
# endif
	total_size  = size;
	// Rounding total_size up to a number of times pagesize
	total_size += pagesize - 1;
	total_size /= pagesize;
	total_size *= pagesize;

	if ( unlikely ( posix_memalign ( &ret, pagesize, total_size ) ) )
		critical ( "(%li): Cannot allocate memory.", size );

# ifdef PARANOID

	if ( unlikely ( ret == NULL ))
		critical ( "(%li): ptr == NULL.", size );

# endif
//	memset(ret, 0, nmemb*size);	// Just in case
	return ret;
}

void *calloc_align ( size_t nmemb, size_t size )
{
	size_t total_size;
	void *ret;
	debug ( 20, "(%li, %li)", nmemb, size );
# ifdef PARANOID
	nmemb++; // Just in case
	size++;	 // Just in case
# endif
	total_size = nmemb * size;
	ret = malloc_align ( total_size );
	memset ( ret, 0, total_size );
	return ret;
}

char *strdup_protect ( const char *src, int prot )
{
	size_t len = strlen ( src ) + 1;
	char *dst  = malloc_align ( len );
	strcpy ( dst, src );

	if ( unlikely ( mprotect ( dst, len, prot ) ) )
		critical ( "(%p, 0x%o): Got error from mprotect(%p, %lu, 0x%o)", src, prot, dst, len, prot );

	return dst;
}

# ifdef SECCOMP_SUPPORT
int is_protected ( void *addr )
{
	char *_addr = addr, t;
	int is_protected;
	t = *_addr;
	is_protected = ( read ( devzero_fd, addr, 1 ) == -1 );

	if ( !is_protected )
		*_addr = t;

	return is_protected;
}
# endif

#endif

int memory_init()
{
#ifdef CAPABILITIES_SUPPORT
	pagesize   = sysconf ( _SC_PAGE_SIZE );

	if ( unlikely ( pagesize == -1 ) )
		critical ( "Got error from sysconf(_SC_PAGE_SIZE)" );

# ifdef SECCOMP_SUPPORT
	devzero_fd = open ( DEVZERO, O_RDONLY );

	if ( unlikely ( devzero_fd == -1 ) )
		critical ( "Got error while open(\""DEVZERO"\", O_RDONLY)" );

# endif
#endif
	return 0;
}

void *shm_malloc_try ( size_t size )
{
	void *ret;
#ifdef PARANOID
	size++;
#endif
	int privileged_shmid = shmget ( 0, size, IPC_PRIVATE | IPC_CREAT | 0600 );
	struct shmid_ds shmid_ds;

	if ( unlikely ( privileged_shmid == -1 ) ) return NULL;

	ret = shmat ( privileged_shmid, NULL, 0 );

	if ( unlikely ( ( long ) ret == -1 ) ) return NULL;

	debug ( 15, "ret == %p", ret );
	// Forbidding access for others to the pointer
	shmctl ( privileged_shmid, IPC_STAT, &shmid_ds );
	shmid_ds.shm_perm.mode = 0;
	shmctl ( privileged_shmid, IPC_SET,  &shmid_ds );
	// Checking that nobody else attached to the shared memory before access forbidding
	shmctl ( privileged_shmid, IPC_STAT, &shmid_ds );

	if ( unlikely ( shmid_ds.shm_lpid != shmid_ds.shm_cpid ) ) {
		error ( "A process (pid %u) attached to my shared memory. It's a security problem. Emergency exit." );
		shmdt ( ret );
		return NULL;
	}

	return ret;
}

void *shm_malloc ( size_t size )
{
	void *ret;
	ret = shm_malloc_try ( size );
	critical_on ( ret == NULL );
	return ret;
}

void *shm_calloc ( size_t nmemb, size_t size )
{
	void *ret;
	size_t total_size;
#ifdef PARANOID
	nmemb++;
	size++;
#endif
	total_size = nmemb * size;
	ret = shm_malloc ( total_size );
	critical_on ( ret == NULL );
	memset ( ret, 0, total_size );
	return ret;
}

void shm_free ( void *ptr )
{
	debug ( 25, "(%p)", ptr );
	shmdt ( ptr );
}