Skip to content
Snippets Groups Projects
Commit 6321814e authored by schneider's avatar schneider
Browse files

Merge branch 'moar_blacklist' into 'master'

pycardium: check file-blacklist in os.unlink and os.rename, too

See merge request card10/firmware!167
parents 03d02219 63e60314
No related branches found
No related tags found
1 merge request!167pycardium: check file-blacklist in os.unlink and os.rename, too
Pipeline #3075 passed
......@@ -11,24 +11,7 @@
#include "py/mperrno.h"
#include "epicardium.h"
#include <strings.h>
bool filename_restricted(const char *fname)
{
// files that cannot be opened in write modes
const char *const forbidden_files[] = {
"cardio.bin", "menu.py", "main.py", "cardio.cfg"
};
for (int i = 0;
i < sizeof(forbidden_files) / sizeof(forbidden_files[0]);
i++) {
if (strcasecmp(fname, forbidden_files[i]) == 0) {
return true;
}
}
return false;
}
#include "os.h"
extern const mp_obj_type_t mp_type_textio;
#if MICROPY_PY_IO_FILEIO
......@@ -167,8 +150,8 @@ STATIC mp_obj_t file_open(const mp_obj_type_t *type, mp_arg_val_t *args)
const char *fname = mp_obj_str_get_str(args[0].u_obj);
if (potentially_critical_access && filename_restricted(fname)) {
mp_raise_OSError(-EPERM);
if (potentially_critical_access && pycrd_filename_restricted(fname)) {
mp_raise_OSError(-EACCES);
}
int res = epic_file_open(fname, modeString);
......
......@@ -4,6 +4,27 @@
#include "py/runtime.h"
#include <string.h>
#include <strings.h>
#include <stdbool.h>
#include "os.h"
bool pycrd_filename_restricted(const char *fname)
{
// files that cannot be opened in write modes
const char *const forbidden_files[] = {
"card10.bin", "menu.py", "main.py", "card10.cfg"
};
for (int i = 0;
i < sizeof(forbidden_files) / sizeof(forbidden_files[0]);
i++) {
if (strcasecmp(fname, forbidden_files[i]) == 0) {
return true;
}
}
return false;
}
static mp_obj_t mp_os_exit(size_t n_args, const mp_obj_t *args)
{
......@@ -89,7 +110,10 @@ static MP_DEFINE_CONST_FUN_OBJ_1(listdir_obj, mp_os_listdir);
static mp_obj_t mp_os_unlink(mp_obj_t py_path)
{
const char *path = mp_obj_str_get_str(py_path);
int rc = epic_file_unlink(path);
if (pycrd_filename_restricted(path)) {
mp_raise_OSError(-EACCES);
}
int rc = epic_file_unlink(path);
if (rc < 0) {
mp_raise_OSError(-rc);
......@@ -114,7 +138,11 @@ static mp_obj_t mp_os_rename(mp_obj_t py_oldp, mp_obj_t py_newp)
{
const char *oldp = mp_obj_str_get_str(py_oldp);
const char *newp = mp_obj_str_get_str(py_newp);
int rc = epic_file_rename(oldp, newp);
if (pycrd_filename_restricted(oldp) ||
pycrd_filename_restricted(newp)) {
mp_raise_OSError(-EACCES);
}
int rc = epic_file_rename(oldp, newp);
if (rc < 0) {
mp_raise_OSError(-rc);
......
#ifndef PYCARDIUM_MODULES_OS_H_INCLUDED
#define PYCARDIUM_MODULES_OS_H_INCLUDED
bool pycrd_filename_restricted(const char *fname);
#endif//PYCARDIUM_MODULES_OS_H_INCLUDED
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment