Skip to content

Commit

Permalink
Add python3 support
Browse files Browse the repository at this point in the history
  • Loading branch information
safaariman committed Jun 5, 2019
1 parent 4c5af96 commit 1871a4b
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 21 deletions.
5 changes: 4 additions & 1 deletion bin/catbox
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import catbox
def run(cmd):
return subprocess.call(cmd, shell=True)


def logger(operation, path, resolved_path):
sys.stderr.write("Violation: %s %s\n" % (operation, resolved_path))


def main():
parser = optparse.OptionParser(
description='Catbox, Python sandboxing library.',
Expand Down Expand Up @@ -61,7 +63,7 @@ def main():

opts, args = parser.parse_args()
if opts.version:
print catbox.version()
print(catbox.version())

exit_val = 0
for cmd in args:
Expand All @@ -79,5 +81,6 @@ def main():

sys.exit(exit_val)


if __name__ == "__main__":
main()
22 changes: 15 additions & 7 deletions src/catbox.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static char doc_canonical[] = "Resolve and simplify given path.\n"
static PyObject *
catbox_version(PyObject *self)
{
return PyString_FromString(CATBOX_VERSION());
return PyUnicode_FromString(CATBOX_VERSION());
}

static PyObject *
Expand Down Expand Up @@ -119,11 +119,11 @@ catbox_run(PyObject *self, PyObject *args, PyObject *kwargs)
PyObject *callable = PyList_GetItem(callables, index);
if (!PyCallable_Check(callable)) {
PyObject *hook_name = PyList_GetItem(hook_names, index);
PyObject *error_string = PyString_FromFormat(
PyObject *error_string = PyUnicode_FromFormat(
"Event hook %s should be a callable function",
PyString_AsString(hook_name)
PyUnicode_AsUTF8(hook_name)
);
PyErr_SetString(PyExc_TypeError, PyString_AsString(error_string));
PyErr_SetString(PyExc_TypeError, PyUnicode_AsUTF8(error_string));
return NULL;
}
}
Expand Down Expand Up @@ -198,7 +198,7 @@ catbox_canonical(PyObject *self, PyObject *args, PyObject *kwargs)
return NULL;
}

ret = PyString_FromString(canonical);
ret = PyUnicode_FromString(canonical);
return ret;
}

Expand All @@ -210,8 +210,16 @@ static PyMethodDef methods[] = {
{ NULL, NULL, 0, NULL }
};

static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"catbox", /* m_name */
doc_catbox, /* m_doc */
-1, /* m_size */
methods, /* m_methods */
};

PyMODINIT_FUNC
initcatbox(void)
PyInit_catbox(void)
{
Py_InitModule3("catbox", methods, doc_catbox);
return PyModule_Create(&moduledef);
}
10 changes: 5 additions & 5 deletions src/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ run_python_callable(PyObject *callable, PyObject *args)
PyObject *tb;
PyErr_Fetch(&e, &val, &tb);
if (PyErr_GivenExceptionMatches(e, PyExc_SystemExit)) {
if (PyInt_Check(val)) {
if (PyLong_Check(val)) {
// Callable exits by sys.exit(n)
exit(PyInt_AsLong(val));
exit(PyLong_AsLong(val));
} else {
// Callable exits by sys.exit()
exit(2);
Expand All @@ -49,7 +49,7 @@ run_python_callable(PyObject *callable, PyObject *args)
static void
run_event_hook(struct trace_context *ctx, const char *hook_name, PyObject *args)
{
PyObject *py_hook_name = PyString_FromString(hook_name);
PyObject *py_hook_name = PyUnicode_FromString(hook_name);
if (ctx->event_hooks && PyDict_Contains(ctx->event_hooks, py_hook_name) == 1) {
PyObject *hook = PyDict_GetItem(ctx->event_hooks, py_hook_name);
run_python_callable(hook, args);
Expand Down Expand Up @@ -276,7 +276,7 @@ core_trace_loop(struct trace_context *ctx)
fprintf(stderr, "BORKBORK: nr %d, pid %d, status %x, event %d\n", ctx->nr_children, pid, status, event);

PyObject *args = PyTuple_New(1);
PyTuple_SetItem(args, 0, PyInt_FromLong(pid));
PyTuple_SetItem(args, 0, PyLong_FromLong(pid));
run_event_hook(ctx, "child_died_unexpectedly", args);
}

Expand Down Expand Up @@ -413,7 +413,7 @@ catbox_core_run(struct trace_context *ctx)

// Run child_initialized hook before notifying child to continue.
PyObject *args = PyTuple_New(1);
PyTuple_SetItem(args, 0, PyInt_FromLong(child_pid));
PyTuple_SetItem(args, 0, PyLong_FromLong(child_pid));
run_event_hook(ctx, "child_initialized", args);

// Start watchdog process
Expand Down
2 changes: 1 addition & 1 deletion src/paths.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ make_pathlist(PyObject *paths)
free_pathlist(pathlist);
return NULL;
}
str = PyString_AsString(item);
str = PyUnicode_AsUTF8(item);
if (!str) {
Py_DECREF(item);
free_pathlist(pathlist);
Expand Down
9 changes: 4 additions & 5 deletions src/retval.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ static PyMemberDef members[] = {
};

static PyTypeObject RetVal_type = {
PyObject_HEAD_INIT(NULL)
0, /* ob_size */
PyVarObject_HEAD_INIT(NULL, 0)
"catbox.RetVal", /* tp_name */
sizeof(RetVal), /* tp_basicsize */
0, /* tp_itemsize */
Expand Down Expand Up @@ -100,9 +99,9 @@ catbox_retval_add_violation(struct trace_context *ctx, const char *operation, co
PyObject *item;

item = PyTuple_New(3);
PyTuple_SetItem(item, 0, PyString_FromString(operation));
PyTuple_SetItem(item, 1, PyString_FromString(path));
PyTuple_SetItem(item, 2, PyString_FromString(canonical));
PyTuple_SetItem(item, 0, PyUnicode_FromString(operation));
PyTuple_SetItem(item, 1, PyUnicode_FromString(path));
PyTuple_SetItem(item, 2, PyUnicode_FromString(canonical));
PyList_Append(ret->violations, item);

if (ctx->logger) {
Expand Down
4 changes: 2 additions & 2 deletions src/syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ return 0;
// uid_t uid = (uid_t)regs.ecx;
// gid_t gid = (gid_t)regs.edx;
// PyObject* dict = PyObject_GetAttrString( ctx->ret_object, "ownerships" );
// PyDict_SetItem( dict, PyString_FromString(path), PyTuple_Pack( 2, PyInt_FromLong(uid), PyInt_FromLong(gid)) );
// PyDict_SetItem( dict, PyUnicode_FromString(path), PyTuple_Pack( 2, PyLong_FromLong(uid), PyLong_FromLong(gid)) );
return 1;
}
if(0 & LOG_MODE) {
Expand All @@ -393,7 +393,7 @@ return 0;
// const char* path = get_str(pid, regs.ebx);
// mode_t mode = (mode_t)regs.ecx;
// PyObject* dict = PyObject_GetAttrString( ctx->ret_object, "modes" );
// PyDict_SetItem( dict, PyString_FromString(path), PyInt_FromLong(mode) );
// PyDict_SetItem( dict, PyUnicode_FromString(path), PyLong_FromLong(mode) );
return 1;
}
if(0 & FAKE_ID) {
Expand Down

0 comments on commit 1871a4b

Please sign in to comment.