gcc address sanitizer heap-buffer-overflow error during sort caused by change of condition? - g++

This is the bare-bone version of my function which illustrates the error.
File runtime-error.cpp:
#include <vector>
#include <algorithm>
using namespace std;
int main() {
using vi = vector<int>;
vector<vi> result = {{0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0},
{0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0},
{0}, {0}, {0}, {0}, {0}, {2}, {2}, {2}, {2}, {2},
{2}, {2}, {2}, {2}, {2}, {2}, {2}, {2}, {2}, {2},
{2}, {2}, {2}, {2}, {2}};
sort(result.begin(), result.end(), [](const vi& v1, const vi& v2) {
const auto v10 = v1[0];
const auto v20 = v2[0]; // line 15
return (v10 <= v20); // error condition
// return (v10 < v20); // no error condition
});
return 0;
}
I build it and run it with the following command:
g++ -Wall -g -fsanitize=address -fno-omit-frame-pointer
runtime-error.cpp -o re && ./re
gcc version:
gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1)
if the //no error condition is used, no problem. but as it is, the following error occurs:
==4046513==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x619000000068 at pc 0x55dec650f945 bp 0x7ffc95746530 sp 0x7ffc95746520
READ of size 8 at 0x619000000068 thread T0
#0 0x55dec650f944 in std::vector<int, std::allocator<int> >::operator[](unsigned long) const /usr/include/c++/9/bits/stl_vector.h:1061
#1 0x55dec650851e in operator() ./runtime-error.cpp:15
#2 0x55dec650d424 in operator()<__gnu_cxx::__normal_iterator<std::vector<int>*, std::vector<std::vector<int> > >, __gnu_cxx::__normal_iterator<std::vector<int>*, std::vector<std::vector<int> > > > /usr/include/c++/9/bits/predefined_ops.h:143
#3 0x55dec650d8c1 in __unguarded_partition<__gnu_cxx::__normal_iterator<std::vector<int>*, std::vector<std::vector<int> > >, __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(const vi&, const vi&)> > > /usr/include/c++/9/bits/stl_algo.h:1910
#4 0x55dec650c8ed in __unguarded_partition_pivot<__gnu_cxx::__normal_iterator<std::vector<int>*, std::vector<std::vector<int> > >, __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(const vi&, const vi&)> > > /usr/include/c++/9/bits/stl_algo.h:1928
#5 0x55dec650c311 in __introsort_loop<__gnu_cxx::__normal_iterator<std::vector<int>*, std::vector<std::vector<int> > >, long int, __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(const vi&, const vi&)> > > /usr/include/c++/9/bits/stl_algo.h:1958
#6 0x55dec650c011 in __sort<__gnu_cxx::__normal_iterator<std::vector<int>*, std::vector<std::vector<int> > >, __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(const vi&, const vi&)> > > /usr/include/c++/9/bits/stl_algo.h:1973
#7 0x55dec650bd55 in sort<__gnu_cxx::__normal_iterator<std::vector<int>*, std::vector<std::vector<int> > >, main()::<lambda(const vi&, const vi&)> > /usr/include/c++/9/bits/stl_algo.h:4905
#8 0x55dec650aec7 in main ./runtime-error.cpp:13
#9 0x7f42966df082 in __libc_start_main ../csu/libc-start.c:308
#10 0x55dec65083ed in _start (./re+0x13ed)
0x619000000068 is located 24 bytes to the left of 1080-byte region [0x619000000080,0x6190000004b8)
allocated by thread T0 here:
#0 0x7f4296d08587 in operator new(unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cc:104
#1 0x55dec65113bf in __gnu_cxx::new_allocator<std::vector<int, std::allocator<int> > >::allocate(unsigned long, void const*) /usr/include/c++/9/ext/new_allocator.h:114
#2 0x55dec6510f2e in std::allocator_traits<std::allocator<std::vector<int, std::allocator<int> > > >::allocate(std::allocator<std::vector<int, std::allocator<int> > >&, unsigned long) /usr/include/c++/9/bits/alloc_traits.h:443
#3 0x55dec6510749 in std::_Vector_base<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >::_M_allocate(unsigned long) /usr/include/c++/9/bits/stl_vector.h:343
#4 0x55dec650fe40 in void std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >::_M_range_initialize<std::vector<int, std::allocator<int> > const*>(std::vector<int, std::allocator<int> > const*, std::vector<int, std::allocator<int> > const*, std::forward_iterator_tag) /usr/include/c++/9/bits/stl_vector.h:1579
#5 0x55dec650f588 in std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >::vector(std::initializer_list<std::vector<int, std::allocator<int> > >, std::allocator<std::vector<int, std::allocator<int> > > const&) /usr/include/c++/9/bits/stl_vector.h:626
#6 0x55dec650a47a in main ./runtime-error.cpp:11
#7 0x7f42966df082 in __libc_start_main ../csu/libc-start.c:308
SUMMARY: AddressSanitizer: heap-buffer-overflow /usr/include/c++/9/bits/stl_vector.h:1061 in std::vector<int, std::allocator<int> >::operator[](unsigned long) const
Shadow bytes around the buggy address:
0x0c327fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c327fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c327fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c327fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c327fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c327fff8000: fa fa fa fa fa fa fa fa fa fa fa fa fa[fa]fa fa
0x0c327fff8010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c327fff8020: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c327fff8030: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c327fff8040: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c327fff8050: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Shadow byte legend (one shadow byte represents 8 application bytes):
Addressable: 00
Partially addressable: 01 02 03 04 05 06 07
Heap left redzone: fa
Freed heap region: fd
Stack left redzone: f1
Stack mid redzone: f2
Stack right redzone: f3
Stack after return: f5
Stack use after scope: f8
Global redzone: f9
Global init order: f6
Poisoned by user: f7
Container overflow: fc
Array cookie: ac
Intra object redzone: bb
ASan internal: fe
Left alloca redzone: ca
Right alloca redzone: cb
Shadow gap: cc
==4046513==ABORTING

<= does not conform to the semantics of strict partial ordering while < does.

Related

Relocation R_X86_64_PC32: why value is x-4 instead of x?

Consider this code:
int arr[4];
void foo(void)
{
arr[0] = arr[1];
}
compiled and objdumped as:
gcc t57.c -O3 -c && objdump -Dr t57.o
leading to:
0000000000000000 <foo>:
0: f3 0f 1e fa endbr64
4: 8b 05 00 00 00 00 mov 0x0(%rip),%eax # a <foo+0xa>
6: R_X86_64_PC32 arr
a: 89 05 00 00 00 00 mov %eax,0x0(%rip) # 10 <foo+0x10>
c: R_X86_64_PC32 arr-0x4
10: c3 retq
Here we see arr and arr-0x4.
Question: why not arr+0x4 and arr? Where this -0x4 comes from?

GCP client library not working - SSL peer shut down incorrectly

I have sample code to fetch regions from Google Cloud API. This sample code works fine from my laptop (windows with OpenJDK 1.8 version). But the same code fails from kubernetes environment which has suse linux with OpenJDK 1.8 version.
From Suse linux side I get :
Exception in thread "main" java.io.IOException: Error getting access token for service account: Remote host closed connection during handshake
at com.google.auth.oauth2.ServiceAccountCredentials.refreshAccessToken(ServiceAccountCredentials.java:444)
at com.google.auth.oauth2.OAuth2Credentials.refresh(OAuth2Credentials.java:157)
at com.google.auth.oauth2.OAuth2Credentials.getRequestMetadata(OAuth2Credentials.java:145)
at com.google.auth.oauth2.ServiceAccountCredentials.getRequestMetadata(ServiceAccountCredentials.java:603)
at com.google.auth.http.HttpCredentialsAdapter.initialize(HttpCredentialsAdapter.java:91)
at com.google.api.client.http.HttpRequestFactory.buildRequest(HttpRequestFactory.java:91)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.buildHttpRequest(AbstractGoogleClientRequest.java:404)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:514)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:455)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:565)
at sample.program.gcp.vpvn.regionList(vpvn.java:85)
at sample.program.gcp.vpvn.main(vpvn.java:307)
Caused by: javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:994)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1367)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1395)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1379)
at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1340)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1315)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:264)
at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:113)
at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:84)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1012)
at com.google.auth.oauth2.ServiceAccountCredentials.refreshAccessToken(ServiceAccountCredentials.java:441)
... 11 more
Caused by: java.io.EOFException: SSL peer shut down incorrectly
at sun.security.ssl.InputRecord.read(InputRecord.java:505)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:975)
... 23 more
When I enable SSL debug, I am not getting much details to troubleshoot this issue:
*** ClientHello, TLSv1.2
RandomCookie: GMT: 1616080171 bytes = { 119, 66, 219, 23, 171, 247, 221, 79, 45, 202, 181, 18, 229, 4, 65, 98, 207, 90, 0, 108, 43, 54, 80, 65, 39, 31, 49, 114 }
Session ID: {}
Cipher Suites: [TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384, TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384, TLS_RSA_WITH_AES_256_CBC_SHA256, TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384, TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384, TLS_DHE_RSA_WITH_AES_256_CBC_SHA256, TLS_DHE_DSS_WITH_AES_256_CBC_SHA256, TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, TLS_RSA_WITH_AES_256_CBC_SHA, TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA, TLS_ECDH_RSA_WITH_AES_256_CBC_SHA, TLS_DHE_RSA_WITH_AES_256_CBC_SHA, TLS_DHE_DSS_WITH_AES_256_CBC_SHA, TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, TLS_RSA_WITH_AES_128_CBC_SHA256, TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256, TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256, TLS_DHE_RSA_WITH_AES_128_CBC_SHA256, TLS_DHE_DSS_WITH_AES_128_CBC_SHA256, TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, TLS_RSA_WITH_AES_128_CBC_SHA, TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA, TLS_ECDH_RSA_WITH_AES_128_CBC_SHA, TLS_DHE_RSA_WITH_AES_128_CBC_SHA, TLS_DHE_DSS_WITH_AES_128_CBC_SHA, TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, TLS_RSA_WITH_AES_256_GCM_SHA384, TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384, TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384, TLS_DHE_RSA_WITH_AES_256_GCM_SHA384, TLS_DHE_DSS_WITH_AES_256_GCM_SHA384, TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, TLS_RSA_WITH_AES_128_GCM_SHA256, TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256, TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256, TLS_DHE_RSA_WITH_AES_128_GCM_SHA256, TLS_DHE_DSS_WITH_AES_128_GCM_SHA256, TLS_EMPTY_RENEGOTIATION_INFO_SCSV]
Compression Methods: { 0 }
***
[write] MD5 and SHA1 hashes: len = 215
0000: 01 00 00 D3 03 03 60 53 6D 2B 77 42 DB 17 AB F7 ......`Sm+wB....
0010: DD 4F 2D CA B5 12 E5 04 41 62 CF 5A 00 6C 2B 36 .O-.....Ab.Z.l+6
0020: 50 41 27 1F 31 72 00 00 56 C0 24 C0 28 00 3D C0 PA'.1r..V.$.(.=.
0030: 26 C0 2A 00 6B 00 6A C0 0A C0 14 00 35 C0 05 C0 &.*.k.j.....5...
0040: 0F 00 39 00 38 C0 23 C0 27 00 3C C0 25 C0 29 00 ..9.8.#.'.<.%.).
0050: 67 00 40 C0 09 C0 13 00 2F C0 04 C0 0E 00 33 00 g.#...../.....3.
0060: 32 C0 2C C0 2B C0 30 00 9D C0 2E C0 32 00 9F 00 2.,.+.0.....2...
0070: A3 C0 2F 00 9C C0 2D C0 31 00 9E 00 A2 00 FF 01 ../...-.1.......
0080: 00 00 54 00 0A 00 08 00 06 00 17 00 18 00 19 00 ..T.............
0090: 0B 00 02 01 00 00 0D 00 1C 00 1A 06 03 06 01 05 ................
00A0: 03 05 01 04 03 04 01 04 02 03 03 03 01 03 02 02 ................
00B0: 03 02 01 02 02 00 17 00 00 00 00 00 1A 00 18 00 ................
00C0: 00 15 6F 61 75 74 68 32 2E 67 6F 6F 67 6C 65 61 ..oauth2.googlea
00D0: 70 69 73 2E 63 6F 6D pis.com
main, WRITE: TLSv1.2 Handshake, length = 215
[Raw write]: length = 220
0000: 16 03 03 00 D7 01 00 00 D3 03 03 60 53 6D 2B 77 ...........`Sm+w
0010: 42 DB 17 AB F7 DD 4F 2D CA B5 12 E5 04 41 62 CF B.....O-.....Ab.
0020: 5A 00 6C 2B 36 50 41 27 1F 31 72 00 00 56 C0 24 Z.l+6PA'.1r..V.$
0030: C0 28 00 3D C0 26 C0 2A 00 6B 00 6A C0 0A C0 14 .(.=.&.*.k.j....
0040: 00 35 C0 05 C0 0F 00 39 00 38 C0 23 C0 27 00 3C .5.....9.8.#.'.<
0050: C0 25 C0 29 00 67 00 40 C0 09 C0 13 00 2F C0 04 .%.).g.#...../..
0060: C0 0E 00 33 00 32 C0 2C C0 2B C0 30 00 9D C0 2E ...3.2.,.+.0....
0070: C0 32 00 9F 00 A3 C0 2F 00 9C C0 2D C0 31 00 9E .2...../...-.1..
0080: 00 A2 00 FF 01 00 00 54 00 0A 00 08 00 06 00 17 .......T........
0090: 00 18 00 19 00 0B 00 02 01 00 00 0D 00 1C 00 1A ................
00A0: 06 03 06 01 05 03 05 01 04 03 04 01 04 02 03 03 ................
00B0: 03 01 03 02 02 03 02 01 02 02 00 17 00 00 00 00 ................
00C0: 00 1A 00 18 00 00 15 6F 61 75 74 68 32 2E 67 6F .......oauth2.go
00D0: 6F 67 6C 65 61 70 69 73 2E 63 6F 6D ogleapis.com
main, received EOFException: error
main, handling exception: javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
main, SEND TLSv1.2 ALERT: fatal, description = handshake_failure
Any hints on how to troubleshoot this issue?
Here with my sample code:
public static void main(String args[]) throws GeneralSecurityException, IOException {
Compute computeService = createComputeService();
Compute.Regions.List request = computeService.regions().list("imageagg-nonprod");
System.out.println("the list of regions for the selected project is \n");
RegionList response;
do {
response = request.execute();
if (response.getItems() == null) {
continue;
}
request.setPageToken(response.getNextPageToken());
} while (response.getNextPageToken() != null);
ArrayList regionNames = new ArrayList<String>();
HashMap<String, ArrayList<String>> ZoneList = new HashMap<>();
response.getItems().forEach(region -> {
ArrayList<String> zones = new ArrayList<String>();
regionNames.add(region.getName());
region.getZones().forEach(zone -> {
zones.add(Paths.get(URI.create(zone).getPath()).getFileName().toString());
});
ZoneList.put(region.getName(), zones);
});
System.out.println("list of region for selected project is \n");
regionNames.forEach(element -> {
System.out.println(element);
});
System.out.println("the names of regions and Zones for the selected Project is \n");
Set entries = ZoneList.entrySet();
Iterator it = entries.iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
}
machineList(ZoneList);
}
public static Compute createComputeService() throws IOException, GeneralSecurityException {
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
String proxyHostOpt = "web-proxy.in.software.net";
int proxyPort = 8080;
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
HttpTransport abc = new NetHttpTransport.Builder().trustCertificates(GoogleUtils.getCertificateTrustStore())
.setProxy(new Proxy(Proxy.Type.HTTP, InetSocketAddress.createUnresolved(proxyHostOpt, proxyPort))).build();
//GoogleCredential credential = GoogleCredential.getApplicationDefault(abc,jsonFactory);
List<String> scopes = new ArrayList<>();
//scopes.add("https://www.googleapis.com/auth/cloud-platform");
String jsonToken = "{\n" + " \"type\": \"service_account\",\n" + " \"project_id\": \"imageagg-nonprod\",\n" + " \"private_key_id\": \"99c871d2855b4d9388cc7a3a670a5764deb8c5e9\",\n" + " \"private_key\": \"-----BEGIN PRIVATE KEY-----\\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDh9k2JcCFrDZfm\\ng9DONfKe8xATwljEsW8FXMbPzU5JoXXsy1CYgkeW+eqXguQxFZM3HuI1W+mGBxgE\\n/K2P7XvJxylv7NajpgNmm4KGIh4hOpi+Sn3GVS31ftGM5A/CYKhRpr5uskr5PEin\\nDYxl0hUnfTodJCT+uxPxoCeN8aWuq5s+BapKKB8KVduUqmz3f8GL2Pc5wlm/YyOK\\nJYC781MAzLIFe8cLAVUJrVETqOtFTPCjy0yMGiUKxkyL20C11WFwfdD5ou0SD+6U\\nsT1YD/15KYh9GvV1E2XIPGzVtSHvU9h7FDRqOa+05QP3uDHegrAAib4PHA/A7KPD\\nBwkA6sW/AgMBAAECggEAHCPBtS9vIfdP5uecfcmvHMdVRbiquFgGZOsQYTmGmdnP\\nJz2MnGmBA9a8tc=\\n-----END PRIVATE KEY-----\\n\",\n" + " \"client_email\": \"315654350484-compute#developer.gserviceaccount.com\",\n" + " \"client_id\": \"112960668\",\n" + " \"auth_uri\": \"https://accounts.google.com/o/oauth2/auth\",\n" + " \"token_uri\": \"https://oauth2.googleapis.com/token\",\n" + " \"auth_provider_x509_cert_url\": \"https://www.googleapis.com/oauth2/v1/certs\",\n" + " \"client_x509_cert_url\": \"https://www.googleapis.com/robot/v1/metadata/x509/315654350484-compute%40developer.gserviceaccount.com\"\n" + "}";
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> map
= objectMapper.readValue(jsonToken, new TypeReference<Map<String,Object>>(){});
scopes.add(ComputeScopes.COMPUTE);
scopes.add(ComputeScopes.CLOUD_PLATFORM);
//scopes.add(ComputeScopes.DEVSTORAGE_FULL_CONTROL);
GoogleCredentials credentials = GoogleCredentials.fromStream(IOUtils.toInputStream(jsonToken, StandardCharsets.UTF_8)).createScoped(scopes);
ServiceAccountCredentials serviceAccountCredentials = ServiceAccountCredentials.fromStream(IOUtils.toInputStream(jsonToken, StandardCharsets.UTF_8));
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(credentials);
// Making call with credentials1 created with json string and proxy set as per requirements
return new Compute.Builder(abc, jsonFactory, requestInitializer).setApplicationName("hcmx").build();
}
My java version details:
java -version
openjdk version "11" 2018-09-25
OpenJDK Runtime Environment 18.9 (build 11+28)
OpenJDK 64-Bit Server VM 18.9 (build 11+28, mixed mode)
Environment where code is running:
[root#hcm-pool-centos76-3 ~]# uname -a
Linux hcm-pool-centos76-3 3.10.0-1062.9.1.el7.x86_64 #1 SMP Fri Dec 6 15:49:49 UTC 2

use STM32 crypto library to generate signature

I used STM32 crypto library(firmware implement) and set private key value generated by openssl , but I can't get the right signature.
No matter what the private key value or all-zero(
"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"), The R parameter of signature always
"D0 F4 7A FE 2F 31 76 B6 C3 1E BC 09 7E 57 81 0A B9 3A D9 65 01 39 90 D0".
Anyone can help?
int generate_sign(unsigned char* Input){
EC_st.pmA = P_192_a;
...
status = STM32_SHA256_HASH_DigestCompute((uint8_t*)TEST_ca,
sizeof(TEST_ca),
(uint8_t*)MessageDigest,
&MessageDigestLength);
if (status == HASH_SUCCESS)
{
status = STM32_Init_RNG_for_Sign( &RNGstate);
if (status == RNG_SUCCESS)
{
status = ECCinitEC(&EC_st, &Crypto_Buffer );
if (status == ECC_SUCCESS)
{
status = ECCinitPrivKey(&PrivKey, &EC_st, &Crypto_Buffer);
if (status == ECC_SUCCESS)
{
status = ECCsetPrivKeyValue(PrivKey,priv_key,priv_key_size);
if (status == ECC_SUCCESS)
{
status = ECDSAinitSign(&sign, &EC_st, &Crypto_Buffer);
if (status == ECC_SUCCESS)
{
signCtx.pmEC = &EC_st;
signCtx.pmPrivKey = PrivKey;
signCtx.pmRNG = &RNGstate;
status = ECDSAsign(MessageDigest, MessageDigestLength, sign, &signCtx, &Crypto_Buffer);
if (status == ECC_SUCCESS)
{
status = ECDSAgetSignature(sign, E_ECDSA_SIGNATURE_R_VALUE, sign_R, &sign_R_length);
status = ECDSAgetSignature(sign, E_ECDSA_SIGNATURE_S_VALUE, sign_S, &sign_S_length);
}
}
}
}
}
}
}
}
return 0;
}

