Skip to content

Commit

Permalink
Merge upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
Leymooo committed Aug 22, 2024
2 parents a40af99 + 5341487 commit e249b4f
Show file tree
Hide file tree
Showing 22 changed files with 132 additions and 37 deletions.
6 changes: 4 additions & 2 deletions native/compile-native.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

set -eu

CWD=$(pwd)

echo "Compiling mbedtls"
(cd mbedtls && make no_test)
(cd mbedtls && CFLAGS="-fPIC -I$CWD/src/main/c -DMBEDTLS_USER_CONFIG_FILE='<mbedtls_custom_config.h>'" make no_test)

echo "Compiling zlib"
(cd zlib && CFLAGS=-fPIC ./configure --static && make)
(cd zlib && CFLAGS="-fPIC -DNO_GZIP" ./configure --static && make)

CC="gcc"
CFLAGS="-c -fPIC -O3 -Wall -Werror -I$JAVA_HOME/include/ -I$JAVA_HOME/include/linux/"
Expand Down
6 changes: 6 additions & 0 deletions native/src/main/c/NativeCipherImpl.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
#include "shared.h"
#include "net_md_5_bungee_jni_cipher_NativeCipherImpl.h"

// Hack to keep the compiler from optimizing the memset away
static void *(*const volatile memset_func)(void *, int, size_t) = memset;

typedef unsigned char byte;

typedef struct crypto_context {
int mode;
mbedtls_aes_context cipher;
int keyLen;
byte key[];
} crypto_context;

Expand All @@ -22,6 +26,7 @@ jlong JNICALL Java_net_md_15_bungee_jni_cipher_NativeCipherImpl_init(JNIEnv* env
return 0;
}

crypto->keyLen = (int) keyLen;
(*env)->GetByteArrayRegion(env, key, 0, keyLen, (jbyte*) &crypto->key);

mbedtls_aes_init(&crypto->cipher);
Expand All @@ -36,6 +41,7 @@ void Java_net_md_15_bungee_jni_cipher_NativeCipherImpl_free(JNIEnv* env, jobject
crypto_context *crypto = (crypto_context*) ctx;

mbedtls_aes_free(&crypto->cipher);
memset_func(crypto->key, 0, (size_t) crypto->keyLen);
free(crypto);
}

Expand Down
5 changes: 5 additions & 0 deletions native/src/main/c/NativeCompressImpl.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <zlib.h>
#include "shared.h"
#include "cpuid_helper.h"
#include "net_md_5_bungee_jni_zlib_NativeCompressImpl.h"

typedef unsigned char byte;
Expand All @@ -26,6 +27,10 @@ jint throwException(JNIEnv *env, const char* message, int err) {
return (*env)->Throw(env, throwable);
}

JNIEXPORT jboolean JNICALL Java_net_md_15_bungee_jni_zlib_NativeCompressImpl_checkSupported(JNIEnv* env, jobject obj) {
return (jboolean) checkCompressionNativesSupport();
}

void JNICALL Java_net_md_15_bungee_jni_zlib_NativeCompressImpl_reset(JNIEnv* env, jobject obj, jlong ctx, jboolean compress) {
z_stream* stream = (z_stream*) ctx;
int ret = (compress) ? deflateReset(stream) : inflateReset(stream);
Expand Down
19 changes: 19 additions & 0 deletions native/src/main/c/cpuid_helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Header to check for SSE 2, SSSE 3, and SSE 4.2 support in compression natives
// GCC only!

#ifndef _INCLUDE_CPUID_HELPER_H
#define _INCLUDE_CPUID_HELPER_H

#include <stdbool.h>
#include <cpuid.h>

static inline bool checkCompressionNativesSupport() {
unsigned int eax, ebx, ecx, edx;
if(__get_cpuid(1, &eax, &ebx, &ecx, &edx)) {
return (edx & bit_SSE2) != 0 && (ecx & bit_SSSE3) != 0 && (ecx & bit_SSE4_2) != 0;
}else {
return false;
}
}

#endif // _INCLUDE_CPUID_HELPER_H
31 changes: 31 additions & 0 deletions native/src/main/c/mbedtls_custom_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

// This is a hack to deal with a glitch that happens when mbedtls is compiled against glibc
// but then run on a linux distro that uses musl libc. This implementation of the zeroize
// is compatible with both glibc and musl without requiring the library to be recompiled.

// I checked with a disassembler and for BungeeCord's usage of the library, implementing
// this function as a static function only resulted in 2 different subroutines referencing
// different versions of memset_func, so we might as well keep things simple and use a
// static function here instead of requiring the mbedtls makefile to be modified to add
// additional source files.

#ifndef _INCLUDE_MBEDTLS_CUSTOM_CONFIG_H
#define _INCLUDE_MBEDTLS_CUSTOM_CONFIG_H

#include <string.h>

#define MBEDTLS_PLATFORM_ZEROIZE_ALT

#define mbedtls_platform_zeroize mbedtls_platform_zeroize_impl

// hack to prevent compilers from optimizing the memset away
static void *(*const volatile memset_func)(void *, int, size_t) = memset;

static void mbedtls_platform_zeroize_impl(void *buf, size_t len) {
if (len > 0) {
memset_func(buf, 0, len);
}
}

#endif // _INCLUDE_MBEDTLS_CUSTOM_CONFIG_H

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 20 additions & 1 deletion native/src/main/java/net/md_5/bungee/jni/NativeCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,23 @@ public final class NativeCode<T>
private final String name;
private final Supplier<? extends T> javaImpl;
private final Supplier<? extends T> nativeImpl;
private final boolean enableNativeFlag;
private final boolean extendedSupportCheck;
//
private boolean loaded;

public NativeCode(String name, Supplier<? extends T> javaImpl, Supplier<? extends T> nativeImpl)
{
this( name, javaImpl, nativeImpl, false );
}

public NativeCode(String name, Supplier<? extends T> javaImpl, Supplier<? extends T> nativeImpl, boolean extendedSupportCheck)
{
this.name = name;
this.javaImpl = javaImpl;
this.nativeImpl = nativeImpl;
this.enableNativeFlag = Boolean.parseBoolean( System.getProperty( "net.md_5.bungee.jni." + name + ".enable", "true" ) );
this.extendedSupportCheck = extendedSupportCheck;
}

public T newInstance()
Expand All @@ -32,7 +41,7 @@ public T newInstance()

public boolean load()
{
if ( !loaded && isSupported() )
if ( enableNativeFlag && !loaded && isSupported() )
{
String fullName = "bungeecord-" + name;

Expand All @@ -59,13 +68,23 @@ public boolean load()
}

System.load( temp.getPath() );

if ( extendedSupportCheck )
{
// Should throw NativeCodeException if incompatible
nativeImpl.get();
}

loaded = true;
} catch ( IOException ex )
{
// Can't write to tmp?
} catch ( UnsatisfiedLinkError ex )
{
System.out.println( "Could not load native library: " + ex.getMessage() );
} catch ( NativeCodeException ex )
{
System.out.println( "Native library " + name + " is incompatible: " + ex.getMessage() );
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@ public NativeCodeException(String message, int reason)
{
super( message + " : " + reason );
}

public NativeCodeException(String message)
{
super( message );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class NativeCompressImpl

static native void initFields();

native boolean checkSupported();

native void end(long ctx, boolean compress);

native void reset(long ctx, boolean compress);
Expand Down
8 changes: 8 additions & 0 deletions native/src/main/java/net/md_5/bungee/jni/zlib/NativeZlib.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ public class NativeZlib implements BungeeZlib
private boolean compress;
private long ctx;

public NativeZlib()
{
if ( !nativeCompress.checkSupported() )
{
throw new NativeCodeException( "This CPU does not support the required SSE 4.2 and/or PCLMUL extensions!" );
}
}

@Override
public void init(boolean compress, int level)
{
Expand Down
Binary file modified native/src/main/resources/native-cipher.so
Binary file not shown.
Binary file modified native/src/main/resources/native-compress.so
Binary file not shown.
2 changes: 1 addition & 1 deletion native/src/test/java/net/md_5/bungee/NativeZlibTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public class NativeZlibTest
{

private final NativeCode<BungeeZlib> factory = new NativeCode<>( "native-compress", JavaZlib::new, NativeZlib::new );
private final NativeCode<BungeeZlib> factory = new NativeCode<>( "native-compress", JavaZlib::new, NativeZlib::new, true );

@Test
public void doTest() throws DataFormatException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class ClientCommand extends DefinedPacket
@Override
public void read(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion)
{
command = readString( buf, 256 );
command = readString( buf, ( protocolVersion >= ProtocolConstants.MINECRAFT_1_20_5 ) ? 32767 : 256 );
timestamp = buf.readLong();
salt = buf.readLong();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class UnsignedClientCommand extends DefinedPacket
@Override
public void read(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion)
{
command = readString( buf, 256 );
command = readString( buf, ( protocolVersion >= ProtocolConstants.MINECRAFT_1_20_5 ) ? 32767 : 256 );
}

@Override
Expand Down
1 change: 0 additions & 1 deletion proxy/src/main/java/net/md_5/bungee/ServerConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,6 @@ private void cutThrough(ServerConnection server)
// Remove from old servers
if ( user.getServer() != null )
{
user.getServer().setObsolete( true );
user.getServer().disconnect( "Quitting" );
}

Expand Down
1 change: 0 additions & 1 deletion proxy/src/main/java/net/md_5/bungee/UserConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,6 @@ public void disconnect0(final BaseComponent reason)

if ( server != null )
{
server.setObsolete( true );
server.disconnect( "Quitting" );
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
public class CompressFactory
{

public static final NativeCode<BungeeZlib> zlib = new NativeCode<>( "native-compress", JavaZlib::new, NativeZlib::new );
public static final NativeCode<BungeeZlib> zlib = new NativeCode<>( "native-compress", JavaZlib::new, NativeZlib::new, true );
}
2 changes: 1 addition & 1 deletion proxy/src/main/java/net/md_5/bungee/conf/YamlConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void load()
} ) );
set( "permissions.admin", Arrays.asList( new String[]
{
"bungeecord.command.alert", "bungeecord.command.end", "bungeecord.command.ip", "bungeecord.command.reload", "bungeecord.command.kick"
"bungeecord.command.alert", "bungeecord.command.end", "bungeecord.command.ip", "bungeecord.command.reload", "bungeecord.command.kick", "bungeecord.command.send", "bungeecord.command.find"
} ) );
}

Expand Down
28 changes: 9 additions & 19 deletions proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -619,34 +619,24 @@ private void finish()
}
}

ProxiedPlayer oldName = bungee.getPlayer( getName() );
if ( oldName != null )
{
// TODO See #1218
disconnect( bungee.getTranslation( "already_connected_proxy" ) );
return;
}

if ( isOnlineMode() )
{
// Check for multiple connections
// We have to check for the old name first
ProxiedPlayer oldName = bungee.getPlayer( getName() );
if ( oldName != null )
{
// TODO See #1218
disconnect( bungee.getTranslation( "already_connected_proxy" ) ); // TODO: Cache this disconnect packet
}
// And then also for their old UUID
ProxiedPlayer oldID = bungee.getPlayer( getUniqueId() );
if ( oldID != null )
{
// TODO See #1218
disconnect( bungee.getTranslation( "already_connected_proxy" ) ); // TODO: Cache this disconnect packet
}
} else
{
// In offline mode the existing user stays and we kick the new one
ProxiedPlayer oldName = bungee.getPlayer( getName() );
if ( oldName != null )
{
// TODO See #1218
disconnect( bungee.getTranslation( "already_connected_proxy" ) ); // TODO: Cache this disconnect packet
disconnect( bungee.getTranslation( "already_connected_proxy" ) );
return;
}

}

//BotFilter start
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.net.URLConnection;
import lombok.Data;
import net.md_5.bungee.Util;
import net.md_5.bungee.api.ProxyServer;

@Data
public class JenkinsModuleSource implements ModuleSource
Expand All @@ -15,7 +16,7 @@ public class JenkinsModuleSource implements ModuleSource
@Override
public void retrieve(ModuleSpec module, ModuleVersion version)
{
System.out.println( "Attempting to Jenkins download module " + module.getName() + " v" + version.getBuild() );
ProxyServer.getInstance().getLogger().info( "Attempting to Jenkins download module " + module.getName() + " v" + version.getBuild() );
try
{
URL website = new URL( "https://ci.md-5.net/job/BungeeCord/" + version.getBuild() + "/artifact/module/" + module.getName().replace( '_', '-' ) + "/target/" + module.getName() + ".jar" );
Expand All @@ -25,10 +26,10 @@ public void retrieve(ModuleSpec module, ModuleVersion version)
con.setReadTimeout( 15000 );

Files.write( ByteStreams.toByteArray( con.getInputStream() ), module.getFile() );
System.out.println( "Download complete" );
ProxyServer.getInstance().getLogger().info( "Download complete" );
} catch ( IOException ex )
{
System.out.println( "Failed to download: " + Util.exception( ex ) );
ProxyServer.getInstance().getLogger().warning( "Failed to download: " + Util.exception( ex ) );
}
}
}
10 changes: 5 additions & 5 deletions proxy/src/main/java/net/md_5/bungee/module/ModuleManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void load(ProxyServer proxy, File moduleDirectory) throws Exception
ModuleVersion bungeeVersion = ModuleVersion.parse( proxy.getVersion() );
if ( bungeeVersion == null )
{
System.out.println( "Couldn't detect bungee version. Custom build?" );
proxy.getLogger().warning( "Couldn't detect bungee version. Custom build?" );
return;
}

