Skip to content

Commit db2e027

Browse files
committed
- Fix python bindings to correctly handle passing files to auparse
1 parent e416eb5 commit db2e027

4 files changed

Lines changed: 77 additions & 7 deletions

File tree

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
- Code cleanups
55
- Improve auplugin_fgets performance
66
- Update syscalls and io_uring tables for the 6.19 kernel
7+
- Fix python bindings to correctly handle passing files to auparse
78

89
4.1.2
910
- Use runstatedir to guide the whole audit project to the run directory

auparse/test/auparse_test.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,44 @@ def feed_callback(au, cb_event_type, event_cnt, interpret=False):
275275
(FIELDS_EXPECTED, walked_fields))
276276
print("Test 11 Done\n")
277277

278+
print("Starting Test 12, descriptor source...")
279+
fd = os.open(srcdir + "/test.log", os.O_RDONLY)
280+
au = auparse.AuParser(auparse.AUSOURCE_DESCRIPTOR, fd)
281+
if not au.parse_next_event():
282+
print("Error parsing descriptor")
283+
else:
284+
if not au.first_record():
285+
print("Error getting first record")
286+
else:
287+
print("descriptor type=%d(%s)" % (au.get_type(),
288+
au.get_type_name()))
289+
au = None
290+
os.lseek(fd, 0, os.SEEK_SET)
291+
print("descriptor fd open=%s" % (os.read(fd, 4) != b""))
292+
os.close(fd)
293+
print("Test 12 Done\n")
294+
295+
print("Starting Test 13, file pointer source...")
296+
f = open(srcdir + "/test.log")
297+
au = auparse.AuParser(auparse.AUSOURCE_FILE_POINTER, f)
298+
if not au.parse_next_event():
299+
print("Error parsing file pointer")
300+
else:
301+
if not au.first_record():
302+
print("Error getting first record")
303+
else:
304+
print("file pointer type=%d(%s)" % (au.get_type(),
305+
au.get_type_name()))
306+
au = None
307+
if f.closed:
308+
print("file pointer open=0")
309+
else:
310+
f.seek(0)
311+
print("file pointer open=%s" % (f.read(4) != ""))
312+
f.close()
313+
print("Test 13 Done\n")
314+
278315
print("Finished non-admin tests\n")
279316

280317
au = None
281318
sys.exit(0)
282-

auparse/test/auparse_test.ref.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,5 +1417,15 @@
14171417

14181418
Test 11 Done
14191419

1420+
Starting Test 12, descriptor source...
1421+
descriptor type=1400(AVC)
1422+
descriptor fd open=True
1423+
Test 12 Done
1424+
1425+
Starting Test 13, file pointer source...
1426+
file pointer type=1400(AVC)
1427+
file pointer open=True
1428+
Test 13 Done
1429+
14201430
Finished non-admin tests
14211431

bindings/python/auparse_python.c

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include <errno.h>
66
#include <time.h>
7+
#include <stdint.h>
8+
#include <unistd.h>
79
#include "auparse.h"
810

911
/*
@@ -442,31 +444,52 @@ AuParser_init(AuParser *self, PyObject *args, PyObject *kwds)
442444
} break;
443445
case AUSOURCE_DESCRIPTOR: {
444446
long fd;
447+
int dup_fd;
448+
445449
fd = PyObject_AsFileDescriptor(source);
446450
if (fd < 0) {
447451
PyErr_SetString(PyExc_ValueError, "source must be resolvable to a file descriptor when source_type is AUSOURCE_DESCRIPTOR");
448452
return -1;
449453
}
450-
if ((self->au = auparse_init(source_type, (const void *)fd)) == NULL) {
454+
dup_fd = dup(fd);
455+
if (dup_fd < 0) {
456+
PyErr_SetFromErrno(PyExc_EnvironmentError);
457+
return -1;
458+
}
459+
if ((self->au = auparse_init(source_type,
460+
(const void *)(intptr_t)dup_fd)) == NULL) {
461+
close(dup_fd);
451462
PyErr_SetFromErrno(PyExc_EnvironmentError);
452463
return -1;
453464
}
454465
} break;
455466
case AUSOURCE_FILE_POINTER: {
456467
FILE* fp;
468+
int fd;
469+
int dup_fd;
457470

458471
if (!PyFile_Check(source)) {
459472
PyErr_SetString(PyExc_ValueError, "source must be a file object when source_type is AUSOURCE_FILE_POINTER");
460473
return -1;
461474
}
462-
if ((fp = PYFILE_ASFILE(source)) == NULL) {
475+
fd = PyObject_AsFileDescriptor(source);
476+
if (fd < 0) {
463477
PyErr_SetString(PyExc_TypeError, "source must be open file when source_type is AUSOURCE_FILE_POINTER");
464478
return -1;
465-
}
479+
}
480+
dup_fd = dup(fd);
481+
if (dup_fd < 0) {
482+
PyErr_SetFromErrno(PyExc_EnvironmentError);
483+
return -1;
484+
}
485+
fp = fdopen(dup_fd, "r");
486+
if (fp == NULL) {
487+
close(dup_fd);
488+
PyErr_SetFromErrno(PyExc_EnvironmentError);
489+
return -1;
490+
}
466491
const char *filename = NULL;
467492
#if PY_MAJOR_VERSION < 3
468-
int fd = fileno(fp);
469-
fp = fdopen(fd, "r");
470493
/* PyFile_Name is available in Python 2 */
471494
filename = PYSTR_ASSTRING(PyFile_Name(source));
472495
#else
@@ -481,6 +504,7 @@ AuParser_init(AuParser *self, PyObject *args, PyObject *kwds)
481504
PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
482505
else
483506
PyErr_SetFromErrno(PyExc_IOError);
507+
fclose(fp);
484508
return -1;
485509
}
486510
} break;
@@ -2535,4 +2559,3 @@ PyInit_auparse(void)
25352559

25362560
return m;
25372561
}
2538-

0 commit comments

Comments
 (0)