Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modif: Prevent sandbox name from containing only digits #5578

Merged
merged 1 commit into from
Jan 31, 2023

Conversation

layderv
Copy link
Contributor

@layderv layderv commented Jan 9, 2023

If I use only numbers for a name,firejail --ls=numbers / fails. Is this an actual bug? If you forbid only numbers in jails, I will close this PR. I can rework it to delete the if's around the lines I added. This commit seems to me like the fix that touches the fewest lines of code. I gave priority to extract_pid which should better find the jail if it exists, is it correct?

Copy link
Collaborator

@kmk3 kmk3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@layderv on Jan 9:

If I use only numbers for a name,firejail --ls=numbers / fails. Is this an
actual bug? If you forbid only numbers in jails, I will close this PR.

From firejail(1):

       --ls=name|pid dir_or_filename
              List files in sandbox container, see FILE TRANSFER section for
              more details.

A numeric argument is treated as the PID rather than the sandbox name.

See also the descriptions for --name= and --join=.

@layderv
Copy link
Contributor Author

layderv commented Jan 10, 2023

--name=name
              Set sandbox name. Several options, such as --join and --shutdown, can use this name to identify a sandbox.

              In case the name supplied by the user is already in use by another sandbox, Firejail will assign a new name as "name-PID", where PID is the process ID of the sandbox. This
              functionality can be disabled at run time in /etc/firejail/firejail.config file, by setting "name-change" flag to "no".

              Example:
              $ firejail --name=browser firefox &
              $ firejail --name=browser --private firefox --no-remote &
              $ firejail --list
              1198:netblue:browser:firejail --name=browser firefox
              1312:netblue:browser-1312:firejail --name=browser --private firefox --no-remote
--join=name|pid
              Join  the  sandbox  identified  by name or by PID. By default a /bin/bash shell is started after joining the sandbox.  If a program is specified, the program is run in the
              sandbox. If --join command is issued as a regular user, all security filters are configured for the new process the same they are configured in  the  sandbox.   If  --join
              command is issued as root, the security filters, cgroups and cpus configurations are not applied to the process joining the sandbox.

              Example:
              $ firejail --name=mygame --caps.drop=all warzone2100 &
              $ firejail --join=mygame

              Example:
              $ firejail --list
              3272:netblue::firejail --private firefox
              $ firejail --join=3272

@kmk3

A numeric argument is treated as the PID rather than the sandbox name.

This is the behavior it shows, but it's explained nowhere that if a numeric argument is provided, it's treated as a PID. --name does not forbid this. --join=name|pid is "name" or "pid", it does not state that numeric names are treated as PID.

In fact you might prefer: if it's a numeric string, try to find it; if that PID does not exist, extract it (treat it as a normal name).

I would do one of the following:

  • do nothing
  • implement "if it's a numeric string, try to find it; if that PID does not exist, extract it (treat it as a normal name)."
  • update the man page to be clearer

@kmk3
Copy link
Collaborator

kmk3 commented Jan 10, 2023

@layderv on Jan 10:

A numeric argument is treated as the PID rather than the sandbox name.

This is the behavior it shows, but it's explained nowhere that if a numeric
argument is provided, it's treated as a PID. --name does not forbid this.
--join=name|pid is "name" or "pid", it does not state that numeric names
are treated as PID.

Yes, it should either be forbidden or at least be made clearer.

In fact you might prefer: if it's a numeric string, try to find it; if that
PID does not exist, extract it (treat it as a normal name).

I think usability-wise, that would make it easier to join the wrong sandbox by
accident.

I would do one of the following:

  • do nothing
  • implement "if it's a numeric string, try to find it; if that PID does not
    exist, extract it (treat it as a normal name)."
  • update the man page to be clearer

For the sake of simplicity, if --name=1234 is given, I would just print an
error such as the following:

Error: --name= cannot contain only numbers as that is treated as a PID in --join= and other commands

I don't see the benefit of allowing numbers only since that precludes using the
name in all of the applicable commands (which appear to be about 27 commands
when searching for name|pid in firejail(1)).

@layderv
Copy link
Contributor Author

