Package: aptitude
Version: 0.8.13-7
Severity: normal
X-Debbugs-Cc: [email protected]

Dear Maintainer,

I executed the debian tests for the apt package with:
`autopkgtest apt -- null` on my arm64 machine with memory tagging
enabled for the heap with GLIBC_TUNABLES=glibc.mem.tagging=3
This resulted in the failure of the test case 
"test-explore-or-groups-in-markinstall" with:

```
952s (220/320) Testcase test-explore-or-groups-in-markinstall:  P P P P P P P P 
P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P 
P P P P P P P P P P P P P P P P P P P P P P P P P P P
952s Install checks with foos dependency type Recommends
952s Test for successful execution of aptitude install bad-conflict-level2 -sy 
… Reading package lists...
952s Building dependency tree...
952s Initializing package states...
952s Building tag database...
952s Segmentation fault
952s FAIL: exitcode 139
952s  P
952s Install checks with foos dependency type Recommends
952s Part of the test group: Test for successful execution of aptitude install 
bad-conflict-level2 -sy …
952s Test for correctness of file 
/tmp/tmp.LskCtzDKvp/rootdir/tmp/testsuccess.output …
952s -Building dependency tree...
952s -Initializing package states...
952s -Building tag database...
952s -Segmentation fault
952s +No packages will be installed, upgraded, or removed.
952s +0 packages upgraded, 0 newly installed, 0 to remove and 3 not upgraded.
952s +Need to get 0 B of archives. After unpacking 0 B will be used.
952s +Would download/install/remove packages.
952s === content of file we compared with 
(/tmp/tmp.LskCtzDKvp/rootdir/tmp/testsuccess.output) ===
952s Building dependency tree...
952s Initializing package states...
952s Building tag database...
952s Segmentation fault
952s FAIL
952s
952s Running test-explore-or-groups-in-markinstall -> FAILED
```

This turned out to be a use-after-free problem in the aptitude
application. I reproduced it on an x86_64 machine with
a self-compiled version of aptitude with ASan
at the git version: d24eb35bb7168169774b68b270b08f8079a2c9a5

```
  ERROR: AddressSanitizer: heap-use-after-free on address 0x51900000be98
  READ of size 1 at 0x51900000be98 thread T1
    #0  generic_problem_resolver<aptitude_universe>::do_log(...) 
src/generic/problemresolver/problemresolver.h:3716
    #1-#9 sigc++ signal emission
    #10 aptitude::util::logging::Logger::Impl::log(...) 
src/generic/util/logging.cc:137
    #11 
generic_problem_resolver<aptitude_universe>::find_promotions_for_solver(...)
    #12 generic_problem_resolver<aptitude_universe>::add_unresolved_dep(...)
    #13 
generic_problem_resolver<aptitude_universe>::add_new_unresolved_deps(...)
    #14 
generic_problem_resolver<aptitude_universe>::generate_single_successor(...)
    #15 generic_problem_resolver<aptitude_universe>::generate_successors(...)
    #16 generic_problem_resolver<aptitude_universe>::process_step(...)
    #17 generic_problem_resolver<aptitude_universe>::find_next_solution(...)
    #18 resolver_manager::do_get_solution(...) 
src/generic/apt/resolver_manager.cc:1163
    #19 resolver_manager::background_thread_execution() 
src/generic/apt/resolver_manager.cc:593

  0x51900000be98 is located 24 bytes inside of 1032-byte region
  freed by thread T0 here:
    #1  aptitude_resolver::~aptitude_resolver() 
src/generic/apt/aptitude_resolver.h:454
    #2  resolver_manager::discard_resolver() 
src/generic/apt/resolver_manager.cc:852
    #3  resolver_manager::reset_resolver(bool) 
src/generic/apt/resolver_manager.cc:217
    #4  setup_resolver src/cmdline/cmdline_resolver.cc:167
    #5  cmdline_resolve_deps(...) src/cmdline/cmdline_resolver.cc:857
    #6  cmdline_do_prompt(...) src/cmdline/cmdline_prompt.cc:1012
    #7  cmdline_simulate(...) src/cmdline/cmdline_simulate.cc:54
    #8  cmdline_do_action(...) src/cmdline/cmdline_do_action.cc:390

  previously allocated by thread T0 here:
    #1  resolver_manager::create_resolver() 
src/generic/apt/resolver_manager.cc:968
    #2  resolver_manager::maybe_create_resolver(bool) 
src/generic/apt/resolver_manager.cc:805
    ...
    #9  aptitudeDepCache::end_action_group(undo_group*) 
src/generic/apt/aptcache.cc:2357
```

