Skip to content

Commit

Permalink
override clock_adjtime() with eBPF instead of ld.so.preload
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed Oct 20, 2024
1 parent 7c370be commit fb295aa
Show file tree
Hide file tree
Showing 14 changed files with 94 additions and 21 deletions.
1 change: 1 addition & 0 deletions kernel-kit/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ make -j`nproc` bzImage modules || exit 1
echo "Creating the kernel package"
make INSTALL_MOD_PATH=`pwd`/../output/linux_kernel-${kernel_version}/usr INSTALL_MOD_STRIP=1 modules_install
cp -f arch/x86/boot/bzImage ../output/vmlinuz-${kernel_version}
bpftool btf dump file vmlinux format c > ../output/vmlinux-${kernel_version}.h
rm -f ../output/linux_kernel-${kernel_version}/usr/lib/modules/${kernel_version}/{build,source}
mkdir -p ../output/linux_kernel-${kernel_version}/boot
cp -f .config ../output/linux_kernel-${kernel_version}/boot/config-${kernel_version}
Expand Down
3 changes: 0 additions & 3 deletions woof-code/rootfs-petbuilds/connman-puppy/README.md

This file was deleted.

8 changes: 0 additions & 8 deletions woof-code/rootfs-petbuilds/connman-puppy/libadjtime.c

This file was deleted.

8 changes: 0 additions & 8 deletions woof-code/rootfs-petbuilds/connman-puppy/petbuild

This file was deleted.

1 change: 0 additions & 1 deletion woof-code/rootfs-petbuilds/connman-puppy/pinstall.sh

This file was deleted.

21 changes: 21 additions & 0 deletions woof-code/rootfs-petbuilds/rtclock/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
CFLAGS ?= -O2 -Wall -pedantic
CLANG ?= clang
LLVM_STRIP ?= llvm-strip
LIBS = $(shell pkg-config --libs libbpf)
PREFIX = /usr/local

rtclock: rtclock.c rtclock.skel.h
$(CC) $(CFLAGS) -o $@ $< $(LIBS)

rtclock.skel.h: rtclock.bpf.o
bpftool gen skeleton $< > $@

rtclock.bpf.o: rtclock.bpf.c
$(CLANG) -g -O2 -Wall -target bpf -D__KERNEL__ -c $< -o $@
$(LLVM_STRIP) -g $@

install: rtclock
install -D -m 755 $< $(DESTDIR)$(PREFIX)/bin/$<

clean:
rm -f rtclock rtclock.skel.h rtclock.bpf.o
3 changes: 3 additions & 0 deletions woof-code/rootfs-petbuilds/rtclock/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
rtclock prevents connman from changing the hardware clock using eBPF, to prevent conflicts with other OSs running on the same machine that may save time in UTC rather than local time.

When connman's clock_adjtime() calls get intercepted by rtclock, clock synchronization over NTP only sets the software clock. /etc/rc.d/rc.sysinit is responsible for synchronizing the software clock with RTC at boot time, so if RTC is set correctly (by another OS), the software clock will show the correct time even before network is ready.
7 changes: 7 additions & 0 deletions woof-code/rootfs-petbuilds/rtclock/petbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
download() {
:
}

build() {
make install
}
22 changes: 22 additions & 0 deletions woof-code/rootfs-petbuilds/rtclock/rtclock.bpf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <vmlinux.h>

#include <bpf/bpf_core_read.h>

#define STA_UNSYNC 0x0040

char LICENSE[] SEC("license") = "GPL";

SEC("tracepoint/syscalls/sys_enter_clock_adjtime")
int override_clock_adjtime(struct trace_event_raw_sys_enter *ctx)
{
struct __kernel_timex ktx;

if (bpf_probe_read_user(&ktx, sizeof(ktx), (void *)ctx->args[1]) < 0) {
return 0;
}

ktx.status |= STA_UNSYNC;
bpf_probe_write_user((void *)ctx->args[1], &ktx, sizeof(ktx));

return 0;
}
31 changes: 31 additions & 0 deletions woof-code/rootfs-petbuilds/rtclock/rtclock.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <signal.h>
#include <stdlib.h>
#include <bpf/libbpf.h>
#include <unistd.h>

#include "rtclock.skel.h"

