[kernel] Add automatic verification to syscalls

Since we have a DSL for specifying syscalls, we can create a verificaton
method for each syscall that can cover most argument (and eventually
capability) verification instead of doing it piecemeal in each syscall
implementation, which can be more error-prone.

Now a new _syscall_verify_* function exists for every syscall, which
calls the real implementation. The syscall table for the syscall handler
now maps to these verify functions.

Other changes:

- Updated the definition grammar to allow options to have a "key:value"
  style, to eventually support capabilities.
- Added an "optional" option for parameters that says a syscall will
  accept a null value.
- Some bonnibel fixes, as definition file changes weren't always
  properly causing updates in the build dep graph.
- The syscall implementation function signatures are no longer exposed
  in syscall.h. Also, the unused syscall enum has been removed.
This commit is contained in:
Justin C. Miller
2022-01-16 15:11:58 -08:00
parent e845379b1e
commit e0246df26b
21 changed files with 415 additions and 219 deletions

View File

@@ -48,6 +48,7 @@ kernel = module("kernel",
"syscall.cpp.cog",
"syscall.h.cog",
"syscall.s",
"syscall_verify.cpp.cog",
"syscalls.inc.cog",
"syscalls/channel.cpp",
"syscalls/endpoint.cpp",
@@ -68,10 +69,15 @@ from os.path import join
layout = join(source_root, "definitions/memory_layout.yaml")
sysconf = join(source_root, "definitions/sysconf.yaml")
definitions = glob('definitions/**/*.def', recursive=True)
definitions = glob(join(source_root, 'definitions/**/*.def'), recursive=True)
kernel.add_depends(["memory.h.cog"], [layout])
kernel.add_depends(["sysconf.h.cog"], [sysconf])
kernel.add_depends(["syscall.cpp.cog", "syscall.h.cog", "syscalls.inc.cog"], definitions)
kernel.add_depends([
"syscall.cpp.cog",
"syscall.h.cog",
"syscalls.inc.cog",
"syscall_verify.cpp.cog",
], definitions)
kernel.variables['ldflags'] = ["${ldflags}", "-T", "${source_root}/src/kernel/kernel.ld"]

View File

@@ -21,9 +21,39 @@ ctx = Context(definitions_path)
ctx.parse("syscalls.def")
syscalls = ctx.interfaces['syscalls']
cog.outl(f"constexpr size_t num_syscalls = {len(syscalls.methods)};")
]]]*/
/// [[[end]]]
namespace syscalls
{
/*[[[cog code generation
last_scope = None
for id, scope, method in syscalls.methods:
if scope != last_scope:
cog.outl()
last_scope = scope
if scope:
name = f"{scope.name}_{method.name}"
else:
name = method.name
args = []
if method.constructor:
args.append("j6_handle_t *handle")
elif not method.static:
args.append("j6_handle_t handle")
for param in method.params:
for type, suffix in param.type.c_names(param.options):
args.append(f"{type} {param.name}{suffix}")
cog.outl(f"""j6_status_t _syscall_verify_{name} ({", ".join(args)});""")
]]]*/
//[[[end]]]
}
uintptr_t syscall_registry[num_syscalls] __attribute__((section(".syscall_registry")));
void
@@ -44,8 +74,8 @@ syscall_initialize()
else:
name = method.name
cog.outl(f"syscall_registry[{id}] = reinterpret_cast<uintptr_t>(syscalls::{name});")
cog.outl(f"""log::debug(logs::syscall, "Enabling syscall {id:x} as {name}");""")
cog.outl(f"syscall_registry[{id}] = reinterpret_cast<uintptr_t>(syscalls::_syscall_verify_{name});")
cog.outl(f"""log::debug(logs::syscall, "Enabling syscall {id:02x} as {name}");""")
cog.outl("")
]]]*/
//[[[end]]]

View File

@@ -13,48 +13,9 @@ ctx = Context(definitions_path)
ctx.parse("syscalls.def")
syscalls = ctx.interfaces["syscalls"]
cog.outl(f"constexpr size_t num_syscalls = {len(syscalls.methods)};")
]]]*/
/// [[[end]]]
enum class syscall : uint64_t
{
/*[[[cog code generation
for id, scope, method in syscalls.methods:
if scope:
name = f"{scope.name}_{method.name}"
else:
name = method.name
cog.outl(f"{name:20} = {id},")
]]]*/
//[[[end]]]
};
void syscall_initialize();
extern "C" void syscall_enable();
namespace syscalls
{
/*[[[cog code generation
for id, scope, method in syscalls.methods:
if scope:
name = f"{scope.name}_{method.name}"
else:
name = method.name
args = []
if method.constructor:
args.append("j6_handle_t *handle")
elif not method.static:
args.append("j6_handle_t handle")
for param in method.params:
for type, suffix in param.type.c_names(param.options):
args.append(f"{type} {param.name}{suffix}")
cog.outl(f"""j6_status_t {name} ({", ".join(args)});""")
]]]*/
//[[[end]]]
}

View File

