Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hardening #187

Merged
merged 5 commits into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 62 additions & 15 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,92 @@ on:

jobs:
Linux:
strategy:
fail-fast: false
matrix:
volume:
- /tmp
- .
- /
asroot:
- name: ''
sudo: ''
- name: ' as root'
sudo: 'sudo '

name: Linux ${{ matrix.volume }}${{ matrix.asroot.name }}
runs-on: ubuntu-22.04

steps:
- uses: actions/checkout@v3
- run: sudo apt-get install -y libparted-dev libudev-dev
- run: make all extra

- if: matrix.volume == '/'
run: sudo chmod a+w /

- run: ./f3write -V
- run: ./f3write --help
- run: ./f3write -s 2 -e 4 -w 50000 /tmp
- run: ${{ matrix.asroot.sudo }}./f3write -s 2 -e 4 -w 50000 ${{ matrix.volume }}

- run: stat /tmp/2.h2w
- run: stat /tmp/3.h2w
- run: stat /tmp/4.h2w
- run: stat ${{ matrix.volume }}/2.h2w
- run: stat ${{ matrix.volume }}/3.h2w
- run: stat ${{ matrix.volume }}/4.h2w

- run: ./f3read -V
- run: ./f3read --help
- run: ./f3read -s 2 -e 4 -r 50000 /tmp
- run: ${{ matrix.asroot.sudo }}./f3read -s 2 -e 4 -r 50000 ${{ matrix.volume }}

MacOS:
strategy:
fail-fast: false
matrix:
volume:
- /tmp
- .
# MacOS denies `sudo chmod a+w /`
#- /
asroot:
- name: ''
sudo: ''
- name: ' as root'
sudo: 'sudo '

name: MacOS ${{ matrix.volume }}${{ matrix.asroot.name }}
runs-on: macos-12

steps:
- uses: actions/checkout@v3
- run: brew install argp-standalone
- run: make

- run: ./f3write -V
- run: ./f3write --help
- run: ./f3write -s 2 -e 4 -w 50000 /tmp
- run: ${{ matrix.asroot.sudo }}./f3write -s 2 -e 4 -w 50000 ${{ matrix.volume }}

- run: stat /tmp/2.h2w
- run: stat /tmp/3.h2w
- run: stat /tmp/4.h2w
- run: stat ${{ matrix.volume }}/2.h2w
- run: stat ${{ matrix.volume }}/3.h2w
- run: stat ${{ matrix.volume }}/4.h2w

- run: ./f3read -V
- run: ./f3read --help
- run: ./f3read -s 2 -e 4 -r 50000 /tmp
- run: ${{ matrix.asroot.sudo }}./f3read -s 2 -e 4 -r 50000 ${{ matrix.volume }}

Cygwin:
strategy:
fail-fast: false
matrix:
volume:
- cygwin: /cygdrive/c
windows: 'C:'
- cygwin: .
windows: .
- cygwin: /
windows: 'C:\cygwin'

name: Cygwin ${{ matrix.volume.cygwin }}
runs-on: windows-2022

steps:
- uses: actions/checkout@v3

Expand All @@ -59,12 +106,12 @@ jobs:

- run: '& .\f3write.exe -V'
- run: '& .\f3write.exe --help'
- run: '& .\f3write.exe -s 2 -e 4 -w 50000 /cygdrive/c'
- run: '& .\f3write.exe -s 2 -e 4 -w 50000 ${{ matrix.volume.cygwin }}'

- run: 'Get-Item C:\2.h2w'
- run: 'Get-Item C:\3.h2w'
- run: 'Get-Item C:\4.h2w'
- run: 'Get-Item ${{ matrix.volume.windows }}\2.h2w'
- run: 'Get-Item ${{ matrix.volume.windows }}\3.h2w'
- run: 'Get-Item ${{ matrix.volume.windows }}\4.h2w'

- run: '& .\f3read.exe -V'
- run: '& .\f3read.exe --help'
- run: '& .\f3read.exe -s 2 -e 4 -r 50000 /cygdrive/c'
- run: '& .\f3read.exe -s 2 -e 4 -r 50000 ${{ matrix.volume.cygwin }}'
2 changes: 2 additions & 0 deletions f3read.c
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,8 @@ int main(int argc, char **argv)
argp_parse(&argp, argc, argv, 0, NULL, &args);
print_header(stdout, "read");

adjust_dev_path(&args.dev_path);

files = ls_my_files(args.dev_path, args.start_at, args.end_at);

iterate_files(args.dev_path, files, args.start_at, args.end_at,
Expand Down
2 changes: 2 additions & 0 deletions f3write.c
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,8 @@ int main(int argc, char **argv)
argp_parse(&argp, argc, argv, 0, NULL, &args);
print_header(stdout, "write");

adjust_dev_path(&args.dev_path);

unlink_old_files(args.dev_path, args.start_at, args.end_at);

return fill_fs(args.dev_path, args.start_at, args.end_at,
Expand Down
18 changes: 16 additions & 2 deletions utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,26 @@
#include <dirent.h>
#include <errno.h>
#include <err.h>
#include <unistd.h>
AltraMayor marked this conversation as resolved.
Show resolved Hide resolved

#include "version.h"
#include "utils.h"

void adjust_dev_path(const char **dev_path)
{
if (chdir(*dev_path)) {
err(errno, "Can't change working directory to %s at %s()", *dev_path, __func__);
}

if (!chroot(*dev_path)) {
assert(!chdir("/"));
} else if (errno != EPERM) {
err(errno, "Can't change root directory to %s at %s()", *dev_path, __func__);
}

*dev_path = ".";
}

const char *adjust_unit(double *ptr_bytes)
{
const char *units[] = { "Byte", "KB", "MB", "GB", "TB", "PB", "EB" };
Expand Down Expand Up @@ -216,8 +232,6 @@ int posix_fadvise(int fd, off_t offset, off_t len, int advice)

#if (__APPLE__ && __MACH__) || defined(__OpenBSD__)

#include <unistd.h> /* For usleep(). */

AltraMayor marked this conversation as resolved.
Show resolved Hide resolved
void msleep(double wait_ms)
{
assert(!usleep(wait_ms * 1000));
Expand Down
2 changes: 2 additions & 0 deletions utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#define SECTOR_SIZE (512)
#define GIGABYTES (1024 * 1024 * 1024)

void adjust_dev_path(const char **dev_path);

const char *adjust_unit(double *ptr_bytes);

/* Return true if @filename matches the regex /^[0-9]+\.h2w$/ */
Expand Down