memory leaks if using boost::asio::io_service::run in separate thread

I am trying to connect with a websocket using boost::asio library on win 7 32 bit. Normally, I get two memory leaks related openssl but If I am putting the boost::asio::io_service::run method in a separate thread, I am getting 6 memory leaks, out of which two leaks are of openssl lib.
Here is my code sample.
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <boost/array.hpp>
#include <boost/thread.hpp>
#include <iostream>
#include <string>
#include <vld.h>
boost::asio::io_service io_service;
boost::asio::ip::tcp::resolver resolver(io_service);
boost::array<char, 4096> buffer;
boost::thread thr; // thread object
boost::asio::ssl::context ctx(io_service, boost::asio::ssl::context::tlsv1_client);
boost::asio::ssl::stream<boost::asio::ip::tcp::socket> ssl_sock(io_service, ctx);
void read_handler(const boost::system::error_code &ec, std::size_t bytes_transferred)
{
boost::system::error_code ec1;
ssl_sock.lowest_layer().close(ec1);
}
void connect_handler(const boost::system::error_code &ec)
{
boost::system::error_code ec1;
ssl_sock.handshake(boost::asio::ssl::stream<boost::asio::ip::tcp::socket>::client, ec1);
std::string m = "GET / HTTP/1.1\r\n"
"Sec-WebSocket-Key: GCeEn+pBGdrdj4mcrtBukA==\r\n"
"Upgrade: websocket\r\n"
"Sec-WebSocket-Extensions: x-webkit-deflate-frame"
"Connection: Upgrade\r\n"
"Sec-WebSocket-Version: 13\r\n"
"\r\n";
if(!ec)
{
boost::asio::write(ssl_sock, boost::asio::buffer(m));
ssl_sock.async_read_some(boost::asio::buffer(buffer), read_handler);
}
std::cout<<ec.message().c_str();
}
void resolve_handler(const boost::system::error_code &ec, boost::asio::ip::tcp::resolver::iterator it)
{
if(!ec)
{
ssl_sock.lowest_layer().async_connect(*it, connect_handler);
}
std::cout<<ec.message().c_str();
}
int main()
{
ctx.set_verify_mode(boost::asio::ssl::verify_none);
boost::asio::ip::tcp::resolver::query query("echo.websocket.org", "443");
resolver.async_resolve(query, resolve_handler);
thr = boost::thread( boost::bind(&boost::asio::io_service::run, &io_service) );
thr.join();
// io_service.run();
}
On read handler, I am just closing the connect. Overall, I am just creating and closing the connection. Socket is closing properly as the error code is 0.
Following are the memory leaks.
d:\cfiles\projects\winssl\openssl-1.0.1e\crypto\mem.c (79): WebsocketPPTest.exe!default_malloc_ex + 0xB bytes
g:\websocketpptest\boost\asio\ssl\detail\impl\openssl_init.ipp (127): WebsocketPPTest.exe!boost::asio::ssl::detail::openssl_init_base::instance + 0x4A bytes
g:\websocketpptest\boost\asio\ssl\detail\openssl_init.hpp (61): WebsocketPPTest.exe!boost::asio::ssl::detail::openssl_init<1>::openssl_init<1> + 0x5A bytes
g:\websocketpptest\boost\asio\ssl\impl\context.ipp (171): WebsocketPPTest.exe!boost::asio::ssl::context::context + 0x6D bytes
g:\websocketpptest\src\boostsocket.cpp (19): WebsocketPPTest.exe!`dynamic initializer for 'ctx'' + 0x2F bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crt0dat.c (873): WebsocketPPTest.exe!_initterm
f:\dd\vctools\crt_bld\self_x86\crt\src\crt0dat.c (288): WebsocketPPTest.exe!_cinit + 0xF bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crt0.c (262): WebsocketPPTest.exe!__tmainCRTStartup + 0x7 bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crt0.c (189): WebsocketPPTest.exe!mainCRTStartup
0x76AF336A (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
0x77309F72 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x63 bytes
0x77309F45 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x36 bytes
Data:
00 00 00 00 40 68 71 00 01 00 00 00 04 00 00 00 ....#hq. ........
E0 96 4C 00 ..L..... ........
---------- Block 221 at 0x00716840: 16 bytes ----------
Call Stack:
d:\cfiles\projects\winssl\openssl-1.0.1e\crypto\mem.c (79): WebsocketPPTest.exe!default_malloc_ex + 0xB bytes
g:\websocketpptest\boost\asio\ssl\detail\impl\openssl_init.ipp (127): WebsocketPPTest.exe!boost::asio::ssl::detail::openssl_init_base::instance + 0x4A bytes
g:\websocketpptest\boost\asio\ssl\detail\openssl_init.hpp (61): WebsocketPPTest.exe!boost::asio::ssl::detail::openssl_init<1>::openssl_init<1> + 0x5A bytes
g:\websocketpptest\boost\asio\ssl\impl\context.ipp (171): WebsocketPPTest.exe!boost::asio::ssl::context::context + 0x6D bytes
g:\websocketpptest\src\boostsocket.cpp (19): WebsocketPPTest.exe!`dynamic initializer for 'ctx'' + 0x2F bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crt0dat.c (873): WebsocketPPTest.exe!_initterm
f:\dd\vctools\crt_bld\self_x86\crt\src\crt0dat.c (288): WebsocketPPTest.exe!_cinit + 0xF bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crt0.c (262): WebsocketPPTest.exe!__tmainCRTStartup + 0x7 bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crt0.c (189): WebsocketPPTest.exe!mainCRTStartup
0x76AF336A (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
0x77309F72 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x63 bytes
0x77309F45 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x36 bytes
Data:
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
---------- Block 5344 at 0x02A23008: 96 bytes ----------
Call Stack:
Data:
E0 31 A2 02 40 16 3F 00 00 16 3F 00 08 00 00 00 .1..#.?. ..?.....
10 00 00 00 00 00 00 00 08 00 00 00 00 02 00 00 ........ ........
00 01 00 00 01 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 1E 00 00 00 1C 00 00 00 ........ ........
01 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 ........ ........
1C 00 00 00 00 00 00 00 1D 00 00 00 00 00 00 00 ........ ........
---------- Block 5346 at 0x02A230A8: 12 bytes ----------
Call Stack:
Data:
D0 6A A2 02 00 00 00 00 E0 BE 01 00 .j...... ........
---------- Block 5345 at 0x02A231E0: 64 bytes ----------
Call Stack:
Data:
A8 30 A2 02 00 00 00 00 00 00 00 00 00 00 00 00 .0...... ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
---------- Block 5343 at 0x02A26AD0: 400 bytes ----------
Call Stack:
Data:
00 00 00 00 60 22 00 00 00 00 00 00 00 00 00 00 ....`".. ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........

Problem with publishing rtmp stream to FMS with librtmp

I'm trying to get video from the webcam and get it encoded and then publish the stream to FMS. and now I'am having a problem when I try to publish RTMP stream to the FMS with librtmp.
My code:
char uri[]="rtmp://127.0.0.1/live/bolton";
r= RTMP_Alloc();
RTMP_Init(r);
RTMP_SetupURL(r, (char*)uri);
r->Link.lFlags |= RTMP_LF_LIVE;
r->Link.lFlags |= RTMP_LF_BUFX;
RTMP_EnableWrite(r);
//RTMP_SetBufferMS(r, bufferTime);
RTMP_Connect(r, NULL);
RTMP_ConnectStream(r,0);
And log:
DEBUG: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
DEBUG: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
DEBUG: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
DEBUG: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
DEBUG: HandShake: Handshaking finished....
DEBUG: RTMP_Connect1, handshaked
DEBUG2: RTMP_SendPacket: fd=768, size=85
DEBUG2: 0000: 03 00 00 00 00 00 55 14 00 00 00 00 ......U.....
DEBUG2: 0000: 02 00 07 63 6f 6e 6e 65 63 74 00 3f f0 00 00 00 ...connect.?....
DEBUG2: 0010: 00 00 00 03 00 03 61 70 70 02 00 04 6c 69 76 65 ......app...live
DEBUG2: 0020: 00 04 74 79 70 65 02 00 0a 6e 6f 6e 70 72 69 76 ..type...nonpriv
DEBUG2: 0030: 61 74 65 00 05 74 63 55 72 6c 02 00 15 72 74 6d ate..tcUrl...rtm
DEBUG2: 0040: 70 3a 2f 2f 31 32 37 2e 30 2e 30 2e 31 2f 6c 69 p://127.0.0.1/li
DEBUG2: 0050: 76 65 00 00 09 ve...
DEBUG: Invoking connect
DEBUG2: RTMP_ReadPacket: fd=768
ERROR: RTMP_ReadPacket, failed to read RTMP packet header
DEBUG2: RTMP_SendPacket: fd=-1, size=307
It seems that RTMP_connect connected correctly, but failes in the function RTMP_ConnectStream, I'm not familiar with the rtmp connect sequence, and it's killing me.
What should I do to find the problems, Thanks very much!
I'm dealing with the same problem, but using librtmp with ffmpeg. If you look in the function RTMP_ReadPacket(), you'll see the error is thrown when trying to read the packet header with the method ReadN():
int
RTMP_ReadPacket(RTMP *r, RTMPPacket *packet)
{
uint8_t hbuf[RTMP_MAX_HEADER_SIZE] = { 0 };
char *header = (char *)hbuf;
int nSize, hSize, nToRead, nChunk;
int didAlloc = FALSE;
RTMP_Log(RTMP_LOGDEBUG2, "%s: fd=%d", __FUNCTION__, r->m_sb.sb_socket);
if (ReadN(r, (char *)hbuf, 1) == 0)
{
RTMP_Log(RTMP_LOGERROR, "%s, failed to read RTMP packet header", __FUNCTION__);
return FALSE;
}
That error only throws if the ReadN returns a 0. I haven't figured out on my end why that's happening yet, though.
I got the same issue in the Visual studio.
It turned out that in the debug mode,librtmp set some handshake value 0 instead of generating random data.
//handshake.h
/* generate random data */
#ifdef _DEBUG
memset(serversig+8, 0, RTMP_SIG_SIZE-8);
#else
ip = (int32_t *)(serversig+8);
for (i = 2; i < RTMP_SIG_SIZE/4; i++)
*ip++ = rand();
#endif
Just make sure generating random data.