mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-24 02:03:06 -05:00
Tests: Convert file system suid-sgid stripping test to be LibTest based.
This commit is contained in:
parent
41387df5f8
commit
48632461c1
Notes:
sideshowbarker
2024-07-18 19:01:55 +09:00
Author: https://github.com/bgianfo Commit: https://github.com/SerenityOS/serenity/commit/48632461c11 Pull-request: https://github.com/SerenityOS/serenity/pull/6687 Reviewed-by: https://github.com/linusg
2 changed files with 100 additions and 118 deletions
100
Userland/Tests/Kernel/TestKernelFilePermissions.cpp
Normal file
100
Userland/Tests/Kernel/TestKernelFilePermissions.cpp
Normal file
|
@ -0,0 +1,100 @@
|
|||
/*
|
||||
* Copyright (c) 2020-2021, the SerenityOS developers.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/String.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <LibTest/TestCase.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
TEST_CASE(test_change_file_contents)
|
||||
{
|
||||
char path[] = "/tmp/suid.XXXXXX";
|
||||
auto fd = mkstemp(path);
|
||||
EXPECT(fd != -1);
|
||||
ftruncate(fd, 0);
|
||||
EXPECT(fchmod(fd, 06755) != -1);
|
||||
|
||||
char buffer[8] {};
|
||||
write(fd, buffer, sizeof(buffer));
|
||||
|
||||
struct stat s;
|
||||
EXPECT(fstat(fd, &s) != -1);
|
||||
close(fd);
|
||||
unlink(path);
|
||||
|
||||
EXPECT(!(s.st_mode & S_ISUID));
|
||||
EXPECT(!(s.st_mode & S_ISGID));
|
||||
}
|
||||
|
||||
TEST_CASE(test_change_file_ownership)
|
||||
{
|
||||
char path[] = "/tmp/suid.XXXXXX";
|
||||
auto fd = mkstemp(path);
|
||||
EXPECT(fd != -1);
|
||||
ftruncate(fd, 0);
|
||||
EXPECT(fchmod(fd, 06755) != -1);
|
||||
|
||||
fchown(fd, getuid(), getgid());
|
||||
|
||||
struct stat s;
|
||||
EXPECT(fstat(fd, &s) != -1);
|
||||
close(fd);
|
||||
unlink(path);
|
||||
|
||||
EXPECT(!(s.st_mode & S_ISUID));
|
||||
EXPECT(!(s.st_mode & S_ISGID));
|
||||
}
|
||||
|
||||
TEST_CASE(test_change_file_permissions)
|
||||
{
|
||||
char path[] = "/tmp/suid.XXXXXX";
|
||||
auto fd = mkstemp(path);
|
||||
EXPECT(fd != -1);
|
||||
ftruncate(fd, 0);
|
||||
EXPECT(fchmod(fd, 06755) != -1);
|
||||
|
||||
fchmod(fd, 0755);
|
||||
|
||||
struct stat s;
|
||||
EXPECT(fstat(fd, &s) != -1);
|
||||
close(fd);
|
||||
unlink(path);
|
||||
|
||||
EXPECT(!(s.st_mode & S_ISUID));
|
||||
EXPECT(!(s.st_mode & S_ISGID));
|
||||
}
|
||||
|
||||
TEST_CASE(test_change_file_location)
|
||||
{
|
||||
char path[] = "/tmp/suid.XXXXXX";
|
||||
auto fd = mkstemp(path);
|
||||
EXPECT(fd != -1);
|
||||
ftruncate(fd, 0);
|
||||
EXPECT(fchmod(fd, 06755) != -1);
|
||||
|
||||
auto suid_path = Core::File::read_link(String::formatted("/proc/{}/fd/{}", getpid(), fd));
|
||||
EXPECT(suid_path.characters());
|
||||
auto new_path = String::formatted("{}.renamed", suid_path);
|
||||
|
||||
rename(suid_path.characters(), new_path.characters());
|
||||
|
||||
struct stat s;
|
||||
EXPECT(lstat(new_path.characters(), &s) != -1);
|
||||
close(fd);
|
||||
unlink(path);
|
||||
|
||||
// Renamed file should retain set-uid/set-gid permissions
|
||||
EXPECT(s.st_mode & S_ISUID);
|
||||
EXPECT(s.st_mode & S_ISGID);
|
||||
|
||||
unlink(new_path.characters());
|
||||
}
|
|
@ -1,118 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2020, the SerenityOS developers.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/String.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <assert.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static void test_change_file_contents()
|
||||
{
|
||||
char path[] = "/tmp/suid.XXXXXX";
|
||||
auto fd = mkstemp(path);
|
||||
assert(fd != -1);
|
||||
ftruncate(fd, 0);
|
||||
assert(fchmod(fd, 06755) != -1);
|
||||
|
||||
char buffer[8];
|
||||
memset(&buffer, 0, sizeof(buffer));
|
||||
write(fd, buffer, sizeof(buffer));
|
||||
|
||||
struct stat s;
|
||||
assert(fstat(fd, &s) != -1);
|
||||
close(fd);
|
||||
unlink(path);
|
||||
|
||||
assert(!(s.st_mode & S_ISUID));
|
||||
assert(!(s.st_mode & S_ISGID));
|
||||
}
|
||||
|
||||
static void test_change_file_ownership()
|
||||
{
|
||||
char path[] = "/tmp/suid.XXXXXX";
|
||||
auto fd = mkstemp(path);
|
||||
assert(fd != -1);
|
||||
ftruncate(fd, 0);
|
||||
assert(fchmod(fd, 06755) != -1);
|
||||
|
||||
fchown(fd, getuid(), getgid());
|
||||
|
||||
struct stat s;
|
||||
assert(fstat(fd, &s) != -1);
|
||||
close(fd);
|
||||
unlink(path);
|
||||
|
||||
assert(!(s.st_mode & S_ISUID));
|
||||
assert(!(s.st_mode & S_ISGID));
|
||||
}
|
||||
|
||||
static void test_change_file_permissions()
|
||||
{
|
||||
char path[] = "/tmp/suid.XXXXXX";
|
||||
auto fd = mkstemp(path);
|
||||
assert(fd != -1);
|
||||
ftruncate(fd, 0);
|
||||
assert(fchmod(fd, 06755) != -1);
|
||||
|
||||
fchmod(fd, 0755);
|
||||
|
||||
struct stat s;
|
||||
assert(fstat(fd, &s) != -1);
|
||||
close(fd);
|
||||
unlink(path);
|
||||
|
||||
assert(!(s.st_mode & S_ISUID));
|
||||
assert(!(s.st_mode & S_ISGID));
|
||||
}
|
||||
|
||||
static void test_change_file_location()
|
||||
{
|
||||
char path[] = "/tmp/suid.XXXXXX";
|
||||
auto fd = mkstemp(path);
|
||||
assert(fd != -1);
|
||||
ftruncate(fd, 0);
|
||||
assert(fchmod(fd, 06755) != -1);
|
||||
|
||||
auto suid_path = Core::File::read_link(String::formatted("/proc/{}/fd/{}", getpid(), fd));
|
||||
assert(suid_path.characters());
|
||||
auto new_path = String::formatted("{}.renamed", suid_path);
|
||||
|
||||
rename(suid_path.characters(), new_path.characters());
|
||||
|
||||
struct stat s;
|
||||
assert(lstat(new_path.characters(), &s) != -1);
|
||||
close(fd);
|
||||
unlink(path);
|
||||
|
||||
// renamed file should retain set-uid/set-gid permissions
|
||||
assert(s.st_mode & S_ISUID);
|
||||
assert(s.st_mode & S_ISGID);
|
||||
|
||||
unlink(new_path.characters());
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
#define RUNTEST(x) \
|
||||
{ \
|
||||
printf("Running " #x " ...\n"); \
|
||||
x(); \
|
||||
printf("Success!\n"); \
|
||||
}
|
||||
RUNTEST(test_change_file_contents);
|
||||
RUNTEST(test_change_file_ownership);
|
||||
RUNTEST(test_change_file_permissions);
|
||||
RUNTEST(test_change_file_location);
|
||||
printf("PASS\n");
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Reference in a new issue