A more simple way to reproduce the issue is with the following script
which mirrors the logic of the testing setup from above:

```
#!/bin/bash
set -eu

work=$(mktemp -d /tmp/aptitude.XXXXXX)
echo "Reproducer directory: $work"

mkdir -p "$work"/{repo,state/lists/partial,cache/archives/partial,etc}
touch "$work/state/status"

# A reduced dependency graph from the original test.
package() {
    printf 'Package: %s\nVersion: 1\nArchitecture: all\n' "$1"
    printf 'Maintainer: Test <[email protected]>\n'
    printf 'Description: Reproducer package\n'
    printf 'Filename: %s_1_all.deb\nSize: 100\n' "$1"
    if [ -n "${2:-}" ]; then
        printf '%s\n' "$2"
    fi
    printf '\n'
}

{
    package unneeded
    package unneeded2 'Depends: bad-level1'
    package bad-level0 'Depends: unneeded, unknown'
    package bad-level1 'Depends: bad-level0'
    package bad-conflict-level0 'Depends: unneeded
Conflicts: bad-conflict-level2'
    package bad-conflict-level1 'Depends: bad-conflict-level0'
    package bad-conflict-level2 'Depends: bad-conflict-level1, unneeded2'
} > "$work/repo/Packages"

gzip -c "$work/repo/Packages" > "$work/repo/Packages.gz"

printf 'deb [trusted=yes] file:%s/repo ./\n' "$work" \
    > "$work/etc/sources.list"

cat > "$work/apt.conf" <<EOF
Dir "$work";
Dir::State "$work/state";
Dir::State::status "$work/state/status";
Dir::State::lists "$work/state/lists";
Dir::Cache "$work/cache";
Dir::Cache::archives "$work/cache/archives";
Dir::Cache::pkgcache "";
Dir::Cache::srcpkgcache "";
Dir::Etc "$work/etc";
Dir::Etc::sourcelist "sources.list";
Dir::Etc::sourceparts "-";
Dir::Etc::main "-";
Dir::Etc::parts "-";
APT::Sandbox::User "root";
Acquire::Languages "none";
EOF

APT_CONFIG="$work/apt.conf" /usr/bin/apt-get update

echo
echo "Running aptitude..."

APT_CONFIG=$work/apt.conf /usr/bin/aptitude install bad-conflict-level2 -sy
```


This is a possible fix, however, it was generated by Opus 5.5 and should
only be considered as a draft. I verified that with these changes
the bug does not occur anymore.

```
diff --git a/src/generic/problemresolver/problemresolver.h 
b/src/generic/problemresolver/problemresolver.h
index 7ca99472..1a2e1786 100644
--- a/src/generic/problemresolver/problemresolver.h
+++ b/src/generic/problemresolver/problemresolver.h
@@ -3706,6 +3706,8 @@ private:
     bestDepSolvers.for_each_solver(generate_successor_f);
   }

+  sigc::connection log_connection;
+
   void do_log(const char *sourceName,
               int sourceLine,
               logging::log_level level,
@@ -3764,7 +3766,7 @@ public:
      promotion_queue_tail(new promotion_queue_entry(0, 0)),
      version_costs(new cost[_universe.get_version_count()])
   {
-    logger->connect_message_logged(sigc::mem_fun(*this, 
&generic_problem_resolver::do_log));
+    log_connection = logger->connect_message_logged(sigc::mem_fun(*this, 
&generic_problem_resolver::do_log));

     LOG_DEBUG(logger, "Creating new problem resolver: step_score = " << 
_step_score
              << ", broken_score = " << _broken_score
@@ -3811,6 +3813,7 @@ public:

   ~generic_problem_resolver()
   {
+    log_connection.disconnect();
     delete[] version_costs;
   }
```


-- Package-specific info:
Terminal: xterm
$DISPLAY not set.
which aptitude: /usr/bin/aptitude

aptitude version information:
aptitude 0.8.13
Compiler: g++ 14.2.0
Compiled against:
  apt version 7.0.0
  NCurses version 6.5
  libsigc++ version: 2.12.1
  Gtk+ support disabled.
  Qt support disabled.

