Skip to content

Commit d4e4288

Browse files
committed
CI: * https://source.denx.de/u-boot/custodians/u-boot-efi/-/pipelines/24904 UEFI: * Let efi_net_set_dp properly update the device path Network: * Avoid buffer overflows in wget_info with legacy TCP stack
2 parents 6ae0a57 + 32a6c5e commit d4e4288

File tree

2 files changed

+63
-16
lines changed

2 files changed

+63
-16
lines changed

lib/efi_loader/efi_net.c

+52-9
Original file line numberDiff line numberDiff line change
@@ -927,12 +927,15 @@ efi_status_t efi_net_register(void)
927927
&netobj->net);
928928
if (r != EFI_SUCCESS)
929929
goto failure_to_add_protocol;
930-
if (!net_dp)
931-
efi_net_set_dp("Net", NULL);
932-
r = efi_add_protocol(&netobj->header, &efi_guid_device_path,
933-
net_dp);
930+
931+
if (net_dp)
932+
r = efi_add_protocol(&netobj->header, &efi_guid_device_path,
933+
net_dp);
934+
else
935+
r = efi_net_set_dp("Net", NULL);
934936
if (r != EFI_SUCCESS)
935937
goto failure_to_add_protocol;
938+
936939
r = efi_add_protocol(&netobj->header, &efi_pxe_base_code_protocol_guid,
937940
&netobj->pxe);
938941
if (r != EFI_SUCCESS)
@@ -1057,18 +1060,58 @@ efi_status_t efi_net_register(void)
10571060
*/
10581061
efi_status_t efi_net_set_dp(const char *dev, const char *server)
10591062
{
1060-
efi_free_pool(net_dp);
1063+
efi_status_t ret = EFI_SUCCESS;
1064+
struct efi_handler *phandler;
1065+
struct efi_device_path *old_net_dp, *new_net_dp;
10611066

1062-
net_dp = NULL;
1067+
old_net_dp = net_dp;
1068+
new_net_dp = NULL;
10631069
if (!strcmp(dev, "Net"))
1064-
net_dp = efi_dp_from_eth();
1070+
new_net_dp = efi_dp_from_eth();
10651071
else if (!strcmp(dev, "Http"))
1066-
net_dp = efi_dp_from_http(server);
1072+
new_net_dp = efi_dp_from_http(server);
10671073

1068-
if (!net_dp)
1074+
if (!new_net_dp) {
10691075
return EFI_OUT_OF_RESOURCES;
1076+
}
1077+
1078+
// If netobj is not started yet, end here.
1079+
if (!netobj) {
1080+
goto exit;
1081+
}
1082+
1083+
phandler = NULL;
1084+
efi_search_protocol(&netobj->header, &efi_guid_device_path, &phandler);
1085+
1086+
// If the device path protocol is not yet installed, install it
1087+
if (!phandler)
1088+
goto add;
1089+
1090+
// If it is already installed, try to update it
1091+
ret = efi_reinstall_protocol_interface(&netobj->header, &efi_guid_device_path,
1092+
old_net_dp, new_net_dp);
1093+
if (ret != EFI_SUCCESS)
1094+
goto error;
1095+
1096+
net_dp = new_net_dp;
1097+
efi_free_pool(old_net_dp);
10701098

10711099
return EFI_SUCCESS;
1100+
add:
1101+
ret = efi_add_protocol(&netobj->header, &efi_guid_device_path,
1102+
new_net_dp);
1103+
if (ret != EFI_SUCCESS)
1104+
goto error;
1105+
exit:
1106+
net_dp = new_net_dp;
1107+
efi_free_pool(old_net_dp);
1108+
1109+
return ret;
1110+
error:
1111+
// Failed, restore
1112+
efi_free_pool(new_net_dp);
1113+
1114+
return ret;
10721115
}
10731116

10741117
/**

net/wget.c

+11-7
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ static inline int store_block(uchar *src, unsigned int offset, unsigned int len)
5353
ulong store_addr = image_load_addr + offset;
5454
uchar *ptr;
5555

56+
// Avoid overflow
57+
if (wget_info->buffer_size && wget_info->buffer_size < offset + len)
58+
return -1;
5659
if (CONFIG_IS_ENABLED(LMB) && wget_info->set_bootdev) {
5760
if (store_addr < image_load_addr ||
5861
lmb_read_check(store_addr, len)) {
@@ -98,12 +101,6 @@ static void tcp_stream_on_closed(struct tcp_stream *tcp)
98101
net_set_state(wget_loop_state);
99102
if (wget_loop_state != NETLOOP_SUCCESS) {
100103
net_boot_file_size = 0;
101-
if (wget_info->status_code == HTTP_STATUS_OK) {
102-
wget_info->status_code = HTTP_STATUS_BAD;
103-
wget_info->hdr_cont_len = 0;
104-
if (wget_info->headers)
105-
wget_info->headers[0] = 0;
106-
}
107104
printf("\nwget: Transfer Fail, TCP status - %d\n", tcp->status);
108105
return;
109106
}
@@ -212,6 +209,11 @@ static void tcp_stream_on_rcv_nxt_update(struct tcp_stream *tcp, u32 rx_bytes)
212209
"wget: Connected Len %lu\n",
213210
content_length);
214211
wget_info->hdr_cont_len = content_length;
212+
if (wget_info->buffer_size && wget_info->buffer_size < wget_info->hdr_cont_len){
213+
tcp_stream_reset(tcp);
214+
goto end;
215+
}
216+
215217
}
216218

217219
net_boot_file_size = rx_bytes - http_hdr_size;
@@ -227,7 +229,9 @@ static int tcp_stream_rx(struct tcp_stream *tcp, u32 rx_offs, void *buf, int len
227229
if ((max_rx_pos == (u32)(-1)) || (max_rx_pos < rx_offs + len - 1))
228230
max_rx_pos = rx_offs + len - 1;
229231

230-
store_block(buf, rx_offs - http_hdr_size, len);
232+
// Avoid overflow
233+
if (store_block(buf, rx_offs - http_hdr_size, len) < 0)
234+
return -1;
231235

232236
return len;
233237
}

0 commit comments

Comments
 (0)