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

use-after-free bugs in object variants #20305

Closed
planetis-m opened this issue Sep 4, 2022 · 2 comments
Closed

use-after-free bugs in object variants #20305

planetis-m opened this issue Sep 4, 2022 · 2 comments

Comments

@planetis-m
Copy link
Contributor

planetis-m commented Sep 4, 2022

What happened?

The bug happens specifically when deleting an item in a seq. The item taking it's place might not have the same case fields. Then =sink(x[i], move x[xl]) might leave the deleted item's fields still in memory! If the new item switches branches, you get a use-after-free bug.

type
  ContentNodeKind = enum
    P, Br, Text
  ContentNode = object
    case kind: ContentNodeKind
    of P: pChildren: seq[ContentNode]
    of Br: discard
    of Text: textStr: string

proc main =
  var x = ContentNode(kind: P, pChildren: @[
    ContentNode(kind: P, pChildren: @[ContentNode(kind: Text, textStr: "brrr")])
  ])
  x.pChildren.add ContentNode(kind: Br)
  x.pChildren.del(0)
  {.cast(uncheckedAssign).}:
    x.pChildren[0].kind = P
  echo x.pChildren

main()

Nim Version

Nim Compiler Version 1.7.1 [Linux: amd64]
Compiled at 2022-09-04
Copyright (c) 2006-2022 by Andreas Rumpf

git hash: b931e74
active boot switches: -d:release --gc:markAndSweep

Current Standard Output Logs

@[(kind: P, pChildren: @[(kind: Text, textStr: "brrr")])]
SIGSEGV: Illegal storage access. (Attempt to read from nil?)
Σφάλμα κατάτμησης (segmentation fault) (core dumped)

Expected Standard Output Logs

@[(kind: P, pChildren: @[])]

Possible Solution

No response

Additional Information

No response

@planetis-m
Copy link
Contributor Author

Fixed in #20300

Araq pushed a commit that referenced this issue Sep 5, 2022
) [backport]

prevent use-after-free bugs in cased objects

the bug happens specifically when deleting
an item in a seq. The item taking it's place
might not have the same case fields. Then =sink(x[i], move x[xl])
might leave the deleted fields still in memory!
If the new item switches branches again, you get a use-after-free bug.
@ringabout
Copy link
Member

A testcase will be appreciated.

narimiran pushed a commit that referenced this issue Sep 6, 2022
) [backport]

prevent use-after-free bugs in cased objects

the bug happens specifically when deleting
an item in a seq. The item taking it's place
might not have the same case fields. Then =sink(x[i], move x[xl])
might leave the deleted fields still in memory!
If the new item switches branches again, you get a use-after-free bug.

(cherry picked from commit 8dcf367)
planetis-m added a commit to planetis-m/Nim that referenced this issue Sep 8, 2022
ringabout pushed a commit that referenced this issue Sep 13, 2022
* add testcase for bug #20305

* Update tcaseobj.nim

Co-authored-by: Clay Sweetser <[email protected]>
capocasa pushed a commit to capocasa/Nim that referenced this issue Mar 31, 2023
 (nim-lang#20300) [backport]

prevent use-after-free bugs in cased objects

the bug happens specifically when deleting
an item in a seq. The item taking it's place
might not have the same case fields. Then =sink(x[i], move x[xl])
might leave the deleted fields still in memory!
If the new item switches branches again, you get a use-after-free bug.
capocasa pushed a commit to capocasa/Nim that referenced this issue Mar 31, 2023
* add testcase for bug nim-lang#20305

* Update tcaseobj.nim

Co-authored-by: Clay Sweetser <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants