Skip to content

Commit 8518623

Browse files
committed
fix modules memory being overwritten
add a few more syscalls and stubs
1 parent da7a5a5 commit 8518623

File tree

18 files changed

+159
-52
lines changed

18 files changed

+159
-52
lines changed

kernel/interfaces/lib/string.cppm

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,14 @@ export namespace lib
144144
}
145145

146146
template<typename Type>
147-
constexpr Type oct2int(std::string_view str)
147+
constexpr Type oct2int(std::span<char> data)
148148
{
149149
Type value = 0;
150-
auto ptr = str.data();
151-
auto len = str.length();
150+
auto ptr = data.data();
151+
auto len = data.size_bytes();
152152

153-
while (ptr < str.end() && *ptr && len > 0)
153+
const auto end = ptr + len;
154+
while (ptr < end && *ptr && len > 0)
154155
{
155156
value = value * 8 + (*ptr++ - '0');
156157
len--;

kernel/interfaces/system/scheduler.cppm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ export namespace sched
198198
group *group_for(pid_t pgid);
199199
session *session_for(pid_t sid);
200200

201-
group *create_group(process *proc);
202-
session *create_session(group *grp);
201+
group *create_group(pid_t pgid);
202+
session *create_session(pid_t sid);
203203

204204
bool change_group(process *proc, group *grp);
205205
bool change_session(group *grp, session *sess);

kernel/interfaces/system/syscall/misc.cppm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ import std;
88
export namespace syscall::misc
99
{
1010
int uname(struct utsname __user *buf);
11+
int reboot(int magic, int magic2, int op, void __user *arg);
1112
std::ssize_t getrandom(void __user *buf, std::size_t buflen, unsigned int flags);
1213
} // export namespace syscall::misc

kernel/interfaces/system/syscall/proc.cppm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,15 @@ export namespace syscall::proc
2323
pid_t getpgid(pid_t pid);
2424
int setpgid(pid_t pid, pid_t pgid);
2525

26+
pid_t setsid();
27+
2628
int getgroups(int size, gid_t __user *list);
2729
int setgroups(std::size_t size, const gid_t __user *list);
2830

2931
int set_tid_address(int __user *tidptr);
3032

33+
mode_t umask(mode_t mask);
34+
3135
int sigaction(int signum, const struct sigaction __user *act, struct sigaction __user *oldact);
3236
int sigprocmask(int how, const struct sigset_t __user *set, struct sigset_t __user *oldset, std::size_t sigsetsize);
3337
int rseq(struct rseq __user *rseq, std::uint32_t rseq_len, int flags, std::uint32_t sig);

kernel/interfaces/system/syscall/vfs.cppm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,6 @@ export namespace syscall::vfs
4646

4747
int pipe2(int __user *pipefd, int flags);
4848
int pipe(int __user *pipefd);
49+
50+
int socket(int domain, int type, int protocol);
4951
} // export namespace syscall::vfs

kernel/source/arch/x86_64/system/syscall.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,27 +50,31 @@ namespace x86_64::syscall
5050
[32] = { "dup", vfs::dup },
5151
[33] = { "dup2", vfs::dup2 },
5252
[39] = { "getpid", proc::getpid },
53+
[41] = { "socket", vfs::socket },
5354
[56] = { "clone", proc::clone },
5455
[57] = { "fork", proc::fork },
5556
[58] = { "vfork", proc::vfork },
5657
[63] = { "uname", misc::uname },
5758
[72] = { "fcntl", vfs::fcntl },
5859
[79] = { "getcwd", vfs::getcwd, [](std::uintptr_t val) { return val == 0; } },
5960
[85] = { "creat", vfs::creat },
61+
[95] = { "umask", proc::umask },
6062
[96] = { "gettimeofday", chrono::gettimeofday },
6163
[102] = { "getuid", proc::getuid },
6264
[104] = { "getgid", proc::getgid },
6365
[107] = { "geteuid", proc::geteuid },
6466
[108] = { "getegid", proc::getegid },
6567
[109] = { "setpgid", proc::setpgid },
6668
[110] = { "getppid", proc::getppid },
69+
[112] = { "setsid", proc::setsid },
6770
[115] = { "getgroups", proc::getgroups },
6871
[116] = { "setgroups", proc::setgroups },
6972
[118] = { "getresuid", proc::getresuid },
7073
[120] = { "getresgid", proc::getresgid },
7174
[121] = { "getpgid", proc::getpgid },
7275
[158] = { "arch_prctl", arch::arch_prctl },
7376
[164] = { "settimeofday", chrono::settimeofday },
77+
[169] = { "reboot", misc::reboot },
7478
[186] = { "gettid", proc::gettid },
7579
[202] = { "futex", proc::futex },
7680
[218] = { "set_tid_address", proc::set_tid_address },

kernel/source/boot/kernel.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ extern "C"
1919

2020
sched::thread *thread = nullptr;
2121
{
22-
lib::path_view path { "/usr/bin/bash" };
22+
lib::path_view path { "/usr/sbin/init" };
2323
// lib::info("loading {}", path);
2424

2525
auto ret = vfs::resolve(std::nullopt, path);
@@ -38,8 +38,8 @@ extern "C"
3838
auto pmap = std::make_shared<vmm::pagemap>();
3939
auto proc = sched::process::create(nullptr, pmap);
4040

41-
proc->ruid = proc->euid = proc->suid = 1000;
42-
proc->rgid = proc->egid = proc->sgid = 1000;
41+
// proc->ruid = proc->euid = proc->suid = 1000;
42+
// proc->rgid = proc->egid = proc->sgid = 1000;
4343

4444
lib::path_view tty_path { "/dev/tty0" };
4545
ret = vfs::resolve(std::nullopt, tty_path);
@@ -61,10 +61,10 @@ extern "C"
6161
.interp = { },
6262
.argv = { path.basename().data() },
6363
.envp = {
64-
"TERM=linux",
65-
"USER=ilobilix",
66-
"HOME=/home/ilobilix",
67-
"PATH=/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin"
64+
// "TERM=linux",
65+
// "USER=ilobilix",
66+
// "HOME=/home/ilobilix",
67+
// "PATH=/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin"
6868
}
6969
}, proc);
7070

