Skip to content

Commit a5f43f7

Browse files
authored
fix(config,toolkit): harden keystore, grpc config and build supply chain (#6986)
1 parent a901f85 commit a5f43f7

22 files changed

Lines changed: 697 additions & 27 deletions

File tree

‎.github/workflows/codeql.yml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ jobs:
3434
- name: Checkout repository
3535
uses: actions/checkout@v5
3636

37+
- name: Validate Gradle wrapper
38+
uses: gradle/actions/wrapper-validation@v6
39+
3740
# Initializes the CodeQL tools for scanning.
3841
- name: Initialize CodeQL
3942
uses: github/codeql-action/init@v4

‎.github/workflows/integration-test-single-node.yml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ jobs:
2525
- name: Checkout java-tron
2626
uses: actions/checkout@v5
2727

28+
- name: Validate Gradle wrapper
29+
uses: gradle/actions/wrapper-validation@v6
30+
2831
- name: Set up JDK 8
2932
uses: actions/setup-java@v5
3033
with:

‎.github/workflows/pr-build.yml‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ jobs:
3636
steps:
3737
- uses: actions/checkout@v5
3838

39+
- name: Validate Gradle wrapper
40+
uses: gradle/actions/wrapper-validation@v6
41+
3942
- name: Set up JDK ${{ matrix.java }}
4043
uses: actions/setup-java@v5
4144
with:
@@ -72,6 +75,9 @@ jobs:
7275
steps:
7376
- uses: actions/checkout@v5
7477

78+
- name: Validate Gradle wrapper
79+
uses: gradle/actions/wrapper-validation@v6
80+
7581
- name: Set up JDK 17
7682
uses: actions/setup-java@v5
7783
with:
@@ -126,6 +132,9 @@ jobs:
126132
- name: Checkout code
127133
uses: actions/checkout@v5
128134

135+
- name: Validate Gradle wrapper
136+
uses: gradle/actions/wrapper-validation@v6
137+
129138
- name: Check Java version
130139
run: java -version
131140

@@ -183,6 +192,9 @@ jobs:
183192
- name: Checkout code
184193
uses: actions/checkout@v5
185194

195+
- name: Validate Gradle wrapper
196+
uses: gradle/actions/wrapper-validation@v6
197+
186198
- name: Check Java version
187199
run: java -version
188200

@@ -249,6 +261,9 @@ jobs:
249261
with:
250262
ref: ${{ github.event.pull_request.base.sha }}
251263

264+
- name: Validate Gradle wrapper
265+
uses: gradle/actions/wrapper-validation@v6
266+
252267
- name: Cache Gradle packages
253268
uses: actions/cache@v5
254269
with:

‎.github/workflows/pr-check.yml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ jobs:
110110
steps:
111111
- uses: actions/checkout@v5
112112

113+
- name: Validate Gradle wrapper
114+
uses: gradle/actions/wrapper-validation@v6
115+
113116
- name: Set up Python
114117
uses: actions/setup-python@v5
115118
with:

‎.github/workflows/release-build.yml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ jobs:
5454
with:
5555
ref: ${{ github.sha }}
5656

57+
- name: Validate Gradle wrapper
58+
uses: gradle/actions/wrapper-validation@v6
59+
5760
- name: Cache Gradle packages
5861
uses: actions/cache@v5
5962
with:
@@ -121,6 +124,9 @@ jobs:
121124
with:
122125
ref: ${{ github.sha }}
123126

127+
- name: Validate Gradle wrapper
128+
uses: gradle/actions/wrapper-validation@v6
129+
124130
- name: Set up Temurin 17
125131
uses: actions/setup-java@v5
126132
with:

‎README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ Fill in the private key of your SR account into the `localwitness` list in the c
163163
<your_private_key>
164164
]
165165
```
166+
167+
> **Note**: Plain private keys in `localwitness` (and the `-p/--private-key` CLI flag) are deprecated and insecure — they are readable by anyone with access to the config file, process list, or shell history. Use the encrypted [`localwitnesskeystore`](https://tronprotocol.github.io/documentation-en/using_javatron/toolkit/) instead.
168+
166169
Check [Starting a Block Production Node](https://tronprotocol.github.io/documentation-en/using_javatron/installing_javatron/#starting-a-block-production-node) for more details.
167170
You could also test the process by connecting to a testnet or setting up a private network.
168171

‎common/src/main/java/org/tron/core/config/args/NodeConfig.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ public static class RpcConfig {
221221
private int maxConcurrentCallsPerConnection =
222222
DEFAULT_MAX_CONCURRENT_CALLS_PER_CONNECTION;
223223
private int flowControlWindow = 1048576;
224-
private long maxConnectionIdleInMillis = 0;
224+
private long maxConnectionIdleInMillis = 60000L;
225225
private long maxConnectionAgeInMillis = 0;
226226
private int maxMessageSize = 4194304;
227227
private int maxHeaderListSize = 8192;

‎common/src/main/resources/reference.conf‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ node {
288288
flowControlWindow = 1048576
289289

290290
# Connection idle timeout (ms). Connections idle longer than this are gracefully terminated. 0 = no limit
291-
maxConnectionIdleInMillis = 0
291+
maxConnectionIdleInMillis = 60000
292292

293293
# Connection max age (ms). 0 = no limit
294294
maxConnectionAgeInMillis = 0

‎common/src/test/java/org/tron/core/config/args/NodeConfigTest.java‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ public void testRpcDefaultsFromReference() {
108108
assertEquals(NodeConfig.RpcConfig.DEFAULT_MAX_CONCURRENT_CALLS_PER_CONNECTION,
109109
rpc.getMaxConcurrentCallsPerConnection());
110110
assertEquals(1048576, rpc.getFlowControlWindow());
111-
assertEquals(9223372036854775807L, rpc.getMaxConnectionIdleInMillis());
111+
// reference.conf recommends 60000 for idle; age stays 0 in
112+
// reference.conf, which postProcess maps to Long.MAX_VALUE.
113+
assertEquals(60000L, rpc.getMaxConnectionIdleInMillis());
112114
assertEquals(9223372036854775807L, rpc.getMaxConnectionAgeInMillis());
113115
assertEquals(4194304, rpc.getMaxMessageSize());
114116
assertEquals(8192, rpc.getMaxHeaderListSize());
@@ -149,6 +151,17 @@ public void testRpcNegativeConcurrentCallsRejected() {
149151
"node.rpc.maxConcurrentCallsPerConnection must be non-negative, got: -1"));
150152
}
151153

154+
@Test
155+
public void testRpcExplicitZeroIdleAndAgeMeansUnlimited() {
156+
// Legacy semantic preserved: an explicit 0 for idle/age means unlimited
157+
// (Long.MAX_VALUE). reference.conf recommends 60000 for idle instead.
158+
Config config = withRef(
159+
"node { rpc { maxConnectionIdleInMillis = 0, maxConnectionAgeInMillis = 0 } }");
160+
NodeConfig nc = NodeConfig.fromConfig(config);
161+
assertEquals(Long.MAX_VALUE, nc.getRpc().getMaxConnectionIdleInMillis());
162+
assertEquals(Long.MAX_VALUE, nc.getRpc().getMaxConnectionAgeInMillis());
163+
}
164+
152165
@Test
153166
public void testRpcRstDefaultsMatchReference() {
154167
NodeConfig.RpcConfig rpc = new NodeConfig.RpcConfig();

‎crypto/src/main/java/org/tron/keystore/Wallet.java‎

Lines changed: 98 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,32 @@ public class Wallet {
6363
private static final int CURRENT_VERSION = 3;
6464
private static final String CIPHER = "aes-128-ctr";
6565

66+
// Upper bounds for untrusted keystore KDF cost parameters. A hostile
67+
// keystore file may declare arbitrary KDF strengths: extreme scrypt
68+
// parameters make Bouncy Castle either allocate unbounded memory
69+
// (n*r*128 bytes — e.g. n=2^22, r=8 requests 4 GiB and dies with OOM)
70+
// or compute for hours, and out-of-range values can crash the JVM with
71+
// NegativeArraySizeException / ArithmeticException inside SCrypt.generate.
72+
// All values here are rejected by validationError() before any KDF runs.
73+
private static final int SCRYPT_N_MIN = 1 << 12;
74+
private static final int SCRYPT_N_MAX = 1 << 20;
75+
private static final int SCRYPT_R_MAX = 8;
76+
private static final int SCRYPT_P_MAX = 8;
77+
private static final long SCRYPT_MEMORY_MAX_BYTES = 1L << 30; // 128 * r * n cap = 1 GiB
78+
private static final int KDF_DKLEN_MAX = 128;
79+
private static final int PBKDF2_C_MAX = 1 << 20;
80+
6681
public static WalletFile create(String password, SignInterface sign, int n, int p)
6782
throws CipherException {
6883

84+
// Keep create() symmetric with validate()/decrypt(): reject KDF cost
85+
// parameters that the decryption path would refuse, so create() can
86+
// never produce a wallet file that only this class rejects.
87+
String paramError = scryptKdfParamsError(DKLEN, n, R, p);
88+
if (paramError != null) {
89+
throw new CipherException(paramError);
90+
}
91+
6992
byte[] salt = generateRandomBytes(32);
7093

7194
byte[] derivedKey = generateDerivedScryptKey(password.getBytes(UTF_8), salt, n, R, p, DKLEN);
@@ -194,15 +217,28 @@ public static SignInterface decrypt(String password, WalletFile walletFile)
194217
int p = scryptKdfParams.getP();
195218
int r = scryptKdfParams.getR();
196219
byte[] salt = ByteArray.fromHexString(scryptKdfParams.getSalt());
197-
derivedKey = generateDerivedScryptKey(password.getBytes(UTF_8), salt, n, r, p, dklen);
220+
try {
221+
derivedKey = generateDerivedScryptKey(password.getBytes(UTF_8), salt, n, r, p, dklen);
222+
} catch (RuntimeException e) {
223+
// Defense-in-depth: any residual KDF engine failure (e.g. an
224+
// IllegalArgumentException from Bouncy Castle) must surface as a
225+
// clean CipherException, never as a raw RuntimeException stack.
226+
// Errors (OutOfMemoryError etc.) are deliberately not caught —
227+
// they remain fatal.
228+
throw new CipherException("Scrypt key derivation failed", e);
229+
}
198230
} else if (kdfParams instanceof WalletFile.Aes128CtrKdfParams) {
199231
WalletFile.Aes128CtrKdfParams aes128CtrKdfParams =
200232
(WalletFile.Aes128CtrKdfParams) crypto.getKdfparams();
201233
int c = aes128CtrKdfParams.getC();
202234
String prf = aes128CtrKdfParams.getPrf();
203235
byte[] salt = ByteArray.fromHexString(aes128CtrKdfParams.getSalt());
204236

205-
derivedKey = generateAes128CtrDerivedKey(password.getBytes(UTF_8), salt, c, prf);
237+
try {
238+
derivedKey = generateAes128CtrDerivedKey(password.getBytes(UTF_8), salt, c, prf);
239+
} catch (RuntimeException e) {
240+
throw new CipherException("Pbkdf2 key derivation failed", e);
241+
}
206242
} else {
207243
throw new CipherException("Unable to deserialize params: " + crypto.getKdf());
208244
}
@@ -254,11 +290,15 @@ public static SignInterface decrypt(String password, WalletFile walletFile)
254290
/**
255291
* Returns a description of the first schema violation found in
256292
* {@code walletFile}, or {@code null} if the file matches the supported
257-
* V3 keystore shape (current version, known cipher, known KDF).
293+
* V3 keystore shape (current version, known cipher, known KDF, and KDF
294+
* cost parameters within safe bounds).
258295
*
259-
* <p>Shared by {@link #validate(WalletFile)} (which throws the message)
260-
* and {@link #isValidKeystoreFile(WalletFile)} (which returns boolean
261-
* for discovery-style filtering).
296+
* <p>KDF cost bounds are enforced here — the single chokepoint shared by
297+
* {@link #validate(WalletFile)} (which throws the message) and
298+
* {@link #isValidKeystoreFile(WalletFile)} (which returns boolean for
299+
* discovery-style filtering) — so untrusted keystore files can never
300+
* drive the KDF into unbounded memory allocation, multi-hour CPU
301+
* exhaustion, or engine-level crashes before {@link #decrypt} runs.
262302
*/
263303
private static String validationError(WalletFile walletFile) {
264304
if (walletFile.getVersion() != CURRENT_VERSION) {
@@ -276,6 +316,58 @@ private static String validationError(WalletFile walletFile) {
276316
if (kdf == null || (!kdf.equals(PBKDF2) && !kdf.equals(SCRYPT))) {
277317
return "KDF type is not supported";
278318
}
319+
WalletFile.KdfParams kdfParams = crypto.getKdfparams();
320+
if (kdfParams == null) {
321+
// Structural stubs (e.g. tooling fixtures) may omit kdfparams entirely;
322+
// there are no cost parameters to bound. Real keystores always carry them.
323+
return null;
324+
}
325+
if (SCRYPT.equals(kdf)) {
326+
if (!(kdfParams instanceof WalletFile.ScryptKdfParams)) {
327+
return "Scrypt KDF params are missing or malformed";
328+
}
329+
WalletFile.ScryptKdfParams params = (WalletFile.ScryptKdfParams) kdfParams;
330+
return scryptKdfParamsError(params.getDklen(), params.getN(), params.getR(), params.getP());
331+
}
332+
if (!(kdfParams instanceof WalletFile.Aes128CtrKdfParams)) {
333+
return "Pbkdf2 KDF params are missing or malformed";
334+
}
335+
WalletFile.Aes128CtrKdfParams params = (WalletFile.Aes128CtrKdfParams) kdfParams;
336+
return pbkdf2KdfParamsError(params.getDklen(), params.getC());
337+
}
338+
339+
private static String scryptKdfParamsError(int dklen, int n, int r, int p) {
340+
// dklen must yield at least 32 bytes: generateMac copies derivedKey[16..32)
341+
// and AES-128 needs bytes [0..16); smaller keys throw raw ArrayIndexOutOfBoundsException.
342+
if (dklen < 32 || dklen > KDF_DKLEN_MAX) {
343+
return "Scrypt dklen is out of range [32, " + KDF_DKLEN_MAX + "]: " + dklen;
344+
}
345+
if (n < SCRYPT_N_MIN || n > SCRYPT_N_MAX || (n & (n - 1)) != 0) {
346+
return "Scrypt n must be a power of 2 in [" + SCRYPT_N_MIN + ", " + SCRYPT_N_MAX
347+
+ "]: " + n;
348+
}
349+
if (r < 1 || r > SCRYPT_R_MAX) {
350+
return "Scrypt r is out of range [1, " + SCRYPT_R_MAX + "]: " + r;
351+
}
352+
if (p < 1 || p > SCRYPT_P_MAX) {
353+
return "Scrypt p is out of range [1, " + SCRYPT_P_MAX + "]: " + p;
354+
}
355+
// Compute in long arithmetic: hostile r * n must not overflow int.
356+
long memoryBytes = 128L * r * n;
357+
if (memoryBytes > SCRYPT_MEMORY_MAX_BYTES) {
358+
return "Scrypt memory requirement 128*r*n=" + memoryBytes
359+
+ " exceeds the " + SCRYPT_MEMORY_MAX_BYTES + " byte limit";
360+
}
361+
return null;
362+
}
363+
364+
private static String pbkdf2KdfParamsError(int dklen, int c) {
365+
if (dklen < 1 || dklen > KDF_DKLEN_MAX) {
366+
return "Pbkdf2 dklen is out of range [1, " + KDF_DKLEN_MAX + "]: " + dklen;
367+
}
368+
if (c < 1 || c > PBKDF2_C_MAX) {
369+
return "Pbkdf2 iteration count c is out of range [1, " + PBKDF2_C_MAX + "]: " + c;
370+
}
279371
return null;
280372
}
281373

0 commit comments

Comments
 (0)