File: file_path_security.c

package info (click to toggle)
zulucrypt 6.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,908 kB
  • sloc: ansic: 25,142; cpp: 23,281; makefile: 24; xml: 10; sh: 2
file content (291 lines) | stat: -rw-r--r-- 6,996 bytes parent folder | download
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
 *
 *  Copyright ( c ) 2012-2015
 *  name : Francis Banyikwa
 *  email: mhogomchungu@gmail.com
 *  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 2 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 "includes.h"
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mount.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>

#define _ignore_result( x ) if( x ){;}

int zuluCryptSecureOpenFile( const char * path,int * fd,string_t * file,uid_t uid )
{
	int st ;
	int f = -1 ;
	uid_t org = geteuid() ;
	char * dev ;

	_ignore_result( seteuid( uid ) )

	f = open( path,O_RDONLY ) ;

	if( f != -1 ){

		dev = zuluCryptGetFileNameFromFileDescriptor( f ) ;
		*file = StringInherit( &dev ) ;
		*fd = f ;
		st = 1 ;
	}else{
		st = 0  ;
	}
	_ignore_result( seteuid( org ) )
	return st ;
}

void zuluCryptDeleteFile( const char * file )
{
	int fd ;
	void * map ;
	struct stat st ;

	if( file != NULL ){

		fd = open( file,O_WRONLY ) ;

		if( fd != -1 ){

			fstat( fd,&st ) ;
			map =  mmap( 0,(unsigned long)st.st_size,PROT_WRITE,MAP_PRIVATE,fd,0 ) ;

			if( map != MAP_FAILED ){

				memset( map,'\0',(unsigned long)st.st_size ) ;
				munmap( map,(unsigned long)st.st_size ) ;
			}
			close( fd ) ;
		}
		unlink( file ) ;
	}
}

void zuluCryptDeleteFile_1( string_t st )
{
	zuluCryptDeleteFile( StringContent( st ) ) ;
}

static int _check_if_device_is_supported( int st,uid_t uid,char ** dev )
{
	string_t fs ;
	if( st == 0 ){
		_ignore_result( seteuid( 0 ) )
		/*
		* zuluCryptGetFileSystemFromDevice() is defined in blkid_evaluate_tag.c
		*/
		fs = zuluCryptGetFileSystemFromDevice( *dev ) ;

		_ignore_result( seteuid( uid ) )

		if( fs != StringVoid ){
			if( StringHasAtLeastOneComponent( fs,"member","swap",NULL ) ){
				st = 100 ;
			}
			StringDelete( &fs ) ;
		}
	}else{
		/*
		 * safely do free( *dev ) followed by *dev = NULL
		 */
		StringFree_1( dev ) ;
	}

	return st ;
}