layderv commented Jan 11, 2023

@kmk3 updated

@layderv layderv changed the title Extract pid of jail named with only numbers Do not create jails named with numbers Jan 11, 2023
Copy link
Collaborator

@kmk3 kmk3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a few suggestions, but it seems good overall.

src/firejail/main.c Outdated Show resolved Hide resolved
src/firejail/profile.c Outdated Show resolved Hide resolved
src/man/firejail.txt Outdated Show resolved Hide resolved
@kmk3
Copy link
Collaborator

kmk3 commented Jan 12, 2023

Regarding the commit message:

Names cannot contain only numbers

Names cannot contain only numbers. Numbers are used in other commands
as PIDs.

To make it clearer (and also in the style of git in the title), change it to
something like:

Die if sandbox name contains only digits

Names should not contain only numbers, as they are used in other commands as
PIDs.

Or:

Prevent sandbox name from containing only digits

Names should not contain only numbers, as they are used in other commands as
PIDs.

See the following for details:

@layderv
Copy link
Contributor Author

layderv commented Jan 12, 2023

@kmk3 thank you for your review! Updated

@kmk3 kmk3 changed the title Do not create jails named with numbers Prevent sandbox name from containing only digits Jan 13, 2023
@kmk3
Copy link
Collaborator

kmk3 commented Jan 13, 2023

@layderv on Jan 13:

@kmk3 thank you for your review! Updated

No problem :)

By the way, the CI errors are apparently due to Azure:

From https://github.com/netblue30/firejail/actions/runs/3905345101/jobs/6679876855:

Ign:4 https://azure.archive.ubuntu.com/ubuntu jammy-updates/main amd64 libapparmor-dev amd64 3.0.4-2ubuntu2.1
Err:1 https://azure.archive.ubuntu.com/ubuntu jammy/universe amd64 tcl-expect amd64 5.45.4-2build1
  Could not connect to azure.archive.ubuntu.com:80 (52.147.219.192), connection timed out
Err:2 https://azure.archive.ubuntu.com/ubuntu jammy/universe amd64 expect amd64 5.45.4-2build1
  Unable to connect to azure.archive.ubuntu.com:http:
Err:3 https://azure.archive.ubuntu.com/ubuntu jammy/universe amd64 xzdec amd64 5.2.5-2ubuntu1
  Unable to connect to azure.archive.ubuntu.com:http:
Err:4 https://azure.archive.ubuntu.com/ubuntu jammy-updates/main amd64 libapparmor-dev amd64 3.0.4-2ubuntu2.1
  Unable to connect to azure.archive.ubuntu.com:http:
E: Failed to fetch https://azure.archive.ubuntu.com/ubuntu/pool/universe/e/expect/tcl-expect_5.45.4-2build1_amd64.deb  Could not connect to azure.archive.ubuntu.com:80 (52.147.219.192), connection timed out
E: Failed to fetch https://azure.archive.ubuntu.com/ubuntu/pool/universe/e/expect/expect_5.45.4-2build1_amd64.deb  Unable to connect to azure.archive.ubuntu.com:http:
E: Failed to fetch https://azure.archive.ubuntu.com/ubuntu/pool/universe/x/xz-utils/xzdec_5.2.5-2ubuntu1_amd64.deb  Unable to connect to azure.archive.ubuntu.com:http:
E: Failed to fetch https://azure.archive.ubuntu.com/ubuntu/pool/main/a/apparmor/libapparmor-dev_3.0.4-2ubuntu2.1_amd64.deb  Unable to connect to azure.archive.ubuntu.com:http:
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?
Error: Process completed with exit code 100.

@kmk3 kmk3 changed the title Prevent sandbox name from containing only digits modif: Prevent sandbox name from containing only digits Jan 16, 2023
Names should not contain only numbers,
as they are used in other commands as PIDs.
Copy link
Collaborator

@kmk3 kmk3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@netblue30 netblue30 merged commit 9bc8a72 into netblue30:master Jan 31, 2023
@netblue30
Copy link
Owner

Merged, thanks for the patch!

kmk3 added a commit that referenced this pull request Feb 5, 2023
Added on commit 897f579 ("merges", 2023-01-30).

Relates to #5578.
kmk3 added a commit that referenced this pull request Feb 14, 2023
This amends commit 707f48a ("RELNOTES", 2023-02-14).

Note: The "Allow only letters and digits" modif item was implemented on
commit b4ffaa2 ("merges; more on cleaning up esc chars", 2023-02-14)
and relates to both #5578 and #5613.  The "--hostname" part of both the
"Prevent" and the "Allow" modif items was also only added on that
commit.  Discussion about the hostname:
#5613 (comment)

Relates to #5578.
kmk3 added a commit to kmk3/firejail that referenced this pull request Mar 21, 2023
The "invalid_name" function claims to "allow strict ASCII letters and
numbers".

However, it uses isalnum(3) and isdigit(3), which may take the current
locale into account and thus return 1 for non-ASCII characters.

So add the following functions:

* ascii_isalnum
* ascii_isalpha
* ascii_isdigit
* ascii_isxdigit

And use them in "invalid_name" so that it actually uses strictly ASCII
in its comparisons.

Added on commit b4ffaa2 ("merges; more on cleaning up esc chars",
2023-02-14).

Relates to netblue30#5578.

Kind of relates to netblue30#5708.
kmk3 added a commit to kmk3/firejail that referenced this pull request Mar 21, 2023
The "invalid_name" function claims to "allow strict ASCII letters and
numbers".

However, it uses isalnum(3) and isdigit(3), which may take the current
locale into account and thus return 1 for non-ASCII characters.

So add the following functions:

* ascii_isalnum
* ascii_isalpha
* ascii_isdigit
* ascii_isxdigit

And use them in "invalid_name" so that it actually uses strictly ASCII
in its comparisons.

Added on commit b4ffaa2 ("merges; more on cleaning up esc chars",
2023-02-14).

Relates to netblue30#5578.

Kind of relates to netblue30#5708.
kmk3 added a commit to kmk3/firejail that referenced this pull request Mar 21, 2023
The "invalid_name" function claims to "allow strict ASCII letters and
numbers".

However, it uses isalnum(3) and isdigit(3), which may take the current
locale into account and thus return 1 for non-ASCII characters.

So add the following functions:

* ascii_isalnum
* ascii_isalpha
* ascii_isdigit
* ascii_isxdigit

And use them in "invalid_name" so that it actually uses strictly ASCII
in its comparisons.

Added on commit b4ffaa2 ("merges; more on cleaning up esc chars",
2023-02-14).

Relates to netblue30#5578.

Kind of relates to netblue30#5708.
kmk3 added a commit to kmk3/firejail that referenced this pull request Mar 21, 2023
The "invalid_name" function claims to "allow strict ASCII letters and
numbers".

However, it uses isalnum(3) and isdigit(3), which may take the current
locale into account and thus return 1 for non-ASCII characters.

So add the following functions:

* ascii_isalnum
* ascii_isalpha
* ascii_isdigit
* ascii_islower
* ascii_isupper
* ascii_isxdigit

And use the applicable ones in "invalid_name" so that it actually uses
strictly ASCII in its comparisons.

Added on commit b4ffaa2 ("merges; more on cleaning up esc chars",
2023-02-14).

Relates to netblue30#5578.

Kind of relates to netblue30#5708.
kmk3 added a commit to kmk3/firejail that referenced this pull request Jun 14, 2023
Changes:

* Use only `invalid_name` to check the name and hostname instead of
  ad-hoc checks
* Standardize empty/invalid error messages for name/hostname

Note: This makes the hostname validation less strict, though it still
forbids control characters and only numbers.

Relates to netblue30#5578 netblue30#5708.

See also commit b4ffaa2 ("merges; more on cleaning up esc chars",
2023-02-14).
kmk3 added a commit to kmk3/firejail that referenced this pull request Jun 14, 2023
Note that the sandbox name may also be set through the "join-or-start"
option.

Relates to netblue30#5578 netblue30#5708.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done (on RELNOTES)
Development

Successfully merging this pull request may close these issues.

3 participants