Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ChannelForwardedTCPIP.getPortForwarding(Session) DO NOT CHECK a Session instance. #52

Closed
parkjunhong opened this issue Jul 9, 2021 · 3 comments

Comments

@parkjunhong
Copy link

parkjunhong commented Jul 9, 2021

HI, I'm Park, Jun Hong in Korea.

I have been used a original Jsch and developed a program that maintains a ssh tunneling.

I found a below bug and fixed it for my application.

So, I report this bug and hope to fix this bug for here.

Found Bugs
Recently I've finished a program that provides a remote port forwarding. This program provides 3 functions.

  1. Connects a remote port forwarding.
  2. Disconnects a remote port forwarding connection.
  3. Lists remote port forwarding.

I found a bug that Remote Port Forwardings are mixed, although it's remote server is different from each other.

A method that provides Local Port Forwarding checks a Session instance.

file: com.jcraft.jsch.PortWatcher.java

static String[] getPortForwarding(Session session){
  java.util.Vector foo=new java.util.Vector();
 
  synchronized(pool){
    for(int i=0; i<pool.size(); i++){
      PortWatcher p=(PortWatcher)(pool.elementAt(i));
      if(p.session==session){
        foo.addElement(p.lport+":"+p.host+":"+p.rport);
      }
    }
  }

String[] bar=new String[foo.size()];
for(int i=0; i<foo.size(); i++){
bar[i]=(String)(foo.elementAt(i));
}
return bar;
}
But a method that provides Remote Port Forwardings DO NOT CHECK a Session instance.

file: com.jcraft.jsch.ChannelForwardedTCPIP.java

static String[] getPortForwarding(Session session){
  Vector foo = new Vector();
 
  synchronized(pool){
    for(int i=0; i<pool.size(); i++){
      Config config = (Config)(pool.elementAt(i));
      if(config instanceof ConfigDaemon)
        foo.addElement(config.allocated_rport+":"+config.target+":");
      else
        foo.addElement(config.allocated_rport+":"+config.target+":"+((ConfigLHost)config).lport);
    }
  }
 
  String[] bar=new String[foo.size()];
  for(int i=0; i<foo.size(); i++){
    bar[i]=(String)(foo.elementAt(i));
  }
  return bar;
}

To fix this bug, add codes to check whether config.session is equals to the parameter 'session'.

 static String[] getPortForwarding(Session session) {
        Vector foo = new Vector();
        synchronized (pool) {
            for (int i = 0; i < pool.size(); i++) {
                Config config = (Config) (pool.elementAt(i));
                // (start) [BUG-FIX]: check an instance of a session / Park_Jun_Hong_(parkjunhong77_at_gmail_com): 2020.
                // 5. 25. PM. 5:34:14
                if (!config.session.equals(session)) {
                    continue;
                }
                // (end): 2020. 5. 25. PM. 5:34:14

                if (config instanceof ConfigDaemon)
                    foo.addElement(config.allocated_rport + ":" + config.target + ":");
                else
                    foo.addElement(config.allocated_rport + ":" + config.target + ":" + ((ConfigLHost) config).lport);
            }
        }
        String[] bar = new String[foo.size()];
        for (int i = 0; i < foo.size(); i++) {
            bar[i] = (String) (foo.elementAt(i));
        }
        return bar;
    }
@norrisjeremy
Copy link
Contributor

Hi @parkjunhong,
Could you submit a PR with the proposed changes?
That will help to review and better understand the issue.

@norrisjeremy
Copy link
Contributor

norrisjeremy commented Sep 2, 2021

Hi @parkjunhong,

I have included your suggest fix as part of PR #64 (911358b) that I have opened.

Thanks,
Jeremy

@norrisjeremy
Copy link
Contributor

Hi @parkjunhong,

Your suggested fix was included in the 0.1.66 release.

Thanks,
Jeremy

@mwiede mwiede closed this as completed Mar 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants