Skip to content

Fix handling of "r_limits" in Podman REST API /libpod/containers/create #25986

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

Merged
merged 1 commit into from
Apr 28, 2025

Conversation

Honny1
Copy link
Member

@Honny1 Honny1 commented Apr 25, 2025

The JSON decoder correctly cannot decode (overflow) negative values (e.g., -1) for fields of type uint64, as -1 is used to represent max in POSIXRlimit. To handle this, the request body is first decoded into a map to process the r_limits field separately. Logic has been added to parse the soft and hard limits, casting -1 to uint64 to represent max while rejecting other invalid negative values.

This approach ensures compatibility with the Podman CLI and remote API, which already handle -1 by casting it to uint64 (uint64(-1) equals MaxUint64) to signify max.

Fixes: https://issues.redhat.com/browse/RUN-2859
Fixes: #24886

I'm not sure how to test the max value of ulimit for rootless APIs. Because it sets the maximum value allowed on the server.

Does this PR introduce a user-facing change?

Fixed handling of r_limits in Libpod REST API to properly process -1 as  `max` values.

@openshift-ci openshift-ci bot added release-note do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. labels Apr 25, 2025
@github-actions github-actions bot added the kind/api-change Change to remote API; merits scrutiny label Apr 25, 2025
@Honny1 Honny1 force-pushed the fix-unlimited-ulimits branch 2 times, most recently from af78d7b to 828297d Compare April 25, 2025 17:26
Copy link

[NON-BLOCKING] Packit jobs failed. @containers/packit-build please check. Everyone else, feel free to ignore.

@Honny1 Honny1 force-pushed the fix-unlimited-ulimits branch 5 times, most recently from 6d4ad5a to c932678 Compare April 25, 2025 22:21
@Honny1 Honny1 marked this pull request as ready for review April 28, 2025 08:56
@openshift-ci openshift-ci bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Apr 28, 2025
Comment on lines 63 to 84
var input map[string]interface{}
if err := decoder.Decode(&input); err != nil {
utils.Error(w, http.StatusInternalServerError, fmt.Errorf("decode(): %w", err))
return
}

rLimits := []specs.POSIXRlimit{}
if rLimitsInput, ok := input["r_limits"].([]interface{}); ok {
if rLimits, err = parseRLimits(rLimitsInput); err != nil {
utils.Error(w, http.StatusBadRequest, fmt.Errorf("invalid rlimit: %w", err))
return
}
delete(input, "r_limits")
}

inputJSON, err := json.Marshal(input)
if err != nil {
utils.Error(w, http.StatusInternalServerError, fmt.Errorf("marshal(): %w", err))
return
}

if err := json.Unmarshal(inputJSON, &sg); err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

json unmarshal/marshal is not exactly fast and now we add the overhead of another cycle to each container creation.

I have no tested it but could we use something like

type specgentmp struct {
specgen.SpecGenerator
Rlimits []spec.POSIXRlimit `json:"r_limits,omitempty"` //change the type to something you can directly unmarshel -1 into.
}

Then use that convert from the Rlimits overwrite type to the real POSIXRlimit type and assign that back to the specgen type and then you can use the proper specgen type I think.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's working. Thank you, it's a very nice and elegant solution.

The JSON decoder correctly cannot decode (overflow) negative values (e.g., `-1`) for fields of type `uint64`, as `-1` is used to represent `max` in `POSIXRlimit`. To handle this, we use `tmpSpecGenerator` to decode the request body. The `tmpSpecGenerator` replaces the `POSIXRlimit` type with a `tmpRlimit` type that uses the `json.Number` type for decoding values. The `tmpRlimit` is then converted into the `POSIXRlimit` type and assigned to the `SpecGenerator`.

This approach ensures compatibility with the Podman CLI and remote API, which already handle `-1` by casting it to `uint64` (`uint64(-1)` equals `MaxUint64`) to signify `max`.

Fixes: https://issues.redhat.com/browse/RUN-2859
Fixes: containers#24886

Signed-off-by: Jan Rodák <hony.com@seznam.cz>
@Honny1 Honny1 force-pushed the fix-unlimited-ulimits branch from c932678 to e66ff39 Compare April 28, 2025 13:02
Copy link
Member

@Luap99 Luap99 left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

openshift-ci bot commented Apr 28, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: Honny1, Luap99

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci openshift-ci bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Apr 28, 2025
@baude
Copy link
Member

baude commented Apr 28, 2025

neat! LGTM

@TomSweeneyRedHat
Copy link
Member

LGTM

@TomSweeneyRedHat
Copy link
Member

/lgtm

@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. label Apr 28, 2025
@openshift-merge-bot openshift-merge-bot bot merged commit ce0bac2 into containers:main Apr 28, 2025
75 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. kind/api-change Change to remote API; merits scrutiny lgtm Indicates that a PR is ready to be merged. release-note
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Podman REST API /libpod/containers/create "r_limits" is type integer <uint64>
4 participants