<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.freebsd.org/bugzilla/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.freebsd.org/bugzilla/"
          
          maintainer="bugmeister@FreeBSD.org"
>

    <bug>
          <bug_id>255871</bug_id>
          
          <creation_ts>2021-05-14 12:22:02 +0000</creation_ts>
          <short_desc>net/rtsock: Fix a use after free in update_rtm_from_rc</short_desc>
          <delta_ts>2021-06-16 14:36:35 +0000</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>Base System</product>
          <component>kern</component>
          <version>CURRENT</version>
          <rep_platform>Any</rep_platform>
          <op_sys>Any</op_sys>
          <bug_status>Closed</bug_status>
          <resolution>FIXED</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>---</priority>
          <bug_severity>Affects Many People</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter>lylgood</reporter>
          <assigned_to name="Alexander V. Chernikov">melifaro</assigned_to>
          <cc>markj</cc>
    
    <cc>melifaro</cc>
    
    <cc>net</cc>
          

      

      

      <flag name="mfc-stable13"
          id="57683"
          type_id="10"
          status="+"
          setter="koobs"
    />
    <flag name="mfc-stable12"
          id="57684"
          type_id="9"
          status="?"
          setter="koobs"
    />
    <flag name="mfc-stable11"
          id="57685"
          type_id="8"
          status="?"
          setter="koobs"
    />

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1161307</commentid>
    <comment_count>0</comment_count>
      <attachid>224935</attachid>
    <who name="">lylgood</who>
    <bug_when>2021-05-14 12:22:02 +0000</bug_when>
    <thetext>Created attachment 224935
assigning the new allocated *prtm to rtm

Bug File: sys/net/rtsock.c

In function update_rtm_from_rc, *prtm is assigned to rtm at line 946 and the *prtm is freed by callee update_rtm_from_info() via free(orig_rtm, M_TEMP).
And then *prim was assigned with a new allocated rtm in this callee. 

But now, the rtm in the caller still points to a freed memory object. The freed memory object is used later by rtm-&gt;rtm_flags, rtm-&gt;rtm_rmx and rtm-&gt;rtm_rmx.rmx_weight...

The root of this uaf bug is reassignment of *prim in callee does not change the value of outside rtm. My patch fixs this error by assigning the new allocated *prtm to rtm.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1161366</commentid>
    <comment_count>1</comment_count>
    <who name="">commit-hook</who>
    <bug_when>2021-05-14 16:11:20 +0000</bug_when>
    <thetext>A commit in branch main references this bug:

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

commit 76cfc6fa0d819e7caca86c8299947cea1af9d6bc
Author:     Alexander V. Chernikov &lt;melifaro@FreeBSD.org&gt;
AuthorDate: 2021-05-14 16:03:08 +0000
Commit:     Alexander V. Chernikov &lt;melifaro@FreeBSD.org&gt;
CommitDate: 2021-05-14 16:06:41 +0000

    Fix a use after free in update_rtm_from_rc().

    update_rtm_from_rc() calls update_rtm_from_info() internally.
    The latter one may update provided prtm pointer with a new rtm.
    Reassign rtm from prtm afeter calling update_rtm_from_info() to
     avoid touching the freed rtm.

    PR:             255871
    Submitted by:   lylgood@foxmail.com
    MFC after:      3 days

 sys/net/rtsock.c | 1 +
 1 file changed, 1 insertion(+)</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1163760</commentid>
    <comment_count>2</comment_count>
    <who name="">commit-hook</who>
    <bug_when>2021-05-30 10:31:23 +0000</bug_when>
    <thetext>A commit in branch stable/13 references this bug:

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

commit d40def01a4f4fed65329f4a74792cad3134eeb29
Author:     Alexander V. Chernikov &lt;melifaro@FreeBSD.org&gt;
AuthorDate: 2021-05-14 16:03:08 +0000
Commit:     Alexander V. Chernikov &lt;melifaro@FreeBSD.org&gt;
CommitDate: 2021-05-30 10:30:53 +0000

    Fix a use after free in update_rtm_from_rc().

    update_rtm_from_rc() calls update_rtm_from_info() internally.
    The latter one may update provided prtm pointer with a new rtm.
    Reassign rtm from prtm afeter calling update_rtm_from_info() to
     avoid touching the freed rtm.

    PR:             255871
    Submitted by:   lylgood@foxmail.com

 sys/net/rtsock.c | 1 +
 1 file changed, 1 insertion(+)</thetext>
  </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="1"
              isprivate="0"
          >
            <attachid>224935</attachid>
            <date>2021-05-14 12:22:02 +0000</date>
            <delta_ts>2021-05-14 12:22:02 +0000</delta_ts>
            <desc>assigning the new allocated *prtm to rtm</desc>
            <filename>0001-net-uaf-update_rtm_from_rc.patch</filename>
            <type>text/plain</type>
            <size>468</size>
            <attacher>lylgood</attacher>
            
              <data encoding="base64">ZGlmZiAtLWdpdCBhL3N5cy9uZXQvcnRzb2NrLmMgYi9zeXMvbmV0L3J0c29jay5jCmluZGV4IDY5
MzBiZDE2N2FjMS4uM2NiNjQ1ZjQyZTRjIDEwMDY0NAotLS0gYS9zeXMvbmV0L3J0c29jay5jCisr
KyBiL3N5cy9uZXQvcnRzb2NrLmMKQEAgLTk3Niw2ICs5NzYsNyBAQCB1cGRhdGVfcnRtX2Zyb21f
cmMoc3RydWN0IHJ0X2FkZHJpbmZvICppbmZvLCBzdHJ1Y3QgcnRfbXNnaGRyICoqcHJ0bSwKIAlp
ZiAoKGVycm9yID0gdXBkYXRlX3J0bV9mcm9tX2luZm8oaW5mbywgcHJ0bSwgYWxsb2NfbGVuKSkg
IT0gMCkKIAkJcmV0dXJuIChlcnJvcik7CiAKKwlydG0gPSAqcHJ0bTsKIAlydG0tPnJ0bV9mbGFn
cyA9IHJjLT5yY19ydC0+cnRlX2ZsYWdzIHwgbmhvcF9nZXRfcnRmbGFncyhuaCk7CiAJaWYgKHJ0
bS0+cnRtX2ZsYWdzICYgUlRGX0dXRkxBR19DT01QQVQpCiAJCXJ0bS0+cnRtX2ZsYWdzID0gUlRG
X0dBVEVXQVkgfCAK
</data>

          </attachment>
      

    </bug>

</bugzilla>