@@ -7,8 +7,10 @@ use sp_runtime::AccountId32;
7
7
use sp_std:: vec;
8
8
9
9
pub const SUBNET_PRECOMPILE_INDEX : u64 = 2051 ;
10
+ // bytes with max lenght 1K
11
+ pub const MAX_SINGLE_PARAMETER_SIZE : usize = 1024 ;
10
12
// three bytes with max lenght 1K
11
- pub const MAX_PARAMETER_SIZE : usize = 3 * 1024 ;
13
+ pub const MAX_PARAMETER_SIZE : usize = 3 * MAX_SINGLE_PARAMETER_SIZE ;
12
14
13
15
// ss58 public key i.e., the contract sends funds it received to the destination address from the
14
16
// method parameter.
@@ -90,18 +92,46 @@ impl SubnetPrecompile {
90
92
fn parse_register_network_parameters (
91
93
data : & [ u8 ] ,
92
94
) -> Result < ( AccountId32 , vec:: Vec < u8 > , vec:: Vec < u8 > , vec:: Vec < u8 > ) , PrecompileFailure > {
93
- let ( pubkey, _) = get_pubkey ( data) ?;
95
+ let ( pubkey, dynamic_params) = get_pubkey ( data) ?;
96
+ let dynamic_data_len = dynamic_params. len ( ) ;
94
97
95
98
let mut buf = [ 0_u8 ; 4 ] ;
96
99
// get all start point for three data items: name, repo and contact
97
100
buf. copy_from_slice ( get_slice ( data, 60 , 64 ) ?) ;
98
101
let subnet_name_start: usize = u32:: from_be_bytes ( buf) as usize ;
102
+ if subnet_name_start > dynamic_data_len {
103
+ log:: error!(
104
+ "the start position of subnet name as {} is too big " ,
105
+ subnet_name_start
106
+ ) ;
107
+ return Err ( PrecompileFailure :: Error {
108
+ exit_status : ExitError :: InvalidRange ,
109
+ } ) ;
110
+ }
99
111
100
112
buf. copy_from_slice ( get_slice ( data, 92 , 96 ) ?) ;
101
113
let github_repo_start: usize = u32:: from_be_bytes ( buf) as usize ;
114
+ if github_repo_start > dynamic_data_len {
115
+ log:: error!(
116
+ "the start position of github repo as {} is too big " ,
117
+ github_repo_start
118
+ ) ;
119
+ return Err ( PrecompileFailure :: Error {
120
+ exit_status : ExitError :: InvalidRange ,
121
+ } ) ;
122
+ }
102
123
103
124
buf. copy_from_slice ( get_slice ( data, 124 , 128 ) ?) ;
104
125
let subnet_contact_start: usize = u32:: from_be_bytes ( buf) as usize ;
126
+ if subnet_contact_start > dynamic_data_len {
127
+ log:: error!(
128
+ "the start position of subnet contact as {} is too big " ,
129
+ subnet_contact_start
130
+ ) ;
131
+ return Err ( PrecompileFailure :: Error {
132
+ exit_status : ExitError :: InvalidRange ,
133
+ } ) ;
134
+ }
105
135
106
136
// get name
107
137
buf. copy_from_slice ( get_slice (
@@ -111,6 +141,13 @@ impl SubnetPrecompile {
111
141
) ?) ;
112
142
let subnet_name_len: usize = u32:: from_be_bytes ( buf) as usize ;
113
143
144
+ if subnet_name_len > MAX_SINGLE_PARAMETER_SIZE {
145
+ log:: error!( "the length of subnet nae as {} is too big" , subnet_name_len) ;
146
+ return Err ( PrecompileFailure :: Error {
147
+ exit_status : ExitError :: InvalidRange ,
148
+ } ) ;
149
+ }
150
+
114
151
let mut name_vec = vec ! [ 0 ; subnet_name_len] ;
115
152
name_vec. copy_from_slice ( get_slice (
116
153
data,
@@ -125,6 +162,15 @@ impl SubnetPrecompile {
125
162
github_repo_start + 32 ,
126
163
) ?) ;
127
164
let github_repo_len: usize = u32:: from_be_bytes ( buf) as usize ;
165
+ if github_repo_len > MAX_SINGLE_PARAMETER_SIZE {
166
+ log:: error!(
167
+ "the length of github repo as {} is too big" ,
168
+ github_repo_len
169
+ ) ;
170
+ return Err ( PrecompileFailure :: Error {
171
+ exit_status : ExitError :: InvalidRange ,
172
+ } ) ;
173
+ }
128
174
129
175
let mut repo_vec = vec ! [ 0 ; github_repo_len] ;
130
176
repo_vec. copy_from_slice ( get_slice (
@@ -140,6 +186,15 @@ impl SubnetPrecompile {
140
186
subnet_contact_start + 32 ,
141
187
) ?) ;
142
188
let subnet_contact_len: usize = u32:: from_be_bytes ( buf) as usize ;
189
+ if subnet_contact_len > MAX_SINGLE_PARAMETER_SIZE {
190
+ log:: error!(
191
+ "the length of subnet contact as {} is too big" ,
192
+ subnet_contact_len
193
+ ) ;
194
+ return Err ( PrecompileFailure :: Error {
195
+ exit_status : ExitError :: InvalidRange ,
196
+ } ) ;
197
+ }
143
198
144
199
let mut contact_vec = vec ! [ 0 ; subnet_contact_len] ;
145
200
contact_vec. copy_from_slice ( get_slice (
0 commit comments