[packages/htop] Rel 2. Do not follow symlinks when writing htop_history

arekm arekm at pld-linux.org
Thu Oct 29 08:37:07 CET 2026


commit 8cb4d4338e26a9c53e173bcc085430e4e3bb1262
Author: Arkadiusz Miśkiewicz <arekm at maven.pl>
Date:   Thu Oct 29 00:37:22 2026 +0100

    Rel 2. Do not follow symlinks when writing htop_history
    
    Backport the History part of upstream 0ddbd1b0 (GHSA-46qw-qjgv-jhrw)

 htop-history-symlink.patch | 127 +++++++++++++++++++++++++++++++++++++++++++++
 htop.spec                  |   4 +-
 2 files changed, 130 insertions(+), 1 deletion(-)
---
diff --git a/htop.spec b/htop.spec
index 34fe402..10384c5 100644
--- a/htop.spec
+++ b/htop.spec
@@ -3,11 +3,12 @@ Summary(hu.UTF-8):	Egy interaktív processz megjelenítő
 Summary(pl.UTF-8):	Interaktywna przeglądarka procesów
 Name:		htop
 Version:	3.5.3
-Release:	1
+Release:	2
 License:	GPL v2
 Group:		Applications/System
 Source0:	https://github.com/htop-dev/htop/releases/download/%{version}/%{name}-%{version}.tar.xz
 # Source0-md5:	587f0d8ca67592ca199abef235dadb68
+Patch0:		%{name}-history-symlink.patch
 URL:		https://htop.dev/
 BuildRequires:	autoconf >= 2.69
 BuildRequires:	automake >= 1:1.11
@@ -40,6 +41,7 @@ Jej celem jest bycie lepszą odmianą programu 'top'.
 
 %prep
 %setup -q
+%patch -P0 -p1
 
 # don't require /proc at build time
 sed 's@^[[:space:]]*AC_CHECK_FILE($PROCDIR.*@:@' -i configure.ac
diff --git a/htop-history-symlink.patch b/htop-history-symlink.patch
new file mode 100644
index 0000000..cc9c497
--- /dev/null
+++ b/htop-history-symlink.patch
@@ -0,0 +1,127 @@
+Refuse to follow a symlink or write a foreign-owned htop_history, GHSA-46qw-qjgv-jhrw.
+History.c/History.h part of https://github.com/htop-dev/htop/commit/0ddbd1b0
+
+diff --git a/History.c b/History.c
+index 50320abfd..c3f255319 100644
+--- a/History.c
++++ b/History.c
+@@ -9,22 +9,66 @@ in the source distribution for its full text.
+ 
+ #include "History.h"
+ 
++#include <errno.h>
+ #include <fcntl.h>
+ #include <stdio.h>
+ #include <stdlib.h>
+ #include <string.h>
+ #include <unistd.h>
++#include <sys/stat.h>
+ 
+ #include "Macros.h"
+ #include "XUtils.h"
+ 
+ 
++/* Determine whether the history file is safe to (over)write, mirroring the
++   checks Settings_read() applies to htoprc: the file must be a regular file
++   owned by the effective user with owner-write permission. The O_NOFOLLOW
++   flag guards the final path component against symlink attacks, while
++   O_NONBLOCK keeps an owned FIFO from blocking the read-only fallback. */
+ static void History_load(History* this) {
+    if (!this->filename)
+       return;
+-   FILE* fp = fopen(this->filename, "r");
+-   if (!fp)
++
++   int fd = -1;
++   do {
++      fd = open(this->filename, O_RDWR | O_NOCTTY | O_NOFOLLOW | O_NONBLOCK);
++   } while (fd < 0 && errno == EINTR);
++
++   if (fd < 0) {
++      this->writeHistory = (errno == ENOENT);
++      if (errno != EACCES && errno != EPERM && errno != EROFS)
++         return;
++   } else {
++      struct stat sb;
++      int err = fstat(fd, &sb);
++      this->writeHistory = !err && S_ISREG(sb.st_mode) && (sb.st_mode & S_IWUSR) && sb.st_uid == geteuid();
++   }
++
++   /* If opening read & write is not possible, open read only.
++      O_NOFOLLOW rejects a planted symlink, O_NONBLOCK avoids blocking on
++      non-regular files such as FIFOs when no writer is present. */
++   if (fd < 0) {
++      do {
++         fd = open(this->filename, O_RDONLY | O_NOCTTY | O_NOFOLLOW | O_NONBLOCK);
++      } while (fd < 0 && errno == EINTR);
++   }
++
++   if (fd < 0)
++      return;
++
++   /* Only read regular files; reading a FIFO would block. */
++   struct stat sb;
++   if (fstat(fd, &sb) != 0 || !S_ISREG(sb.st_mode)) {
++      close(fd);
++      return;
++   }
++
++   FILE* fp = fdopen(fd, "r");
++   if (!fp) {
++      close(fd);
+       return;
++   }
+ 
+    char line[LINEEDITOR_MAX + 2];
+    while (fgets(line, sizeof(line), fp)) {
+@@ -48,6 +92,7 @@ History* History_new(const char* filename) {
+    this->position = 0;
+    this->saved[0] = '\0';
+    this->filename = filename ? xStrdup(filename) : NULL;
++   this->writeHistory = true;
+ 
+    if (this->filename)
+       History_load(this);
+@@ -66,12 +111,27 @@ void History_delete(History* this) {
+ }
+ 
+ void History_save(const History* this) {
+-   if (!this->filename)
++   if (!this->filename || !this->writeHistory)
+       return;
+-   /* Settings_write writes things via a temp file & rename, we do it less robust but faster here: */
+-   int fd = open(this->filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
++   /* Settings_write writes things via a temp file & rename, we do it less robust but faster here.
++      O_NOFOLLOW guards against a symlink planted at the final path component,
++      O_NONBLOCK avoids hanging on an existing FIFO, and the fstat() re-check
++      closes a race between open and the owner verification. */
++   int fd = open(this->filename, O_WRONLY | O_NOCTTY | O_CREAT | O_NOFOLLOW | O_NONBLOCK, 0600);
+    if (fd == -1)
+       return;
++
++   struct stat sb;
++   if (fstat(fd, &sb) != 0 || !S_ISREG(sb.st_mode) || !(sb.st_mode & S_IWUSR) || sb.st_uid != geteuid()) {
++      close(fd);
++      return;
++   }
++
++   if (ftruncate(fd, 0) != 0) {
++      close(fd);
++      return;
++   }
++
+    FILE* fp = fdopen(fd, "w");
+    if (!fp) {
+       close(fd); // fd not consumed on failure, so close it
+diff --git a/History.h b/History.h
+index d2989d7fc..d187ed7e5 100644
+--- a/History.h
++++ b/History.h
+@@ -22,6 +22,7 @@ typedef struct History_ {
+    size_t position;      /* current browse position: count = "at new input" */
+    char saved[LINEEDITOR_MAX + 1]; /* saved current input while browsing */
+    char* filename;    /* path to history file (may be NULL = no read / write) */
++   bool writeHistory;  /* whether the history file may be (over)written */
+ } History;
+ 
+ /* Create a new History, loading from the given file (may be NULL = init new history) */
================================================================

---- gitweb:

http://git.pld-linux.org/gitweb.cgi/packages/htop.git/commitdiff/8cb4d4338e26a9c53e173bcc085430e4e3bb1262



More information about the pld-cvs-commit mailing list