@@ -0,0 +1,73 @@
// vim: ft=cpp
#include <stdint.h>
#include <arch/memory.h>
#include <j6/errors.h>
#include <j6/types.h>
namespace {
template <typename T>
__attribute__((always_inline))
inline bool check_refparam(const T* param, bool optional) {
return reinterpret_cast<uintptr_t>(param) < arch::kernel_offset &&
(optional || param);
}
}
namespace syscalls {
/*[[[cog code generation
from definitions.context import Context
ctx = Context(definitions_path)
ctx.parse("syscalls.def")
syscalls = ctx.interfaces["syscalls"]
cbool = {True: "true", False: "false"}
for id, scope, method in syscalls.methods:
if scope:
name = f"{scope.name}_{method.name}"
else:
name = f"{method.name}"
args = []
argdefs = []
refparams = []
if method.constructor:
argdefs.append("j6_handle_t *handle")
args.append("handle")
refparams.append(("handle", False))
elif not method.static:
argdefs.append("j6_handle_t handle")
args.append("handle")
for param in method.params:
for type, suffix in param.type.c_names(param.options):
arg = f"{param.name}{suffix}"
argdefs.append(f"{type} {arg}")
args.append(arg)
if param.refparam:
subs = param.type.c_names(param.options)
refparams.append((param.name + subs[0][1], param.optional))
if param.outparam:
for sub in subs[1:]:
refparams.append((param.name + sub[1], param.optional))
cog.outl(f"""extern j6_status_t {name} ({", ".join(argdefs)});""")
cog.outl(f"""j6_status_t _syscall_verify_{name} ({", ".join(argdefs)}) {{""")
for pname, optional in refparams:
cog.outl(f" if (!check_refparam({pname}, {cbool[optional]}))")
cog.outl( " return j6_err_invalid_arg;")
cog.outl()
cog.outl(f""" return syscalls::{name}({", ".join(args)});""")
cog.outl("}")
cog.outl("\n")
]]]*/
//[[[end]]]
} // namespace syscalls

View File

@@ -29,7 +29,9 @@ endpoint_send(j6_handle_t handle, uint64_t tag, const void * data, size_t data_l
j6_status_t
endpoint_receive(j6_handle_t handle, uint64_t * tag, void * data, size_t * data_len, uint64_t timeout)
{
if (!tag || !data_len || (*data_len && !data))
// Data is marked optional, but we need the length, and if length > 0,
// data is not optional.
if (!data_len || (*data_len && !data))
return j6_err_invalid_arg;
endpoint *e = get_handle<endpoint>(handle);
@@ -46,7 +48,7 @@ endpoint_receive(j6_handle_t handle, uint64_t * tag, void * data, size_t * data_
j6_status_t
endpoint_sendrecv(j6_handle_t handle, uint64_t * tag, void * data, size_t * data_len, uint64_t timeout)
{
if (!tag || (*tag & j6_tag_system_flag))
if (*tag & j6_tag_system_flag)
return j6_err_invalid_arg;
endpoint *e = get_handle<endpoint>(handle);

View File

@@ -13,9 +13,6 @@ namespace syscalls {
j6_status_t
kobject_koid(j6_handle_t handle, j6_koid_t *koid)
{
if (koid == nullptr)
return j6_err_invalid_arg;
kobject *obj = get_handle<kobject>(handle);
if (!obj)
return j6_err_invalid_arg;

View File

@@ -19,9 +19,6 @@ namespace syscalls {
j6_status_t
log(const char *message)
{
if (message == nullptr)
return j6_err_invalid_arg;
thread &th = thread::current();
log::info(logs::syscall, "Message[%llx]: %s", th.koid(), message);
return j6_status_ok;
@@ -36,23 +33,24 @@ noop()
}
j6_status_t
system_get_log(j6_handle_t sys, void *buffer, size_t *size)
system_get_log(j6_handle_t sys, void *buffer, size_t *buffer_len)
{
if (!size || (*size && !buffer))
// Buffer is marked optional, but we need the length, and if length > 0,
// buffer is not optional.
if (!buffer_len || (*buffer_len && !buffer))
return j6_err_invalid_arg;
size_t orig_size = *size;
*size = g_logger.get_entry(buffer, *size);
size_t orig_size = *buffer_len;
*buffer_len = g_logger.get_entry(buffer, *buffer_len);
if (!g_logger.has_log())
system::get().deassert_signal(j6_signal_system_has_log);
return (*size > orig_size) ? j6_err_insufficient : j6_status_ok;
return (*buffer_len > orig_size) ? j6_err_insufficient : j6_status_ok;
}
j6_status_t
system_bind_irq(j6_handle_t sys, j6_handle_t endp, unsigned irq)
{
// TODO: check capabilities on sys handle
endpoint *e = get_handle<endpoint>(endp);
if (!e) return j6_err_invalid_arg;
@@ -65,9 +63,6 @@ system_bind_irq(j6_handle_t sys, j6_handle_t endp, unsigned irq)
j6_status_t
system_map_phys(j6_handle_t handle, j6_handle_t * area, uintptr_t phys, size_t size, uint32_t flags)
{
// TODO: check capabilities on sys handle
if (!area) return j6_err_invalid_arg;
// TODO: check to see if frames are already used? How would that collide with
// the bootloader's allocated pages already being marked used?
if (!(flags & vm_flags::mmio))
@@ -82,7 +77,6 @@ system_map_phys(j6_handle_t handle, j6_handle_t * area, uintptr_t phys, size_t s
j6_status_t
system_request_iopl(j6_handle_t handle, unsigned iopl)
{
// TODO: check capabilities on sys handle
if (iopl != 0 && iopl != 3)
return j6_err_invalid_arg;

View File

@@ -56,9 +56,6 @@ vma_unmap(j6_handle_t handle, j6_handle_t proc)
j6_status_t
vma_resize(j6_handle_t handle, size_t *size)
{
if (!size)
return j6_err_invalid_arg;
vm_area *a = get_handle<vm_area>(handle);
if (!a) return j6_err_invalid_arg;