Bug 228432

Summary: [PATCH] sysctl `KERN_PROC_CWD` should return `ENOMEM` when the output buffer isn't big enough
Product: Base System Reporter: Brennan Vincent <brennan>
Component: kernAssignee: Mark Johnston <markj>
Status: Closed FIXED    
Severity: Affects Only Me CC: chris, emaste, markj
Priority: --- Keywords: patch
Version: CURRENT   
Hardware: Any   
OS: Any   
Bug Depends on:    
Bug Blocks: 240573    
Attachments:
Description Flags
patch fixing the issue none

Description Brennan Vincent 2018-05-23 04:04:53 UTC
Created attachment 193626 [details]
patch fixing the issue

`man 3 sysctl` states: "If the amount of data available is greater than the size of the buffer supplied, the call supplies as much data as fits in the buffer provided and returns with the error code ENOMEM."

This is not true in the case of `KERN_PROC_CWD`, in which case `sysctl` returns 0, indicating success.
Comment 1 Mark Johnston freebsd_committer freebsd_triage 2018-05-23 15:27:20 UTC
I think this change is right. Note that export_kinfo_to_sb() is used in other contexts, but in most of them the return value isn't checked anyway.
Comment 2 Mark Johnston freebsd_committer freebsd_triage 2018-05-23 15:31:27 UTC
I also meant to point out that we don't seem to have any applications relying on the current behaviour, which seems to have been introduced relatively recently: r249487.
Comment 3 Brennan Vincent 2018-05-23 17:02:54 UTC
This is my first patch to FreeBSD so forgive my ignorance -- what are the next steps? Can you land the change for me Mark? Or do I need to find someone else to review it, and if so who?
Comment 4 Mark Johnston freebsd_committer freebsd_triage 2018-05-23 17:08:00 UTC
(In reply to Brennan Vincent from comment #3)
I'll take care of getting it reviewed and committed.
Comment 5 Mark Johnston freebsd_committer freebsd_triage 2018-05-29 19:51:21 UTC
https://reviews.freebsd.org/D15607
Comment 6 Brennan Vincent 2019-07-28 21:11:40 UTC
Ping: Mark, is there any good reason your fix in D15607 never landed?
Comment 7 Mark Johnston freebsd_committer freebsd_triage 2019-08-08 21:19:25 UTC
(In reply to Brennan Vincent from comment #6)
I thought I had found some problem with the patch, but didn't have time to revisit it and it slipped from my todo list.  I'll revisit it shortly, apologies for the delay.
Comment 8 commit-hook freebsd_committer freebsd_triage 2021-12-17 18:12:26 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=36bd49ac4db8c62a3b354094646cbd8547ad2c51

commit 36bd49ac4db8c62a3b354094646cbd8547ad2c51
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2021-12-16 21:07:04 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2021-12-17 18:10:23 +0000

    fd: Avoid truncating output buffers for KERN_PROC_{CWD,FILEDESC}

    These sysctls failed to return an error if the caller had provided too
    short an output buffer.  Change them to return ENOMEM instead, to ensure
    that callers can detect truncation in the face of a concurrently
    changing fd table.

    PR:             228432
    Discussed with: cem, jhb
    MFC after:      1 month
    Differential Revision:  https://reviews.freebsd.org/D15607

 sys/kern/kern_descrip.c | 54 ++++++++++++++++++++++++++-----------------------
 1 file changed, 29 insertions(+), 25 deletions(-)
Comment 9 commit-hook freebsd_committer freebsd_triage 2022-01-16 15:42:03 UTC
A commit in branch stable/13 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=af30714ff433bad84b9d5e72740991fee7cb1414

commit af30714ff433bad84b9d5e72740991fee7cb1414
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2021-12-16 21:07:04 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2022-01-16 15:40:25 +0000

    fd: Avoid truncating output buffers for KERN_PROC_{CWD,FILEDESC}

    These sysctls failed to return an error if the caller had provided too
    short an output buffer.  Change them to return ENOMEM instead, to ensure
    that callers can detect truncation in the face of a concurrently
    changing fd table.

    PR:             228432
    Discussed with: cem, jhb

    (cherry picked from commit 36bd49ac4db8c62a3b354094646cbd8547ad2c51)

 sys/kern/kern_descrip.c | 54 ++++++++++++++++++++++++++-----------------------
 1 file changed, 29 insertions(+), 25 deletions(-)