int main(int argc, char *argv[])
{
sigset_t set;
struct rtclock_bpf *skel;
int sig;

if (sigemptyset(&set) < 0 || sigaddset(&set, SIGTERM) < 0 || sigaddset(&set, SIGINT) < 0 || sigprocmask(SIG_BLOCK, &set, NULL) < 0) return EXIT_FAILURE;

libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
libbpf_set_print(NULL);

if (!(skel = rtclock_bpf__open_and_load())) return EXIT_FAILURE;

if (rtclock_bpf__attach(skel)) {
rtclock_bpf__destroy(skel);
return EXIT_FAILURE;
}

daemon(0, 0);
sigwait(&set, &sig);

rtclock_bpf__destroy(skel);
return EXIT_FAILURE;
}
2 changes: 2 additions & 0 deletions woof-code/rootfs-skeleton/etc/rc.d/rc.sysinit
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ mount -t tmpfs -onosuid,noexec,size=10%,mode=755 tmpfs /run
mount -t tmpfs -onodev,noexec,nosuid,size=20%,mode=1777 /run/lock
mount -t proc -onodev,noexec,nosuid proc /proc
mount -t sysfs -onodev,noexec,nosuid sysfs /sys
mount -t debugfs none /sys/kernel/debug
mount -t pstore pstore /sys/fs/pstore
[ -d /sys/firmware/efi/efivars ] && mount -t efivarfs none /sys/firmware/efi/efivars

Expand Down Expand Up @@ -296,6 +297,7 @@ echo -n "Setting up services (network" >/dev/console
hostname -F /etc/hostname

busybox hwclock -s
rtclock

mkdir -p /var/run/dbus
chown messagebus /var/run/dbus
Expand Down
2 changes: 2 additions & 0 deletions woof-code/support/petbuilds.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ EOF
chmod 755 petbuild-rootfs-complete/usr/bin/$PROG
done

cp -f ../kernel-kit/output/vmlinux-*.h petbuild-rootfs-complete/usr/include/vmlinux.h

cp -f /etc/resolv.conf petbuild-rootfs-complete/etc/

HAVE_ROOTFS=1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ yes|locales|locales|exe,dev,doc,nls>exe||deps:yes
yes|kmod|kmod|exe,dev,doc,nls||deps:yes
yes|l3afpad|l3afpad|exe,dev,doc,nls||deps:yes
yes|less|less|exe,dev>null,doc,nls||deps:yes
yes|libbpf-dev|libbpf-dev|exe,dev,doc,nls||deps:yes
yes|libc6-dev|libc6-dev|exe,dev,doc,nls||deps:yes
yes|libargon2-dev|libargon2-dev|exe,dev,doc,nls||deps:yes # needed by pfscrypt
yes|libfreetype-dev|libfreetype-dev|exe,dev,doc,nls||deps:yes
Expand Down Expand Up @@ -187,7 +188,9 @@ yes|automake|automake,autotools-dev|exe>dev,dev,doc>dev,nls>dev||deps:yes
yes|autopoint|autopoint|exe>dev,dev,doc>dev,nls>dev||deps:yes
yes|binutils|binutils|exe>dev,dev,doc>dev,nls>dev||deps:yes
yes|bison|bison|exe>dev,dev,doc>dev,nls>dev||deps:yes
yes|bpftool|bpftool|exe>dev,dev,doc>dev,nls>dev||deps:yes
yes|ccache|ccache|exe>dev,dev,doc>dev,nls>dev||deps:yes
yes|clang|clang|exe>dev,dev,doc>dev,nls>dev||deps:yes
yes|cmake|cmake|exe>dev,dev,doc>dev,nls>dev||deps:yes # build-time dependency of hardinfo
yes|dwarves|dwarves|exe>dev,dev,doc>dev,nls>dev||deps:yes # kernel-kit
yes|flex|flex|exe>dev,dev,doc>dev,nls>dev||deps:yes
Expand All @@ -200,6 +203,7 @@ yes|libelf-dev|libelf-dev|exe>dev,dev,doc>dev,nls>dev||deps:yes # kernel-kit
yes|libtool|libtool,libtool-bin|exe>dev,dev,doc>dev,nls>dev||deps:yes
yes|linux-header|linux-libc-dev|exe>dev,dev,doc>dev,nls>dev||deps:yes
yes|linux-source|linux-source|exe>dev,dev,doc>dev,nls>dev||deps:yes # kernel-kit
yes|llvm|llvm|exe>dev,dev,doc>dev,nls>dev||deps:yes
yes|m4|m4|exe>dev,dev,doc>dev,nls>dev||deps:yes
yes|make|make|exe>dev,dev,doc>dev,nls>dev||deps:yes
yes|meson|meson|exe>dev,dev,doc>dev,nls>dev||deps:yes
Expand Down
2 changes: 1 addition & 1 deletion woof-distro/x86_64/debian/trixie64/_00build.conf
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#

## packages to build from source
PETBUILDS="aaa_pup_c gexec yad connman-puppy ram-saver fixmenusd spot-pkexec swaylock swayidle chayang spacefm mdview pup_advert_blocker"
PETBUILDS="aaa_pup_c gexec yad rtclock ram-saver fixmenusd spot-pkexec swaylock swayidle chayang spacefm mdview pup_advert_blocker"
[ "$DISTRO_TARGETARCH" = "x86_64" ] && PETBUILDS="$PETBUILDS efilinux"
if [ "$DISTRO_VARIANT" = "dwl" ]; then
PETBUILDS="$PETBUILDS dwl foot-puppy yambar-dwl"
Expand Down

0 comments on commit fb295aa

Please sign in to comment.