Skip to content

fix(nano): Stop sharing NanoRNG object between contracts#1333

Merged
msbrogli merged 1 commit intomasterfrom
fix/nano-shared-rng
Jul 30, 2025
Merged

fix(nano): Stop sharing NanoRNG object between contracts#1333
msbrogli merged 1 commit intomasterfrom
fix/nano-shared-rng

Conversation

@msbrogli
Copy link
Member

@msbrogli msbrogli commented Jul 22, 2025

Motivation

The Nano runner previously returned the same NanoRNG instance to all contracts, allowing potential exploits where an attacker could override methods in one contract to manipulate random number generation in others. To mitigate this, several layers of protection have been introduced:

  • Class-level Protection: Method overriding is prevented using __setattr__() in a metaclass, ensuring the class definition remains immutable.
  • Instance-level Protection (Setattr): Direct overrides via __setattr__() are blocked on individual instances.
  • Instance-level Protection (Slots): Methods are made read-only through the use of __slots__, restricting attribute modifications.
  • Instance Isolation: Unique NanoRNG instances are now provided to each contract, eliminating sharing.
  • Class Isolation: Instances are created via a dynamic shell subclass, ensuring that even the NanoRNG class itself is not shared across contracts.

These measures collectively enhance security by isolating RNG behavior and preventing unauthorized modifications. These measures can also be applied to other classes and instances accessible by contracts.

Original discussion: https://github.com/HathorNetwork/nano-hathor-core/pull/168#discussion_r2025167397

Acceptance Criteria

  1. Add a test that exploits the vulnerability.
  2. Stop sharing NanoRNG objects between contracts.
  3. Each contract will have a single NanoRNG object which will be shared in case of multiple access by that contract.
  4. Add slots to NanoRNG.
  5. Prevent assigning methods in NanoRNG instances.
  6. Prevent assigning methods in NanoRNG classes.

Checklist

  • If you are requesting a merge into master, confirm this code is production-ready and can be included in future releases as soon as it gets merged

@msbrogli msbrogli requested a review from jansegre as a code owner July 22, 2025 16:42
@msbrogli msbrogli self-assigned this Jul 22, 2025
@msbrogli msbrogli moved this from Todo to In Progress (WIP) in Hathor Network Jul 22, 2025
@msbrogli msbrogli requested a review from glevco July 22, 2025 16:46
@msbrogli msbrogli moved this from In Progress (WIP) to In Progress (Done) in Hathor Network Jul 22, 2025
@msbrogli msbrogli force-pushed the fix/nano-shared-rng branch from ca750a4 to 00cec83 Compare July 22, 2025 16:48
@msbrogli msbrogli force-pushed the fix/nano-shared-rng branch 3 times, most recently from 3649819 to 1bf0004 Compare July 22, 2025 17:02
@github-actions
Copy link

github-actions bot commented Jul 22, 2025

🐰 Bencher Report

Branchfix/nano-shared-rng
Testbedubuntu-22.04
Click to view all benchmark results
BenchmarkLatencyBenchmark Result
minutes (m)
(Result Δ%)
Lower Boundary
minutes (m)
(Limit %)
Upper Boundary
minutes (m)
(Limit %)
sync-v2 (up to 20000 blocks)📈 view plot
🚷 view threshold
1.66 m
(+1.54%)Baseline: 1.64 m
1.47 m
(88.63%)
1.80 m
(92.31%)
🐰 View full continuous benchmarking report in Bencher

@msbrogli msbrogli force-pushed the fix/nano-shared-rng branch 2 times, most recently from 71c2e35 to a25f88a Compare July 23, 2025 19:35
glevco
glevco previously approved these changes Jul 23, 2025
@glevco glevco moved this from In Progress (Done) to In Review (WIP) in Hathor Network Jul 23, 2025
@msbrogli msbrogli force-pushed the fix/nano-shared-rng branch from a25f88a to 79aaeba Compare July 24, 2025 15:45
@msbrogli msbrogli force-pushed the fix/nano-shared-rng branch 3 times, most recently from 222f605 to 3af7c29 Compare July 24, 2025 16:38
@msbrogli msbrogli moved this from In Review (WIP) to In Progress (Done) in Hathor Network Jul 24, 2025
@msbrogli msbrogli force-pushed the fix/nano-shared-rng branch 3 times, most recently from 53280da to a70744d Compare July 24, 2025 17:12
@glevco glevco moved this from In Progress (Done) to In Review (WIP) in Hathor Network Jul 24, 2025
jansegre
jansegre previously approved these changes Jul 24, 2025
@codecov
Copy link

codecov bot commented Jul 24, 2025

Codecov Report

❌ Patch coverage is 96.55172% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 85.66%. Comparing base (5c883a7) to head (1927f37).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
hathor/nanocontracts/runner/runner.py 80.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1333   +/-   ##
=======================================
  Coverage   85.65%   85.66%           
=======================================
  Files         424      424           
  Lines       32095    32120   +25     
  Branches     4994     4997    +3     
=======================================
+ Hits        27492    27516   +24     
- Misses       3603     3604    +1     
  Partials     1000     1000           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

jansegre
jansegre previously approved these changes Jul 28, 2025
@msbrogli msbrogli force-pushed the fix/nano-shared-rng branch 2 times, most recently from 9be1685 to 0ea08d7 Compare July 29, 2025 20:40
glevco
glevco previously approved these changes Jul 29, 2025
@msbrogli msbrogli force-pushed the fix/nano-shared-rng branch from 0ea08d7 to 1927f37 Compare July 30, 2025 16:59
@msbrogli msbrogli moved this from In Review (WIP) to In Progress (Done) in Hathor Network Jul 30, 2025
@msbrogli msbrogli moved this from In Progress (Done) to In Review (WIP) in Hathor Network Jul 30, 2025
@msbrogli msbrogli merged commit 1927f37 into master Jul 30, 2025
7 checks passed
@msbrogli msbrogli deleted the fix/nano-shared-rng branch July 30, 2025 17:07
@github-project-automation github-project-automation bot moved this from In Review (WIP) to Waiting to be deployed in Hathor Network Jul 30, 2025
@jansegre jansegre moved this from Waiting to be deployed to Done in Hathor Network Aug 5, 2025
This was referenced Aug 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants