You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by JoshRosen <gi...@git.apache.org> on 2018/01/08 23:54:32 UTC

[GitHub] spark pull request #20191: [SPARK-22997] Add additional defenses against use...

GitHub user JoshRosen opened a pull request:

    https://github.com/apache/spark/pull/20191

    [SPARK-22997] Add additional defenses against use of freed MemoryBlocks

    ## What changes were proposed in this pull request?
    
    This patch modifies Spark's `MemoryAllocator` implementations so that `free(MemoryBlock)` mutates the passed block to clear pointers (in the off-heap case) or null out references to backing `long[]` arrays (in the on-heap case). The goal of this change is to add an extra layer of defense against use-after-free bugs because currently it's hard to detect corruption caused by blind writes to freed memory blocks.
    
    ## How was this patch tested?
    
    New unit tests in `PlatformSuite`, including new tests for existing functionality because we did not have sufficient mutation coverage of the on-heap memory allocator's pooling logic.

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/JoshRosen/spark SPARK-22997-add-defenses-against-use-after-free-bugs-in-memory-allocator

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/spark/pull/20191.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #20191
    
----
commit a7f8c07fb5158f39bbb6cc1f23cfb13a0d473536
Author: Josh Rosen <jo...@...>
Date:   2018-01-08T23:50:18Z

    Add additional defenses against use of freed MemoryBlocks

----


---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #20191: [SPARK-22997] Add additional defenses against use...

Posted by JoshRosen <gi...@git.apache.org>.
Github user JoshRosen commented on a diff in the pull request:

    https://github.com/apache/spark/pull/20191#discussion_r160296722
  
    --- Diff: common/unsafe/src/main/java/org/apache/spark/unsafe/memory/UnsafeMemoryAllocator.java ---
    @@ -38,9 +38,20 @@ public MemoryBlock allocate(long size) throws OutOfMemoryError {
       public void free(MemoryBlock memory) {
         assert (memory.obj == null) :
           "baseObject not null; are you trying to use the off-heap allocator to free on-heap memory?";
    +    assert (memory.pageNumber != MemoryBlock.FREED_IN_ALLOCATOR_PAGE_NUMBER) :
    +      "page has already been freed";
    +    assert ((memory.pageNumber == MemoryBlock.NO_PAGE_NUMBER)
    +            || (memory.pageNumber == MemoryBlock.FREED_IN_TMM_PAGE_NUMBER)) :
    +      "TMM-allocated pages must be freed via TMM.freePage(), not directly in allocator free()";
    +
         if (MemoryAllocator.MEMORY_DEBUG_FILL_ENABLED) {
           memory.fill(MemoryAllocator.MEMORY_DEBUG_FILL_FREED_VALUE);
         }
         Platform.freeMemory(memory.offset);
    +    // As an additional layer of defense against use-after-free bugs, we mutate the
    +    // MemoryBlock to reset its pointer.
    +    memory.offset = 0;
    --- End diff --
    
    I think that it depends on whether the direct memory address stored in `memory.offset` corresponds to the address of memory allocated to the JVM.
    
    If we've freed the underlying Unsafe / Direct memory then I would expect a SIGSEGV, but if the address has been re-used by a subsequent allocation (or if we've done some buffer pooling and have recycled the underlying direct memory without freeing it) then this address could point to valid memory allocated by the JVM but which is currently in use by another Spark task, so reads or writes would trigger data corruption.


---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #20191: [SPARK-22997] Add additional defenses against use of fre...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/20191
  
    Test FAILed.
    Refer to this link for build results (access rights to CI server needed): 
    https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/85825/
    Test FAILed.


---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #20191: [SPARK-22997] Add additional defenses against use of fre...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/20191
  
    **[Test build #85895 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/85895/testReport)** for PR 20191 at commit [`a7f8c07`](https://github.com/apache/spark/commit/a7f8c07fb5158f39bbb6cc1f23cfb13a0d473536).


---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #20191: [SPARK-22997] Add additional defenses against use of fre...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/20191
  
    Test FAILed.
    Refer to this link for build results (access rights to CI server needed): 
    https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/85813/
    Test FAILed.


---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #20191: [SPARK-22997] Add additional defenses against use...

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/spark/pull/20191


---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #20191: [SPARK-22997] Add additional defenses against use of fre...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/20191
  
    Merged build finished. Test FAILed.


---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #20191: [SPARK-22997] Add additional defenses against use...

Posted by vanzin <gi...@git.apache.org>.
Github user vanzin commented on a diff in the pull request:

    https://github.com/apache/spark/pull/20191#discussion_r160296848
  
    --- Diff: common/unsafe/src/main/java/org/apache/spark/unsafe/memory/UnsafeMemoryAllocator.java ---
    @@ -38,9 +38,20 @@ public MemoryBlock allocate(long size) throws OutOfMemoryError {
       public void free(MemoryBlock memory) {
         assert (memory.obj == null) :
           "baseObject not null; are you trying to use the off-heap allocator to free on-heap memory?";
    +    assert (memory.pageNumber != MemoryBlock.FREED_IN_ALLOCATOR_PAGE_NUMBER) :
    +      "page has already been freed";
    +    assert ((memory.pageNumber == MemoryBlock.NO_PAGE_NUMBER)
    +            || (memory.pageNumber == MemoryBlock.FREED_IN_TMM_PAGE_NUMBER)) :
    +      "TMM-allocated pages must be freed via TMM.freePage(), not directly in allocator free()";
    +
         if (MemoryAllocator.MEMORY_DEBUG_FILL_ENABLED) {
           memory.fill(MemoryAllocator.MEMORY_DEBUG_FILL_FREED_VALUE);
         }
         Platform.freeMemory(memory.offset);
    +    // As an additional layer of defense against use-after-free bugs, we mutate the
    +    // MemoryBlock to reset its pointer.
    +    memory.offset = 0;
    --- End diff --
    
    Right, but I mean after your change, so I guess the answer in that case is SIGSEGV?


---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #20191: [SPARK-22997] Add additional defenses against use of fre...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/20191
  
    Merged build finished. Test PASSed.


---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #20191: [SPARK-22997] Add additional defenses against use of fre...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/20191
  
    **[Test build #85813 has finished](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/85813/testReport)** for PR 20191 at commit [`a7f8c07`](https://github.com/apache/spark/commit/a7f8c07fb5158f39bbb6cc1f23cfb13a0d473536).
     * This patch **fails Spark unit tests**.
     * This patch merges cleanly.
     * This patch adds no public classes.


---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #20191: [SPARK-22997] Add additional defenses against use of fre...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/20191
  
    **[Test build #85825 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/85825/testReport)** for PR 20191 at commit [`a7f8c07`](https://github.com/apache/spark/commit/a7f8c07fb5158f39bbb6cc1f23cfb13a0d473536).


---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #20191: [SPARK-22997] Add additional defenses against use...

Posted by JoshRosen <gi...@git.apache.org>.
Github user JoshRosen commented on a diff in the pull request:

    https://github.com/apache/spark/pull/20191#discussion_r160297024
  
    --- Diff: common/unsafe/src/main/java/org/apache/spark/unsafe/memory/UnsafeMemoryAllocator.java ---
    @@ -38,9 +38,20 @@ public MemoryBlock allocate(long size) throws OutOfMemoryError {
       public void free(MemoryBlock memory) {
         assert (memory.obj == null) :
           "baseObject not null; are you trying to use the off-heap allocator to free on-heap memory?";
    +    assert (memory.pageNumber != MemoryBlock.FREED_IN_ALLOCATOR_PAGE_NUMBER) :
    +      "page has already been freed";
    +    assert ((memory.pageNumber == MemoryBlock.NO_PAGE_NUMBER)
    +            || (memory.pageNumber == MemoryBlock.FREED_IN_TMM_PAGE_NUMBER)) :
    +      "TMM-allocated pages must be freed via TMM.freePage(), not directly in allocator free()";
    +
         if (MemoryAllocator.MEMORY_DEBUG_FILL_ENABLED) {
           memory.fill(MemoryAllocator.MEMORY_DEBUG_FILL_FREED_VALUE);
         }
         Platform.freeMemory(memory.offset);
    +    // As an additional layer of defense against use-after-free bugs, we mutate the
    +    // MemoryBlock to reset its pointer.
    +    memory.offset = 0;
    --- End diff --
    
    Yep, this will guarantee SIGSEGV instead of corruption.


---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #20191: [SPARK-22997] Add additional defenses against use...

Posted by vanzin <gi...@git.apache.org>.
Github user vanzin commented on a diff in the pull request:

    https://github.com/apache/spark/pull/20191#discussion_r160295529
  
    --- Diff: common/unsafe/src/main/java/org/apache/spark/unsafe/memory/UnsafeMemoryAllocator.java ---
    @@ -38,9 +38,20 @@ public MemoryBlock allocate(long size) throws OutOfMemoryError {
       public void free(MemoryBlock memory) {
         assert (memory.obj == null) :
           "baseObject not null; are you trying to use the off-heap allocator to free on-heap memory?";
    +    assert (memory.pageNumber != MemoryBlock.FREED_IN_ALLOCATOR_PAGE_NUMBER) :
    +      "page has already been freed";
    +    assert ((memory.pageNumber == MemoryBlock.NO_PAGE_NUMBER)
    +            || (memory.pageNumber == MemoryBlock.FREED_IN_TMM_PAGE_NUMBER)) :
    +      "TMM-allocated pages must be freed via TMM.freePage(), not directly in allocator free()";
    +
         if (MemoryAllocator.MEMORY_DEBUG_FILL_ENABLED) {
           memory.fill(MemoryAllocator.MEMORY_DEBUG_FILL_FREED_VALUE);
         }
         Platform.freeMemory(memory.offset);
    +    // As an additional layer of defense against use-after-free bugs, we mutate the
    +    // MemoryBlock to reset its pointer.
    +    memory.offset = 0;
    --- End diff --
    
    Out of curiosity, what's the failure mode if this block is used after free? SIGSEGV or exception?


---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #20191: [SPARK-22997] Add additional defenses against use of fre...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/20191
  
    **[Test build #85825 has finished](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/85825/testReport)** for PR 20191 at commit [`a7f8c07`](https://github.com/apache/spark/commit/a7f8c07fb5158f39bbb6cc1f23cfb13a0d473536).
     * This patch **fails Spark unit tests**.
     * This patch merges cleanly.
     * This patch adds no public classes.


---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #20191: [SPARK-22997] Add additional defenses against use of fre...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/20191
  
    Test PASSed.
    Refer to this link for build results (access rights to CI server needed): 
    https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/85895/
    Test PASSed.


---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #20191: [SPARK-22997] Add additional defenses against use of fre...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/20191
  
    Merged build finished. Test FAILed.


---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #20191: [SPARK-22997] Add additional defenses against use of fre...

Posted by JoshRosen <gi...@git.apache.org>.
Github user JoshRosen commented on the issue:

    https://github.com/apache/spark/pull/20191
  
    jenkins retest this please


---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #20191: [SPARK-22997] Add additional defenses against use of fre...

Posted by JoshRosen <gi...@git.apache.org>.
Github user JoshRosen commented on the issue:

    https://github.com/apache/spark/pull/20191
  
    I'm merging this into master and branch-2.3.


---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #20191: [SPARK-22997] Add additional defenses against use of fre...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/20191
  
    **[Test build #85895 has finished](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/85895/testReport)** for PR 20191 at commit [`a7f8c07`](https://github.com/apache/spark/commit/a7f8c07fb5158f39bbb6cc1f23cfb13a0d473536).
     * This patch passes all tests.
     * This patch merges cleanly.
     * This patch adds no public classes.


---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #20191: [SPARK-22997] Add additional defenses against use of fre...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/20191
  
    **[Test build #85813 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/85813/testReport)** for PR 20191 at commit [`a7f8c07`](https://github.com/apache/spark/commit/a7f8c07fb5158f39bbb6cc1f23cfb13a0d473536).


---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #20191: [SPARK-22997] Add additional defenses against use of fre...

Posted by JoshRosen <gi...@git.apache.org>.
Github user JoshRosen commented on the issue:

    https://github.com/apache/spark/pull/20191
  
    jenkins retest this please


---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org