Current library versions:
  NCurses version: ncurses 6.5.20250216
  cwidget version: 0.5.18
  Apt version: 7.0.0

aptitude linkage:
        linux-vdso.so.1 (0x000077a1912ee000)
        libapt-pkg.so.7.0 => /lib/x86_64-linux-gnu/libapt-pkg.so.7.0 
(0x000077a190bf8000)
        libncursesw.so.6 => /lib/x86_64-linux-gnu/libncursesw.so.6 
(0x000077a190bbe000)
        libtinfo.so.6 => /lib/x86_64-linux-gnu/libtinfo.so.6 
(0x000077a190b88000)
        libsigc-2.0.so.0 => /lib/x86_64-linux-gnu/libsigc-2.0.so.0 
(0x000077a190b7f000)
        libcwidget.so.4 => /lib/x86_64-linux-gnu/libcwidget.so.4 
(0x000077a190a75000)
        libsqlite3.so.0 => /lib/x86_64-linux-gnu/libsqlite3.so.0 
(0x000077a1908f5000)
        libboost_iostreams.so.1.83.0 => 
/lib/x86_64-linux-gnu/libboost_iostreams.so.1.83.0 (0x000077a1908dc000)
        libxapian.so.30 => /lib/x86_64-linux-gnu/libxapian.so.30 
(0x000077a19067f000)
        libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 
(0x000077a190419000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x000077a190329000)
        libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 
(0x000077a1902fa000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x000077a190106000)
        libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x000077a1900e6000)
        libbz2.so.1.0 => /lib/x86_64-linux-gnu/libbz2.so.1.0 
(0x000077a1900d3000)
        liblzma.so.5 => /lib/x86_64-linux-gnu/liblzma.so.5 (0x000077a1900a2000)
        liblz4.so.1 => /lib/x86_64-linux-gnu/liblz4.so.1 (0x000077a19007b000)
        libzstd.so.1 => /lib/x86_64-linux-gnu/libzstd.so.1 (0x000077a18ffaf000)
        libudev.so.1 => /lib/x86_64-linux-gnu/libudev.so.1 (0x000077a18ff6a000)
        libsystemd.so.0 => /lib/x86_64-linux-gnu/libsystemd.so.0 
(0x000077a18fe54000)
        libcrypto.so.3 => /lib/x86_64-linux-gnu/libcrypto.so.3 
(0x000077a18f817000)
        libxxhash.so.0 => /lib/x86_64-linux-gnu/libxxhash.so.0 
(0x000077a18f804000)
        /lib64/ld-linux-x86-64.so.2 (0x000077a1912f0000)
        libuuid.so.1 => /lib/x86_64-linux-gnu/libuuid.so.1 (0x000077a18f7f8000)
        libcap.so.2 => /lib/x86_64-linux-gnu/libcap.so.2 (0x000077a18f7ec000)

-- System Information:
Debian Release: 13.7
  APT prefers stable-updates
  APT policy: (500, 'stable-updates'), (500, 'stable-security'), (500, 'stable')
Architecture: amd64 (x86_64)

Kernel: Linux 6.18.58.1-microsoft-standard-WSL2 (SMP w/12 CPU threads; PREEMPT)
Locale: LANG=C, LC_CTYPE=C.UTF-8 (charmap=UTF-8), LANGUAGE not set
Shell: /bin/sh linked to /usr/bin/dash
Init: unable to detect

Versions of packages aptitude depends on:
ii  aptitude-common           0.8.13-7
ii  libapt-pkg7.0             3.0.4
ii  libboost-iostreams1.83.0  1.83.0-4.2
ii  libc6                     2.41-12+deb13u5
ii  libcwidget4               0.5.18-6+b2
ii  libgcc-s1                 14.2.0-19
ii  libncursesw6              6.5+20250216-2
ii  libsigc++-2.0-0v5         2.12.1-3
ii  libsqlite3-0              3.46.1-7+deb13u2
ii  libstdc++6                14.2.0-19
ii  libtinfo6                 6.5+20250216-2
ii  libxapian30               1.4.29-3+deb13u1

Versions of packages aptitude recommends:
ii  libdpkg-perl    1.22.22
ii  sensible-utils  0.0.25

Versions of packages aptitude suggests:
pn  apt-xapian-index                <none>
pn  aptitude-doc-en | aptitude-doc  <none>
pn  debtags                         <none>
pn  tasksel                         <none>

-- no debconf information

Reply via email to