69
69
#define FILE_SEPARATOR '/'
70
70
71
71
struct cbargs {
72
- probe_ctx * ctx ;
72
+ probe_ctx * ctx ;
73
73
int error ;
74
- SEXP_t * attr_ent ;
74
+ SEXP_t * attr_ent ;
75
75
};
76
76
77
77
static int file_cb (const char * prefix , const char * p , const char * f , void * ptr , SEXP_t * gr_lastpath )
78
78
{
79
- char path_buffer [PATH_MAX ];
80
- SEXP_t * item , xattr_name ;
81
- struct cbargs * args = (struct cbargs * ) ptr ;
82
- const char * st_path ;
79
+ char path_buffer [PATH_MAX ];
80
+ SEXP_t * item , xattr_name ;
81
+ struct cbargs * args = (struct cbargs * ) ptr ;
82
+ const char * st_path ;
83
83
84
- ssize_t xattr_count = -1 ;
85
- char * xattr_buf = NULL ;
86
- size_t xattr_buflen = 0 , i ;
84
+ ssize_t xattr_count = -1 ;
85
+ char * xattr_buf = NULL ;
86
+ size_t xattr_buflen = 0 , i ;
87
87
88
88
if (f == NULL ) {
89
89
st_path = p ;
@@ -98,23 +98,23 @@ static int file_cb(const char *prefix, const char *p, const char *f, void *ptr,
98
98
st_path = path_buffer ;
99
99
}
100
100
101
- SEXP_init (& xattr_name );
101
+ SEXP_init (& xattr_name );
102
102
103
103
char * st_path_with_prefix = oscap_path_join (prefix , st_path );
104
104
do {
105
105
/* estimate the size of the buffer */
106
-
107
106
xattr_count = llistxattr (st_path_with_prefix , NULL , 0 );
108
107
109
108
if (xattr_count == 0 ) {
110
109
free (st_path_with_prefix );
111
- return (0 );
110
+ free (xattr_buf );
111
+ return 0 ;
112
112
}
113
113
114
114
if (xattr_count < 0 ) {
115
115
free (st_path_with_prefix );
116
- dD ("FAIL: llistxattr(%s, %p, %zu): errno=%u, %s." , errno , strerror (errno ));
117
- return 0 ;
116
+ dD ("FAIL: llistxattr(%s, %p, %zu): errno=%u, %s" , st_path_with_prefix , NULL , 0 , errno , strerror (errno ));
117
+ return 0 ;
118
118
}
119
119
120
120
/* allocate space for xattr names */
@@ -127,12 +127,14 @@ static int file_cb(const char *prefix, const char *p, const char *f, void *ptr,
127
127
/* check & retry if needed */
128
128
} while (errno == ERANGE );
129
129
130
- if (xattr_count < 0 ) {
131
- dD ("FAIL: llistxattr(%s, %p, %zu): errno=%u, %s." , errno , strerror (errno ));
132
- free (xattr_buf );
133
- }
130
+ if (xattr_count < 0 ) {
131
+ dD ("FAIL: llistxattr(%s, %p, %zu): errno=%u, %s" , st_path_with_prefix , xattr_buf , xattr_buflen , errno , strerror (errno ));
132
+ free (st_path_with_prefix );
133
+ free (xattr_buf );
134
+ return 0 ;
135
+ }
134
136
135
- /* update lastpath if needed */
137
+ /* update lastpath if needed */
136
138
if (!SEXP_emptyp (gr_lastpath )) {
137
139
if (SEXP_strcmp (gr_lastpath , p ) != 0 ) {
138
140
SEXP_free_r (gr_lastpath );
@@ -142,69 +144,69 @@ static int file_cb(const char *prefix, const char *p, const char *f, void *ptr,
142
144
SEXP_string_new_r (gr_lastpath , p , strlen (p ));
143
145
}
144
146
145
- i = 0 ;
146
- /* collect */
147
- do {
148
- SEXP_string_new_r (& xattr_name , xattr_buf + i , strlen (xattr_buf + i ));
147
+ i = 0 ;
148
+ /* collect */
149
+ do {
150
+ SEXP_string_new_r (& xattr_name , xattr_buf + i , strlen (xattr_buf + i ));
149
151
150
- if (probe_entobj_cmp (args -> attr_ent , & xattr_name ) == OVAL_RESULT_TRUE ) {
151
- ssize_t xattr_vallen = -1 ;
152
- char * xattr_val = NULL ;
152
+ if (probe_entobj_cmp (args -> attr_ent , & xattr_name ) == OVAL_RESULT_TRUE ) {
153
+ ssize_t xattr_vallen = -1 ;
154
+ char * xattr_val = NULL ;
153
155
154
- xattr_vallen = lgetxattr (st_path_with_prefix , xattr_buf + i , NULL , 0 );
155
- retry_value :
156
- if (xattr_vallen >= 0 ) {
156
+ xattr_vallen = lgetxattr (st_path_with_prefix , xattr_buf + i , NULL , 0 );
157
+ retry_value :
158
+ if (xattr_vallen >= 0 ) {
157
159
// Check possible buffer overflow
158
160
if (sizeof (char ) * (xattr_vallen + 1 ) <= sizeof (char ) * xattr_vallen ) {
159
161
dE ("Attribute is too long." );
160
162
abort ();
161
163
}
162
164
163
165
// Allocate buffer, '+1' is for trailing '\0'
164
- xattr_val = realloc (xattr_val , sizeof (char ) * (xattr_vallen + 1 ));
166
+ xattr_val = realloc (xattr_val , sizeof (char ) * (xattr_vallen + 1 ));
165
167
166
168
// we don't want to override space for '\0' by call of 'lgetxattr'
167
169
// we pass only 'xattr_vallen' instead of 'xattr_vallen + 1'
168
- xattr_vallen = lgetxattr (st_path_with_prefix , xattr_buf + i , xattr_val , xattr_vallen );
170
+ xattr_vallen = lgetxattr (st_path_with_prefix , xattr_buf + i , xattr_val , xattr_vallen );
169
171
170
- if (xattr_vallen < 0 || errno == ERANGE )
171
- goto retry_value ;
172
+ if (xattr_vallen < 0 || errno == ERANGE )
173
+ goto retry_value ;
172
174
173
175
xattr_val [xattr_vallen ] = '\0' ;
174
176
175
- item = probe_item_create (OVAL_UNIX_FILEEXTENDEDATTRIBUTE , NULL ,
176
- "filepath" , OVAL_DATATYPE_STRING , f == NULL ? NULL : st_path ,
177
- "path" , OVAL_DATATYPE_SEXP , gr_lastpath ,
178
- "filename" , OVAL_DATATYPE_STRING , f == NULL ? "" : f ,
179
- "attribute_name" , OVAL_DATATYPE_SEXP , & xattr_name ,
180
- "value" , OVAL_DATATYPE_STRING , xattr_val ,
181
- NULL );
177
+ item = probe_item_create (OVAL_UNIX_FILEEXTENDEDATTRIBUTE , NULL ,
178
+ "filepath" , OVAL_DATATYPE_STRING , f == NULL ? NULL : st_path ,
179
+ "path" , OVAL_DATATYPE_SEXP , gr_lastpath ,
180
+ "filename" , OVAL_DATATYPE_STRING , f == NULL ? "" : f ,
181
+ "attribute_name" , OVAL_DATATYPE_SEXP , & xattr_name ,
182
+ "value" , OVAL_DATATYPE_STRING , xattr_val ,
183
+ NULL );
182
184
183
- free (xattr_val );
184
- } else {
185
- dD ("FAIL: lgetxattr(%s, %s, NULL, 0): errno=%u, %s." , errno , strerror (errno ));
185
+ free (xattr_val );
186
+ } else {
187
+ dD ("FAIL: lgetxattr(%s, %s, NULL, 0): errno=%u, %s" , st_path_with_prefix , xattr_buf + i , errno , strerror (errno ));
186
188
187
- item = probe_item_create (OVAL_UNIX_FILEEXTENDEDATTRIBUTE , NULL , NULL );
188
- probe_item_setstatus (item , SYSCHAR_STATUS_ERROR );
189
+ item = probe_item_create (OVAL_UNIX_FILEEXTENDEDATTRIBUTE , NULL , NULL );
190
+ probe_item_setstatus (item , SYSCHAR_STATUS_ERROR );
189
191
190
- if (xattr_val != NULL )
191
- free (xattr_val );
192
- }
192
+ if (xattr_val != NULL )
193
+ free (xattr_val );
194
+ }
193
195
194
- probe_item_collect (args -> ctx , item ); /* XXX: handle ENOMEM */
195
- }
196
+ probe_item_collect (args -> ctx , item ); /* XXX: handle ENOMEM */
197
+ }
196
198
197
- SEXP_free_r (& xattr_name );
199
+ SEXP_free_r (& xattr_name );
198
200
199
- /* skip to next name */
200
- while (i < xattr_buflen && xattr_buf [i ] != '\0' )
201
- ++ i ;
201
+ /* skip to next name */
202
+ while (i < xattr_buflen && xattr_buf [i ] != '\0' )
203
+ ++ i ;
202
204
++ i ;
203
- } while (xattr_buf + i < xattr_buf + xattr_buflen - 1 );
205
+ } while (xattr_buf + i < xattr_buf + xattr_buflen - 1 );
204
206
205
- free (xattr_buf );
207
+ free (xattr_buf );
206
208
free (st_path_with_prefix );
207
- return ( 0 ) ;
209
+ return 0 ;
208
210
}
209
211
210
212
int fileextendedattribute_probe_offline_mode_supported ()
@@ -214,86 +216,83 @@ int fileextendedattribute_probe_offline_mode_supported()
214
216
215
217
void * fileextendedattribute_probe_init (void )
216
218
{
217
- /*
218
- * Initialize mutex.
219
- */
219
+ /*
220
+ * Initialize mutex.
221
+ */
220
222
pthread_mutex_t * mutex = malloc (sizeof (pthread_mutex_t ));
221
- switch (pthread_mutex_init (mutex , NULL )) {
222
- case 0 :
223
- return ((void * )mutex );
224
- default :
225
- dD ("Can't initialize mutex: errno=%u, %s." , errno , strerror (errno ));
226
- }
223
+ switch (pthread_mutex_init (mutex , NULL )) {
224
+ case 0 :
225
+ return (void * )mutex ;
226
+ default :
227
+ dD ("Can't initialize mutex: errno=%u, %s." , errno , strerror (errno ));
228
+ free (mutex );
229
+ }
227
230
#if 0
228
231
probe_setoption (PROBEOPT_VARREF_HANDLING , false, "path" );
229
232
probe_setoption (PROBEOPT_VARREF_HANDLING , false, "filename" );
230
233
#endif
231
- return ( NULL ) ;
234
+ return NULL ;
232
235
}
233
236
234
237
void fileextendedattribute_probe_fini (void * arg )
235
238
{
236
- /*
237
- * Destroy mutex.
238
- */
239
+ /*
240
+ * Destroy mutex.
241
+ */
239
242
(void ) pthread_mutex_destroy ((pthread_mutex_t * )arg );
240
243
free (arg );
241
244
}
242
245
243
246
int fileextendedattribute_probe_main (probe_ctx * ctx , void * mutex )
244
247
{
245
- SEXP_t * path , * filename , * behaviors ;
246
- SEXP_t * filepath , * attribute_ , * probe_in ;
248
+ SEXP_t * path , * filename , * behaviors ;
249
+ SEXP_t * filepath , * attribute_ , * probe_in ;
247
250
int err ;
248
- struct cbargs cbargs ;
251
+ struct cbargs cbargs ;
249
252
OVAL_FTS * ofts ;
250
253
OVAL_FTSENT * ofts_ent ;
251
254
SEXP_t gr_lastpath ;
252
255
253
- if (mutex == NULL )
254
- return PROBE_EINIT ;
256
+ if (mutex == NULL )
257
+ return PROBE_EINIT ;
255
258
256
- probe_in = probe_ctx_getobject (ctx );
259
+ probe_in = probe_ctx_getobject (ctx );
257
260
258
- path = probe_obj_getent (probe_in , "path" , 1 );
259
- filename = probe_obj_getent (probe_in , "filename" , 1 );
260
- behaviors = probe_obj_getent (probe_in , "behaviors" , 1 );
261
- filepath = probe_obj_getent (probe_in , "filepath" , 1 );
262
- attribute_ = probe_obj_getent (probe_in , "attribute_name" , 1 );
261
+ path = probe_obj_getent (probe_in , "path" , 1 );
262
+ filename = probe_obj_getent (probe_in , "filename" , 1 );
263
+ behaviors = probe_obj_getent (probe_in , "behaviors" , 1 );
264
+ filepath = probe_obj_getent (probe_in , "filepath" , 1 );
265
+ attribute_ = probe_obj_getent (probe_in , "attribute_name" , 1 );
263
266
264
267
/* we want either path+filename or filepath */
265
- if (((path == NULL || filename == NULL ) && filepath == NULL )
266
- || attribute_ == NULL )
267
- {
268
- SEXP_free (behaviors );
269
- SEXP_free (path );
270
- SEXP_free (filename );
271
- SEXP_free (filepath );
272
- SEXP_free (attribute_ );
273
-
274
- return PROBE_ENOELM ;
275
- }
268
+ if (((path == NULL || filename == NULL ) && filepath == NULL ) || attribute_ == NULL )
269
+ {
270
+ SEXP_free (behaviors );
271
+ SEXP_free (path );
272
+ SEXP_free (filename );
273
+ SEXP_free (filepath );
274
+ SEXP_free (attribute_ );
275
+ return PROBE_ENOELM ;
276
+ }
276
277
277
278
probe_filebehaviors_canonicalize (& behaviors );
278
279
279
280
switch (pthread_mutex_lock (mutex )) {
280
- case 0 :
281
- break ;
282
- default :
281
+ case 0 :
282
+ break ;
283
+ default :
283
284
dD ("Can't lock mutex(%p): %u, %s." , mutex , errno , strerror (errno ));
284
-
285
285
SEXP_free (path );
286
286
SEXP_free (filename );
287
287
SEXP_free (filepath );
288
288
SEXP_free (behaviors );
289
- SEXP_free (attribute_ );
290
-
291
- return PROBE_EFATAL ;
292
- }
289
+ SEXP_free (attribute_ );
290
+ return PROBE_EFATAL ;
291
+ }
293
292
294
- cbargs .ctx = ctx ;
293
+ cbargs .ctx = ctx ;
295
294
cbargs .error = 0 ;
296
- cbargs .attr_ent = attribute_ ;
295
+ cbargs .attr_ent = attribute_ ;
297
296
298
297
const char * prefix = getenv ("OSCAP_PROBE_ROOT" );
299
298
SEXP_init (& gr_lastpath );
@@ -305,6 +304,7 @@ int fileextendedattribute_probe_main(probe_ctx *ctx, void *mutex)
305
304
}
306
305
oval_fts_close (ofts );
307
306
}
307
+
308
308
if (!SEXP_emptyp (& gr_lastpath ))
309
309
SEXP_free_r (& gr_lastpath );
310
310
@@ -314,16 +314,15 @@ int fileextendedattribute_probe_main(probe_ctx *ctx, void *mutex)
314
314
SEXP_free (filename );
315
315
SEXP_free (filepath );
316
316
SEXP_free (behaviors );
317
- SEXP_free (attribute_ );
317
+ SEXP_free (attribute_ );
318
318
319
319
switch (pthread_mutex_unlock (mutex )) {
320
- case 0 :
321
- break ;
322
- default :
320
+ case 0 :
321
+ break ;
322
+ default :
323
323
dD ("Can't unlock mutex(%p): %u, %s." , mutex , errno , strerror (errno ));
324
+ return PROBE_EFATAL ;
325
+ }
324
326
325
- return PROBE_EFATAL ;
326
- }
327
-
328
- return err ;
327
+ return err ;
329
328
}
0 commit comments