[Pkg-shadow-devel] [Git][debian/adduser][zg/debian-bug-248500] keep separate id counters for uids and gids
Marc Haber (@zugschlus)
gitlab at salsa.debian.org
Tue Nov 17 12:50:17 GMT 2026
Marc Haber pushed to branch zg/debian-bug-248500 at Debian / adduser
Commits:
13125f96 by Marc Haber at 2026-11-17T13:49:18+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.
- - - - -
4 changed files:
- AdduserCommon.pm
- adduser
- adduser.conf
- debian/tests/f/id_counter.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
=====================================
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,7 +685,7 @@ 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);
+ $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
@@ -777,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);
@@ -811,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);
@@ -925,7 +931,7 @@ 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);
+ $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,
@@ -937,8 +943,12 @@ if ($action eq "adduser") {
} 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,
@@ -1136,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/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
View it on GitLab: https://salsa.debian.org/debian/adduser/-/commit/13125f96bbf80fd69897609f2802432ebb3759d1
--
View it on GitLab: https://salsa.debian.org/debian/adduser/-/commit/13125f96bbf80fd69897609f2802432ebb3759d1
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/20261117/5da1ea99/attachment-0001.htm>
More information about the Pkg-shadow-devel
mailing list