Expand Down Expand Up @@ -105,19 +105,19 @@ public void load(ProxyServer proxy, File moduleDirectory) throws Exception
ModuleSource source = knownSources.get( uri.getScheme() );
if ( source == null )
{
System.out.println( "Unknown module source: " + s );
proxy.getLogger().warning( "Unknown module source: " + s );
continue;
}
String name = uri.getAuthority();
if ( name == null )
{
System.out.println( "Unknown module host: " + s );
proxy.getLogger().warning( "Unknown module host: " + s );
continue;
}

ModuleSpec spec = new ModuleSpec( name, new File( moduleDirectory, name + ".jar" ), source );
modules.add( spec );
System.out.println( "Discovered module: " + spec );
proxy.getLogger().info( "Discovered module: " + spec );
}

for ( ModuleSpec module : modules )
Expand All @@ -126,7 +126,7 @@ public void load(ProxyServer proxy, File moduleDirectory) throws Exception

if ( !bungeeVersion.equals( moduleVersion ) )
{
System.out.println( "Attempting to update plugin from " + moduleVersion + " to " + bungeeVersion );
proxy.getLogger().info( "Attempting to update plugin from " + moduleVersion + " to " + bungeeVersion );
module.getProvider().retrieve( module, bungeeVersion );
}
}
Expand Down

0 comments on commit e249b4f

Please sign in to comment.