kernel/source/deps/uacpi.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,8 @@ extern "C"
240240
{
241241
auto &pmap = vmm::kernel_pagemap;
242242

243-
// const auto psize = vmm::pagemap::max_page_size(len);
244-
const auto psize = vmm::page_size::small;
243+
const auto psize = vmm::pagemap::max_page_size(addr, len);
244+
// const auto psize = vmm::page_size::small;
245245
const auto npsize = vmm::pagemap::from_page_size(psize);
246246

247247
const auto paddr = lib::align_down(addr, npsize);

kernel/source/drivers/fs/dev/tty.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,14 @@ namespace fs::dev::tty
142142

143143
std::shared_ptr<instance> create_instance(std::uint32_t minor) override
144144
{
145-
lib::debug("tty: creating test instance with minor {}", minor);
145+
// lib::debug("tty: creating test instance with minor {}", minor);
146146
return std::make_shared<test_instance>(this, minor);
147147
}
148148

149149
void destroy_instance(std::shared_ptr<instance> inst) override
150150
{
151-
lib::debug("tty: destroying test instance with minor {}", inst->minor);
151+
lib::unused(inst);
152+
// lib::debug("tty: destroying test instance with minor {}", inst->minor);
152153
}
153154

154155
int ioctl(std::shared_ptr<instance> inst, unsigned long request, lib::uptr_or_addr argp) override
@@ -206,7 +207,7 @@ namespace fs::dev::tty
206207
}
207208
self->private_data = inst;
208209

209-
lib::debug("tty: opened ({}, {}) for pid {}", major(rdev), minor(rdev), self->pid);
210+
// lib::debug("tty: opened ({}, {}) for pid {}", major(rdev), minor(rdev), self->pid);
210211
return true;
211212
}
212213

@@ -239,8 +240,8 @@ namespace fs::dev::tty
239240
}
240241
self->private_data.reset();
241242

242-
const auto rdev = self->path.dentry->inode->stat.st_rdev;
243-
lib::debug("tty: closed ({}, {}) for pid {}", major(rdev), minor(rdev), self->pid);
243+
// const auto rdev = self->path.dentry->inode->stat.st_rdev;
244+
// lib::debug("tty: closed ({}, {}) for pid {}", major(rdev), minor(rdev), self->pid);
244245
return true;
245246
}
246247

kernel/source/drivers/initramfs.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ namespace initramfs
6666

6767
auto current = reinterpret_cast<header *>(data.data());
6868
while (magic == std::string_view { current->magic, 6 } &&
69-
current < reinterpret_cast<header *>(data.data() + data.size()))
69+
current <= reinterpret_cast<header *>(data.data() + data.size() - sizeof(header)))
7070
{
7171
if (current->name[0] == '\0')
7272
break;
@@ -87,12 +87,12 @@ namespace initramfs
8787

8888
const auto linkname { get_string(current->linkname) };
8989

90-
const auto mode = lib::oct2int<mode_t>(get_string(current->mode));
91-
const auto size = lib::oct2int<std::size_t>(get_string(current->size));
92-
const auto mtim = lib::oct2int<time_t>(get_string(current->mtime));
90+
const auto mode = lib::oct2int<mode_t>(current->mode);
91+
const auto size = lib::oct2int<std::size_t>(current->size);
92+
const auto mtim = lib::oct2int<time_t>(current->mtime);
9393

94-
const auto devmajor = lib::oct2int<time_t>(get_string(current->devmajor));
95-
const auto devminor = lib::oct2int<time_t>(get_string(current->devminor));
94+
const auto devmajor = lib::oct2int<time_t>(current->devmajor);
95+
const auto devminor = lib::oct2int<time_t>(current->devminor);
9696
const dev_t dev = vfs::dev::makedev(devmajor, devminor);
9797

9898
std::shared_ptr<vfs::inode> inode;
@@ -223,7 +223,10 @@ namespace initramfs
223223
inode->stat.st_mtim = timespec { mtim, 0 };
224224

225225
next:
226-
current = reinterpret_cast<header *>(reinterpret_cast<std::uintptr_t>(current) + 512 + lib::align_up(size, 512zu));
226+
current = reinterpret_cast<header *>(
227+
reinterpret_cast<std::uintptr_t>(current) +
228+
512 + lib::align_up(size, 512zu)
229+
);
227230
}
228231
return true;
229232
}
@@ -254,7 +257,7 @@ namespace initramfs
254257
lib::panic("could not find initramfs");
255258

256259
std::span<std::byte> data {
257-
reinterpret_cast<std::byte *>(lib::tohh(module->address)),
260+
reinterpret_cast<std::byte *>(module->address),
258261
module->size
259262
};
260263

0 commit comments

Comments
 (0)