Bug 243106

Summary: jail(3): memory leak when resizing jail parameter list.
Product: Base System Reporter: Hans Christian Woithe <chwoithe>
Component: binAssignee: Mark Johnston <markj>
Status: Closed FIXED    
Severity: Affects Some People CC: markj
Priority: --- Keywords: patch
Version: 12.1-RELEASE   
Hardware: Any   
OS: Any   
Attachments:
Description Flags
proposed patch none

Description Hans Christian Woithe 2020-01-05 05:39:02 UTC
Created attachment 210458 [details]
proposed patch

reallocarray() is used in jailparam_all() to shrink the jail parameter list.  It appears that the final call to shrink the list is unsafe if the reallocation fails.  There is a possibility, for example, that NULL is assigned to *jpp and njp keeps its previous (likely non-zero) value.  jls, which uses jailparam_all(), will attempt to use this invalid list.

I have attached a proposed fix.
Comment 1 Mark Linimon freebsd_committer freebsd_triage 2020-01-05 21:19:18 UTC
Assign appropriately.

fwiw, the [patch] convention has been replaced by the use of the 'patch' Keyword.
Comment 2 commit-hook freebsd_committer freebsd_triage 2020-01-07 21:44:47 UTC
A commit references this bug:

Author: markj
Date: Tue Jan  7 21:44:27 UTC 2020
New revision: 356476
URL: https://svnweb.freebsd.org/changeset/base/356476

Log:
  libjail: Handle an error from reallocarray() when trimming the buffer.

  There is no API guarantee that realloc() will not fail when the buffer
  is shrinking.  Handle it by simply returning the untrimmed buffer.
  While this is unlikely to ever happen in practice, it seems worth
  handling just to silence static analyzer warnings.

  PR:		243106
  Submitted by:	Hans Christian Woithe <chwoithe@yahoo.com>
  MFC after:	1 week

Changes:
  head/lib/libjail/jail.c
Comment 3 commit-hook freebsd_committer freebsd_triage 2020-01-14 02:42:44 UTC
A commit references this bug:

Author: markj
Date: Tue Jan 14 02:41:53 UTC 2020
New revision: 356718
URL: https://svnweb.freebsd.org/changeset/base/356718

Log:
  MFC r356476:
  libjail: Handle an error from reallocarray() when trimming the buffer.

  PR:	243106

Changes:
_U  stable/12/
  stable/12/lib/libjail/jail.c
Comment 4 Mark Johnston freebsd_committer freebsd_triage 2020-01-14 02:44:08 UTC
Thanks for the report.