[Pkg-shadow-devel] [Git][debian/adduser][debian/latest] 5 commits: adapt autopkgtest to use REUSE_IDs
Marc Haber (@zugschlus)
gitlab at salsa.debian.org
Thu Nov 26 16:00:56 GMT 2026
Marc Haber pushed to branch debian/latest at Debian / adduser
Commits:
ed40e090 by Marc Haber at 2026-11-26T14:14:47+01:00
adapt autopkgtest to use REUSE_IDs
in s/uids/gidpool.t, set REUSE_IDS yes for some tests that would fail
otherwise. The alternative would be to rework the tests more
distinctively and I am not sure whether that's worth the effort
- - - - -
44ed496b by Marc Haber at 2026-11-26T14:14:47+01:00
adapt REUSE_IDS patch to use --ingroup's gid as uid if free
- - - - -
d6266406 by Marc Haber at 2026-11-26T14:14:47+01:00
add ingroup_uid.t to test --ingroup gid => uid mechnanism
- - - - -
de41407e by Marc Haber at 2026-11-26T14:14:47+01:00
adapt firstlastuidgid.t to REUSE_UIDs patch
- - - - -
4d32a479 by Marc Haber at 2026-11-26T14:14:47+01:00
keep separate id counters for uids and gids
keep separate id counters for uids and gids if the configured uid and
gid ranges differ. A single counter made the uid range look exhausted
as soon as gids above it had been handed out.
- - - - -
8 changed files:
- AdduserCommon.pm
- adduser
- adduser.conf
- debian/tests/f/firstlastuidgid.t
- debian/tests/f/id_counter.t
- + debian/tests/f/ingroup_uid.t
- debian/tests/f/suidsgidpool.t
- debian/tests/f/uidgidpool.t
Changes:
=====================================
AdduserCommon.pm
=====================================
@@ -156,6 +156,8 @@ use constant {
'range_intersection',
'highest_used_id',
'id_floor',
+ 'id_floor_uidgid',
+ 'id_counter_keys',
'record_id_used',
'first_avail_uid',
'first_avail_gid',
@@ -296,6 +298,47 @@ sub id_floor {
return $floor;
}
+# id_counter_keys: name the state file counters that belong to the
+# configured uid and gid ranges
+# If the uid range and the gid range are the same (which is the default),
+# uids and gids come from one pool and share one counter, so that an id
+# handed out as a gid is not handed out again as a uid. If the ranges
+# differ, an id of one range says nothing about the other: a gid in the
+# gid range could be above the whole uid range and would make the uid
+# range look exhausted. Then each of them has its own counter.
+# parameters:
+# config: reference to the configuration hash
+# system: true for the system uid/gid ranges
+# return values:
+# (uid counter key, gid counter key)
+sub id_counter_keys {
+ my ($config, $system) = @_;
+ my $p = $system ? 'system_' : '';
+ my $same = 1;
+ foreach my $b ('first', 'last') {
+ $same = 0 if (($config->{"${b}_${p}uid"} // '') ne ($config->{"${b}_${p}gid"} // ''));
+ }
+ return $system ? ('last_sys_id', 'last_sys_id') : ('last_id', 'last_id') if ($same);
+ return $system ? ('last_sys_uid', 'last_sys_gid') : ('last_uid', 'last_gid');
+}
+
+# id_floor_uidgid: the range in which a uid and a gid that have to be
+# identical can be searched, with the counters of both applied
+# parameters:
+# first_uid, last_uid: the uid range
+# first_gid, last_gid: the gid range
+# uid_key, gid_key: the counters, see id_counter_keys; undef if the
+# range was forced by the admin
+# config: reference to the configuration hash
+# return values:
+# (first, last) of the intersection of the two ranges
+sub id_floor_uidgid {
+ my ($first_uid, $last_uid, $first_gid, $last_gid, $uid_key, $gid_key, $config) = @_;
+ $first_uid = id_floor($first_uid, $last_uid, $uid_key, $config);
+ $first_gid = id_floor($first_gid, $last_gid, $gid_key, $config);
+ return range_intersection($first_uid, $last_uid, $first_gid, $last_gid);
+}
+
# record_id_used: remember the highest id handed out by adduser in a range
# parameters:
# id: the id that has just been assigned
@@ -330,12 +373,18 @@ sub record_id_used {
# reserved_uid_pool: hashref of ids reserved by other pending
# UID_POOL entries, keyed by id. May be undef or omitted, which
# is treated the same as an empty hashref.
+# floor: optional lower bound for the scan of the range, as raised
+# by id_floor() when REUSE_IDS=no. It does NOT apply to the
+# preferred id: the GID of an existing group is already in use
+# as a GID, so using it as UID too does not hand out an id that
+# the counter has not yet passed. If omitted, min is used.
# return values:
# -1 if no free uid is available
# otherwise the choosen uid
sub first_avail_uid {
- my ($min, $max, $pool_id, $preferred, $reserved_uid_pool) = @_;
+ my ($min, $max, $pool_id, $preferred, $reserved_uid_pool, $floor) = @_;
$reserved_uid_pool //= {};
+ $floor = $min if (!defined($floor) || $floor < $min);
if (defined ($pool_id)) {
return $pool_id if (!defined(getpwuid($pool_id)));
return -1;
@@ -345,9 +394,9 @@ sub first_avail_uid {
log_trace( "first_avail_uid: preferred id %d is free, using it", $preferred );
return $preferred;
}
- log_info( mtx("Selecting UID from range %d to %d ...\n"),$min,$max);
+ log_info( mtx("Selecting UID from range %d to %d ...\n"),$floor,$max);
- my $t = $min;
+ my $t = $floor;
while ($t <= $max) {
return $t if (!exists($reserved_uid_pool->{$t}) and !defined(getpwuid($t)));
$t++;
=====================================
adduser
=====================================
@@ -505,7 +505,7 @@ if ($action eq "addsysgroup") {
if (!defined($gid_option)) {
$first_gid = $new_firstgid || $config{"first_system_gid"};
$last_gid = $new_lastgid || $config{"last_system_gid"};
- $counter_key = 'last_sys_id' unless ($range_forced);
+ $counter_key = (id_counter_keys(\%config, 1))[1] unless ($range_forced);
$first_gid = id_floor($first_gid, $last_gid, $counter_key, \%config);
$gid_option = &first_avail_gid($first_gid,
$last_gid,
@@ -552,7 +552,7 @@ if ($action eq "addgroup") {
if (!defined($gid_option)) {
$first_gid = $new_firstgid || $config{"first_gid"};
$last_gid = $new_lastgid || $config{"last_gid"};
- $counter_key = 'last_id' unless ($range_forced);
+ $counter_key = (id_counter_keys(\%config, 0))[1] unless ($range_forced);
$first_gid = id_floor($first_gid, $last_gid, $counter_key, \%config);
log_debug( "Searching for gid with first_gid=%s, last_gid=%s, new_name=%s, gid_pool=%s",
$first_gid,
@@ -620,6 +620,7 @@ if ($action eq 'addusertogroup') {
if ($action eq "addsysuser") {
acquire_lock();
my $counter_key;
+ my $gid_counter_key;
my $uid_forced = defined($new_uid);
@@ -660,8 +661,10 @@ if ($action eq "addsysuser") {
my $last_sys_gid = $new_lastgid || $config{"last_system_gid"};
my ($first_uidgid, $last_uidgid) = &range_intersection(
$first_sys_uid, $last_sys_uid, $first_sys_gid, $last_sys_gid);
- $counter_key = 'last_sys_id' unless ($range_forced);
- $first_uidgid = id_floor($first_uidgid, $last_uidgid, $counter_key, \%config);
+ ($counter_key, $gid_counter_key) = id_counter_keys(\%config, 1) unless ($range_forced);
+ ($first_uidgid, $last_uidgid) = id_floor_uidgid(
+ $first_sys_uid, $last_sys_uid, $first_sys_gid, $last_sys_gid,
+ $counter_key, $gid_counter_key, \%config);
# TODO: Check what happens when those ranges do not overlap
$new_uid = &first_avail_uid_gid( $first_uidgid,
$last_uidgid,
@@ -682,8 +685,8 @@ if ($action eq "addsysuser") {
elsif (!defined($new_uid) && !$make_group_also) {
my $first_sys_uid = $new_firstuid || $config{"first_system_uid"};
my $last_sys_uid = $new_lastuid || $config{"last_system_uid"};
- $counter_key = 'last_sys_id' unless ($range_forced);
- $first_sys_uid = id_floor($first_sys_uid, $last_sys_uid, $counter_key, \%config);
+ $counter_key = (id_counter_keys(\%config, 1))[0] unless ($range_forced);
+ my $floor_sys_uid = id_floor($first_sys_uid, $last_sys_uid, $counter_key, \%config);
# if the primary group is already known by GID (--gid) or by
# name (--ingroup), and that GID happens to also be free as a
# UID, prefer it, so the user ends up with matching UID/GID
@@ -700,10 +703,11 @@ if ($action eq "addsysuser") {
$last_sys_uid,
$uid_pool{$new_name}{'id'},
$preferred_uid,
- \%reserved_uid_pool);
+ \%reserved_uid_pool,
+ $floor_sys_uid);
if ($new_uid == -1) {
log_warn( mtx("No UID is available in the range %d-%d (FIRST_SYS_UID - LAST_SYS_UID)."),
- $first_sys_uid,
+ $floor_sys_uid,
$last_sys_uid );
log_fatal( mtx("The user `%s' was not created."), $new_name);
exit( RET_NO_ID_IN_RANGE );
@@ -776,6 +780,8 @@ if ($action eq "addsysuser") {
record_id_used($new_uid, $counter_key, \%config)
unless ($uid_forced || $returnvalue != RET_OK);
+ record_id_used($new_uid, $gid_counter_key, \%config)
+ unless ($uid_forced || $returnvalue != RET_OK);
release_lock(0);
@@ -810,6 +816,7 @@ if ($action eq "addsysuser") {
if ($action eq "adduser") {
acquire_lock();
my $counter_key;
+ my $gid_counter_key;
# an id given by the admin does not take part in the reuse counter
my $uid_forced = defined($new_uid);
@@ -924,19 +931,24 @@ if ($action eq "adduser") {
# even though it was not created as a usergroup. Falls back
# to the lowest free UID in range if the GID is not free as
# a UID (or is outside the UID range).
- $counter_key = 'last_id' unless ($range_forced);
- $first_uid = id_floor($first_uid, $last_uid, $counter_key, \%config);
+ $counter_key = (id_counter_keys(\%config, 0))[0] unless ($range_forced);
+ my $floor_uid = id_floor($first_uid, $last_uid, $counter_key, \%config);
my $ingroup_gid = egetgrnam($ingroup_name);
$new_uid = &first_avail_uid( $first_uid,
$last_uid,
$uid_pool{$new_name}{'id'},
$ingroup_gid,
- \%reserved_uid_pool);
+ \%reserved_uid_pool,
+ $floor_uid);
} else {
my ($first_uidgid, $last_uidgid) = &range_intersection(
$first_uid, $last_uid, $first_gid, $last_gid);
- $counter_key = 'last_id' unless ($range_forced);
- $first_uidgid = id_floor($first_uidgid, $last_uidgid, $counter_key, \%config);
+ ($counter_key, $gid_counter_key) = id_counter_keys(\%config, 0) unless ($range_forced);
+ # the gid is only consumed if the group is created here
+ undef($gid_counter_key) unless ($make_group_also);
+ ($first_uidgid, $last_uidgid) = id_floor_uidgid(
+ $first_uid, $last_uid, $first_gid, $last_gid,
+ $counter_key, $gid_counter_key, \%config);
# TODO: Check what happens when those ranges do not overlap
$new_uid = &first_avail_uid_gid( $first_uidgid,
$last_uidgid,
@@ -1134,6 +1146,8 @@ if ($action eq "adduser") {
$primary_gid, $home_dir) if (-x "/usr/local/sbin/adduser.local");
record_id_used($new_uid, $counter_key, \%config)
unless ($uid_forced || $returnvalue != RET_OK);
+ record_id_used($new_uid, $gid_counter_key, \%config)
+ unless ($uid_forced || $returnvalue != RET_OK);
release_lock(0);
exit( $returnvalue );
=====================================
adduser.conf
=====================================
@@ -54,6 +54,8 @@
# An explicit range on the command line (--firstuid, --lastuid, --firstgid,
# --lastgid) overrides the counter: the first free id of that range is used
# and the counter is neither read nor updated.
+# If the uid range and the gid range are not the same, uids and gids are
+# counted separately.
# Default: REUSE_IDS=no
#REUSE_IDS=no
=====================================
debian/tests/f/firstlastuidgid.t
=====================================
@@ -27,6 +27,10 @@ my $prefix;
sub cleanup {
my $prefix=$_[0] || '';
+ # the id counter (REUSE_IDS=no) must not leak from one test group
+ # into the next one, each of which expects to start from its own
+ # configured first id.
+ reset_state_file();
foreach $userbase( @unames ) {
$user=$prefix.$userbase;
system("/usr/sbin/deluser @quiet --remove-home $user 2>/dev/null");
@@ -233,6 +237,15 @@ sub assert_usergroup_loop {
$uidcount++;
}
} else {
+ if ($user_index == 1 && !$via_cli) {
+ # With the range taken from the config file, the id
+ # counter (REUSE_IDS=no) decides where the scan for the
+ # second user starts. What is checked here is the range,
+ # the counter itself is tested in id_counter.t.
+ $uidcount = (getpwnam($user))[2];
+ cmp_ok($uidcount, '>=', $firstuid,
+ "uid of $user is inside the configured range");
+ }
assert_user_has_uid($user, $uidcount);
$uidcount++;
while (defined(getpwuid($uidcount))) {
=====================================
debian/tests/f/id_counter.t
=====================================
@@ -24,9 +24,9 @@ my @users = qw(
ausridhigh ausridhighnew ausridlost1 ausridlost2
ausridconc1 ausridconc2 ausridconc3 ausridconc4 ausridconc5
ausridconc1 ausridconc2 ausridconc3 ausridconc4 ausridconc5
- ausridrange1 ausridrange2
+ ausridrange1 ausridrange2 ausriddisj1 ausriddisj2
);
-my @groups = qw(ausridgroup1);
+my @groups = qw(ausridgroup1 ausriddisjg1 ausriddisjg2);
END {
cleanup_user($_) foreach (@users);
@@ -260,6 +260,39 @@ my $regular = add_user('ausridrange2');
isnt($regular, 31000, "a regular user is not affected by the previous range");
assert_state_global('last_id', $regular);
+##############################################################
+# 11. if the uid range and the gid range differ, each has its
+# own counter: creating groups must not move the uid counter,
+# in particular not above the end of the uid range
+##############################################################
+apply_config('REUSE_IDS', 'no',
+ 'FIRST_UID', '52000', 'LAST_UID', '52003',
+ 'FIRST_GID', '52100', 'LAST_GID', '52103');
+reset_state_file();
+
+foreach my $group (qw(ausriddisjg1 ausriddisjg2)) {
+ assert_command_success($addgroup,
+ '--stdoutmsglevel=error', '--stderrmsglevel=error', $group);
+}
+my $disj_gid = scalar(getgrnam('ausriddisjg2'));
+is($disj_gid, 52101, "groups are taken from the gid range");
+assert_state_global('last_gid', 52101);
+ok(!defined(get_global_value('last_id')),
+ "the shared counter has not been used");
+
+# the gid of the first group is not in the uid range, so the first
+# free uid of the uid range is used
+my $disj1 = add_user('ausriddisj1', '--no-create-home',
+ '--ingroup', 'ausriddisjg1');
+is($disj1, 52000, "uid comes from the uid range, not above it");
+assert_state_global('last_uid', 52000);
+
+my $disj2 = add_user('ausriddisj2', '--no-create-home',
+ '--ingroup', 'ausriddisjg1');
+is($disj2, 52001, "the uid counter continues in the uid range");
+assert_state_global('last_uid', 52001);
+assert_state_global('last_gid', 52101);
+
# done_testing(); done in END
-# vim: tabstop=4 shiftwidth=4 expandtab
\ No newline at end of file
+# vim: tabstop=4 shiftwidth=4 expandtab
=====================================
debian/tests/f/ingroup_uid.t
=====================================
@@ -0,0 +1,56 @@
+#! /usr/bin/perl -Idebian/tests/lib
+
+# adduser --ingroup: if the GID of the primary group is also free as a
+# UID, the user gets that UID, even if the id counter (REUSE_IDS=no)
+# has already advanced beyond it because of groups created later.
+
+use diagnostics;
+use strict;
+use warnings;
+
+use AdduserTestsCommon;
+use Test::More;
+
+my $adduser = '/usr/sbin/adduser';
+my $addgroup = '/usr/sbin/addgroup';
+my $delgroup = '/usr/sbin/delgroup';
+
+my @groups = qw(aingrpuid1 aingrpuid2 aingrpuid3 aingrpuid4);
+my @users = qw(aingrpuser1);
+
+END {
+ cleanup_user($_) foreach (@users);
+ foreach my $group (@groups) {
+ system($delgroup, $group) if (defined(getgrnam($group)));
+ }
+ done_testing();
+}
+
+foreach my $reuse ('no', 'yes') {
+ apply_config('REUSE_IDS', $reuse);
+ reset_state_file();
+
+ foreach my $group (@groups) {
+ assert_command_success($addgroup,
+ '--stdoutmsglevel=error', '--stderrmsglevel=error', $group);
+ }
+
+ my $gid = scalar(getgrnam('aingrpuid2'));
+ ok(defined($gid), "group aingrpuid2 has gid $gid (REUSE_IDS=$reuse)");
+ ok(!defined(getpwuid($gid)), "uid $gid is free");
+
+ assert_command_success($adduser,
+ '--stdoutmsglevel=error', '--stderrmsglevel=error',
+ '--comment', '\"\"', '--disabled-password',
+ '--ingroup', 'aingrpuid2', $users[0]);
+ assert_user_exists($users[0]);
+ is(scalar(getpwnam($users[0])), $gid,
+ "uid equals gid of --ingroup group (REUSE_IDS=$reuse)");
+
+ cleanup_user($users[0]);
+ foreach my $group (@groups) {
+ system($delgroup, $group);
+ }
+}
+
+# vim: tabstop=4 shiftwidth=4 expandtab
=====================================
debian/tests/f/suidsgidpool.t
=====================================
@@ -204,6 +204,7 @@ $confhash{"FIRST_SYSTEM_UID"}="$firstuid";
$confhash{"FIRST_SYSTEM_GID"}="$firstgid";
$confhash{"RESERVE_UID_POOL"}="no";
$confhash{"RESERVE_GID_POOL"}="no";
+$confhash{"REUSE_IDS"}="yes";
apply_config_hash(\%confhash);
# test not reserved uid in pool
@@ -276,6 +277,7 @@ $confhash{"FIRST_SYSTEM_UID"}="$firstuid";
$confhash{"FIRST_SYSTEM_GID"}="$firstgid";
$confhash{"RESERVE_UID_POOL"}="no";
$confhash{"RESERVE_GID_POOL"}="no";
+$confhash{"REUSE_IDS"}="yes";
apply_config_hash(\%confhash);
# test not reserved uid in pool
=====================================
debian/tests/f/uidgidpool.t
=====================================
@@ -206,6 +206,7 @@ $confhash{"FIRST_UID"}="$firstuid";
$confhash{"FIRST_GID"}="$firstgid";
$confhash{"RESERVE_UID_POOL"}="no";
$confhash{"RESERVE_GID_POOL"}="no";
+$confhash{"REUSE_IDS"}="yes";
apply_config_hash(\%confhash);
# test not reserved uid in pool
@@ -281,6 +282,7 @@ $confhash{"FIRST_UID"}="$firstuid";
$confhash{"FIRST_GID"}="$firstgid";
$confhash{"RESERVE_UID_POOL"}="no";
$confhash{"RESERVE_GID_POOL"}="no";
+$confhash{"REUSE_IDS"}="yes";
apply_config_hash(\%confhash);
# test not reserved uid in pool
View it on GitLab: https://salsa.debian.org/debian/adduser/-/compare/ee36dd24d093d0fa82cd898e58cb7d0e2f2a9e53...4d32a479ad14be1b6547c85478deaae3b5180dc7
--
View it on GitLab: https://salsa.debian.org/debian/adduser/-/compare/ee36dd24d093d0fa82cd898e58cb7d0e2f2a9e53...4d32a479ad14be1b6547c85478deaae3b5180dc7
You're receiving this email because of your account on salsa.debian.org. Manage all notifications: https://salsa.debian.org/-/profile/notifications | Help: https://salsa.debian.org/help
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://alioth-lists.debian.net/pipermail/pkg-shadow-devel/attachments/20261126/490b3e9a/attachment-0001.htm>
More information about the Pkg-shadow-devel
mailing list