int zuluCryptGetDeviceFileProperties( const char * file,int * fd_path,int * fd_loop,char ** dev,uid_t uid )
{
	int st = 100 ;
	int xt = 0 ;
	int lfd ;

	const char * dev_1 = NULL ;
	string_t st_dev = StringVoid ;

	struct stat stat_st ;
	struct stat stat_st_1 ;
	/*
	 * try to open the device with user privileges
	 */
	_ignore_result( seteuid( uid ) )

	*dev = NULL ;

	*fd_path = open( file,O_RDONLY ) ;

	if( *fd_path != -1 ){

		fstat( *fd_path,&stat_st ) ;
		fcntl( *fd_path,F_SETFD,FD_CLOEXEC ) ;
		/*
		 * A user has access to the device.They should get here only with paths to files they have access to.
		 * Allow access to files only
		 */
		if( S_ISREG( stat_st.st_mode ) ){
			/*
			 * we can open file in read mode,let see if we can in write mode too
			 */
			lfd = open( file,O_RDWR ) ;
			if( lfd != -1 ){
				/*
				 * we can open the file in read write mode
				 */
				fstat( lfd,&stat_st_1 ) ;
				fcntl( lfd,F_SETFD,FD_CLOEXEC ) ;

				/*
				 * check to make sure the file we got earlier is the same one we got now.
				 * ie check to make sure the file wasnt changed btw calls.
				 */
				if( stat_st.st_dev == stat_st_1.st_dev && stat_st.st_ino == stat_st_1.st_ino ){
					close( *fd_path ) ;
					*fd_path = lfd ;
					_ignore_result( seteuid( 0 ) )
					/*
					 * zuluCryptAttachLoopDeviceToFileUsingFileDescriptor() is defined in ./create_loop_device.c
					 */
					xt = zuluCryptAttachLoopDeviceToFileUsingFileDescriptor( *fd_path,fd_loop,O_RDWR,&st_dev ) ;
					_ignore_result( seteuid( uid ) )
					*dev = StringDeleteHandle( &st_dev ) ;
				}
			}else{
				/*
				 * we can not open the file in write mode,continue with read only access
				 */
				_ignore_result( seteuid( 0 ) )
				/*
				 * zuluCryptAttachLoopDeviceToFileUsingFileDescriptor() is defined in ./create_loop_device.c
				 */
				xt = zuluCryptAttachLoopDeviceToFileUsingFileDescriptor( *fd_path,fd_loop,O_RDONLY,&st_dev ) ;
				_ignore_result( seteuid( uid ) )
				*dev = StringDeleteHandle( &st_dev ) ;
			}
			if( xt != 1 ){
				st = 100 ;
				_ignore_result( close( *fd_path ) )
				*fd_path = -1 ;
			}else{
				dev_1 = zuluCryptGetFileNameFromFileDescriptor( *fd_path ) ;
				if( StringPrefixEqual( dev_1,"/dev/shm/" ) ){
					st =1 ;
					_ignore_result( close( *fd_path ) )
					*fd_path = -1 ;
				}else{
					st = 0 ;
				}
				StringFree( dev_1 ) ;
			}
		}else{
			if( S_ISBLK( stat_st.st_mode ) ){

				if( uid == 0 ){
					/*
					 * we got a block device and we are root,accept it
					 */
					*dev = zuluCryptGetFileNameFromFileDescriptor( *fd_path ) ;
					st = 0 ;
				}else{
					/*
					 * odd,normal user has access to a block device,allow it only if the
					 * device is in "/dev/" but not in "/dev/shm"
					 */
					*dev = zuluCryptGetFileNameFromFileDescriptor( *fd_path ) ;
					if( StringPrefixEqual( *dev,"/dev/shm/" ) ){
						st = 1 ;
					}else if( StringPrefixEqual( *dev,"/dev/" ) ){
						st = 0 ;
					}
				}

			}else if( S_ISDIR( stat_st.st_mode ) ){

				st = 2 ;
			}else{
				/*
				 * whatever it is,it cant be good,reject it
				 */
				st = 100 ;
			}
			_ignore_result( close( *fd_path ) )
			*fd_path = -1 ;
		}
	}else{
		/*
		 * failed to open above with users privileges,try to open the device with root's privileges.
		 * We should only accept block devices in "/dev/" but not in "/dev/shm".
		 */
		_ignore_result( seteuid( 0 ) )

		*fd_path = open( file,O_RDONLY ) ;

		if( *fd_path != -1 ){

			fstat( *fd_path,&stat_st ) ;
			/*
			 * zuluCryptGetFileNameFromFileDescriptor() is defined in ./create_loop_device.c
			 */
			*dev = zuluCryptGetFileNameFromFileDescriptor( *fd_path ) ;

			if( S_ISBLK( stat_st.st_mode ) ){

				if( StringPrefixEqual( *dev,"/dev/shm/" ) ){
					/*
					* we do not support this path
					*/
					st = 1 ;

				}else if( StringPrefixEqual( *dev,"/dev/" ) ){
					/*
					* got a block device,accept it
					*/
					st = 0 ;
				}else{
					/*
					* reject others
					*/
					st = 100 ;
				}
			}else{
				/*
				 * whatever it is,it cant be good,reject it
				 */
				st = 100 ;
			}
			/*
			 * We are closing the file because we dont need to hold on to it as paths in "/dev/" can not be moved under us by
			 * normal users.
			 */
			_ignore_result( close( *fd_path ) )
			*fd_path = -1 ;
		}else{
			/*
			 * invalid path or something i dont know,reject
			 */
			st = 100 ;
		}

		_ignore_result( seteuid( uid ) )
	}

	return _check_if_device_is_supported( st,uid,dev ) ;
}